Home > Back-end >  Output Modification required
Output Modification required

Time:09-01

I want to print out dividends that exist in a range and their sum. let's say I give a range between 15 and 85 and print numbers divisible by 4. it works fine. but I want output in a specific shape such that " 16 32 48 64 80 =240. The problem I am facing is that when I print " " with the printf statement, the signs also come after the last output such as 80 . which is wrong. kindly guide me through it. The current output is : 16 32 48 64 80 =240

 #include <stdio.h>
#include <conio.h>
int main()
{
int first=0;
int second=0;
printf("Enter first number :");
scanf_s("%d",&first);
printf("Enter second number :");
scanf_s("%d",&second);
first  ;
int sum=0;
while(first<=second)
{
    if(first%4==0 && first==0)
    {
        printf("%d  ",first);
        printf("  ");
        sum =first;
    }
    first  ;
}
printf("  =%d ",sum);
}

CodePudding user response:

Edit : then

#include <stdio.h>

int main(void)
{
    int first=0;
    int second=0;

    printf("Enter first number :");
    scanf("%d",&first);

    printf("Enter second number :");
    scanf("%d",&second);

    int sum=0;
    int start=0;
    while(first<=second)
    {
        if(first==0)
        {
            if(start) printf("  ");
            start=1;
            printf("%d ", first);
            sum =first;
        }
        first  ;
    }
    printf("= %d ",sum);

    return 0;
}

CodePudding user response:

Don't think of what follows, prepare for what might come.

I've made two commented changes to your code:

char *connect = ""; // a connection string used soon
while(first<=second)
{
    if(first%4==0 && first==0)
    {
        printf( "%s%d", connect, first );
        connect = "   "; // if another value output, this prints first
        sum  = first;
    }
    first  ;
}
printf("  = %d ",sum);

EDIT:

Keeping the general look/feel of the OP code, the following provides a measure of improvement, imho. Humbly offered for whomsoever may find it worthy of study.

#include <stdio.h>

int main()
{
    for( ;; ) { // infinite loop
        int first;
        printf( "Enter first number :" );
        scanf_s( " %d", &first );

        int second = first - 1;
        while( second <= first ) {
            printf( "Enter second number (>%d):", first );
            scanf_s( " %d", &second );
        }

        int interval = 0; // acts like modulo
        while( interval < 2 ) {
            printf( "Enter interval (2 ):" );
            scanf_s( " %d", &interval );
        }

        first  = interval - (first % interval);
        // Or, one alternative start value
        // first  = interval - ( first % interval );

        int sum = 0;
        char *connect = ""; // Answering OP problem of printing
        while( first <= second )
        {
            printf("%s%d", connect, first );
            connect = "   "; // subsequent numbers prefixed by "   "
            sum  = first;
            first  = interval;
        }

        printf( " = %d\n", sum );
    }

    return 0;
}

Output:

Enter first number :-170
Enter second number (>-170):90
Enter interval (2 ):42
-126   -84   -42   0   42   84 = -126
Enter first number :38
Enter second number (>38):300
Enter interval (2 ):42
42   84   126   168   210   252   294 = 1176

CodePudding user response:

one solution found. That is to print the sum in this way:

print("\b\b= %d",sum);

The sign will be printed by last the dividend output. so we can use \b escape sequence to move back in the console. In this case, we use "\b\b" to move backward two times .

  • Related