Home > Mobile >  how to add a new line to only the end of the output sequence
how to add a new line to only the end of the output sequence

Time:10-20

#include <stdio.h>

int main(void) {

  int num1;

 int num2;

 int i;
 
 scanf("%d", &num1);
  scanf("%d", &num2);
 
if (num1 <= num2) {
   for(i = num1; i <= num2; i  =5 ){
   
   printf("%d \n",i);
  
   
   
   }
}
   
   else {
   
   printf("Second integer can't be less than the first.\n");
   }
   return 0;
}

I'm trying to only add one new line to the end of the outputs but every time I execute this program it adds several new lines to the end of each output and I only need one at the very last output. for example, if I input -15 10 then I will get an output of

  -15 
-10 
-5 
0 
5 
10

when I'm only looking for one new line at the end of the 10 I want my output to look like this with only one new line at the end.

-15 -10 -5 0 5 10 \n

CodePudding user response:

Print your newline outside of the for loop. This way you can print all your numbers, then when finished put a new line on the end.

#include <stdio.h>

int main(void) {
  int num1;
  int num2;
  int i;
 
  scanf("%d", &num1);
  scanf("%d", &num2);
 
  if (num1 <= num2) {
    for(i = num1; i <= num2; i  =5 ){
      printf("%d ",i);
    }
    printf("\n");
  } else {
    printf("Second integer can't be less than the first.\n");
  }
  return 0;
}

CodePudding user response:

Don't include \n inside the loop. Print it when the loop is done.

for(i = num1; i <= num2; i  =5 ){
   printf("%d ",i);
}
printf("\n");

CodePudding user response:

Scince the obvious answer is, well... obvious, and also well covered by other answers, consider this alternative;

for( i = num1; i <= num2; i  = 5 )
{
    printf( "%d%c", i,
            i   5 > num2 ? '\n' : 
                           ' ' ) ;
}

It is somewhat more complicated, but does not print a trailing space in the line.

The expression i 5 > num2 is a test to determine whether the loop will terminate after i = 5. However you would do well to define a symbolic constant for that so that given, say:

#define STEP 5

then:

for( i = num1; i <= num2; i  = STEP )
{
    printf( "%d%c", i,
            i   STEP > num2 ? '\n' : 
                              ' ' ) ;
}

It can be simplified a little with a while loop:

i = num1 ;
while( i <= num2 )
{
    printf( "%d", i ) ;
    i  = STEP ;
    printf( "%c", i > num2 ? '\n' : 
                             ' ' ) ;
}

CodePudding user response:

You really need to pay attention to the formatting of your code. LOTS of bugs hide in poorly formatted code, and code that uses more variables and statements than are needed, and code that does not validate user data before using it.

Here's a version that is direct and to the point:

#include <stdio.h>

int mmain() {
    int n1, n2; // short names; easy to type and to read.

    // Get two integers (without a prompt)
    if( scanf( "%d%d", &n1, &n2 ) != 2 || n1 > n2 ) {
        printf( "Bad data. Second integer must be greater than first.\n" );
        return 1; // early termination as execution cannot continue.
    }

    // loop, printing values, each with appropriate next character (SP or LF)
    do
        printf( "%d%c", n1, " \n"[n1 5>n2] );
    while( (n1 =5) <= n2 );

    return 0;
}
  •  Tags:  
  • c
  • Related