Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

what's wrong in this?

+2 votes
asked Jul 17, 2020 by pranjul garg (140 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;
}

7 Answers

+2 votes
answered Jul 20, 2020 by Peter Minarik (84,720 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, 2020 by LiOS (6,420 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, 2020 by Peter Minarik (84,720 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, 2020 by LiOS (6,420 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, 2020 by Peter Minarik (84,720 points)
Exactly!

Well said.
0 votes
answered Jul 21, 2020 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, 2020 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, 2020 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 .

0 votes
answered Feb 4, 2021 by sofibilal193 (620 points)
1. You have used semi-colon in for() loop statement, Remove it.

2. Remove \n in Scanf() statement, it automatically goes on next line after entering the number.
0 votes
answered Feb 7, 2021 by Arpan Tyagi (140 points)
First error is
[Scanf("%d",&a);]
Second error is [for table print ]
For(i=1;i<=10;i++)
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.
...