what's wrong in this?

+1 vote
asked Jul 17 by pranjul garg (130 points)
#include <stdio.h>
int main()
{
    int a,i;
    printf("enter a number:");
    scanf("%d \n",&a);
    for(i=1;i<=10; ++i);
    {
        printf("%d * %d = %d \n", i, a,i*a);
        
    }
return 0;
}

5 Answers

+2 votes
answered Jul 20 by Peter Minarik (7,160 points)

You want to specify only the number you want to read and no extra in the scanf. Like this:

scanf("%d", &a);

You have a semicolon at the end of the line of the for loop, meaning it does nothing. Remove it, like this:

for (i = 1; i <= 10; ++i)

0 votes
answered Jul 20 by LiOS (4,320 points)
1 - The input hangs after entering a number because of the presence of the escape sequence \n.

Note, that a character is also accepted too in the scanf.

2 - The for loop has a ; at the end which terminates the process but will still re-iterate and is the reason it will print out the very last iteration of 11 * number = answer

Removing the ; will repeat allow iteration of the for loop, printing the result each time e.g 1 * number = answer, 2 * number = answer....
commented Jul 21 by Peter Minarik (7,160 points)
*The input does not hang* after hitting "ENTER". It actually is expecting an "ENTER" as specified by t he "\n" and just waiting more input as specified by the format string. If after that, the user provides more input, scanf is finished and the code proceeds to the next line.

http://www.cplusplus.com/reference/cstdio/scanf/
commented Jul 22 by LiOS (4,320 points)
True.... used the wrong terminology. However, the result of the presence of \n is pointless and impractical and should be removed. For example, typing 5, enter and 4 only 'reads' the 5 and assigns it to variable a, as it should do, but the need to type additional input shouldn't be required.
commented Jul 22 by Peter Minarik (7,160 points)
Exactly!

Well said.
0 votes
answered Jul 21 by himanshu jain (140 points)

Question:- Print the table of any given number.

Psudo:- 

  1. Take Input  let's say a 
  2. Run a Loop say i  from 1 to 10 
  3. print( i * a) 

 

The only problem in your solution is that you have just terminated the FOR loop after initialized. 

look here:- for (i = 1; i <= 10; ++i) ;

This is your Typing mistake. 

Hence your answer is printing only one time .

0 votes
answered Jul 22 by Abhishek Kumar (220 points)

Remove "\n" form the scanf statement;

like- scanf("%d",&a);

Also remove ";" after for loop;

like- for(i=1;i<n;i++)

0 votes
answered Jul 22 by Dabons22 (250 points)

look here:- for (i = 1; i <= 10; ++i) ;

This is your Typing mistake. 

Hence your answer is printing only one time .

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...