Ask your own question, for FREE!
Computer Science 16 Online
OpenStudy (anonymous):

help pls int main() { int n; printf("n="); scanf("%lf",n); for(i=0;;i++); if(i=10)break; printf("%d",i,i*i); return 0; } can someone help me correct this code. too many mistakes in this code. tried countless times but failed :(

OpenStudy (anonymous):

Firstly you need to add an #include directive for stdio.h. Aside from that, you need to use the proper format specifier in scanf... %lf corresponds to reading a double, rather than an int; maybe you meant %d? Your loop is tight and doesn't do anything since you have a semicolon right after the for statement; even if the loop were correct, your if statement should be eliminated in favor of a loop condition i.e. for (i = 0; i < 10; ++i)... note that your if statement is broken anyways, since if (i = 10) is not the same thing as if (i == 10) -- the latter compares i and 10 while the former reassigns i and thus will always take the branch since (i = 10) evaluates to 10 which is non-zero. Did you even mean to use 10, rather than n, here? Your final printf call is probably incorrect, too, since you specify two arguments (i and i * i) while only formatting the first, and there's no white space for delimiting. You don't need to return 0 since main by default returns 0 according to the C standard, and you should really use braces and new lines to make teh code less cluttered. Here's a corrected form of your code: #include <stdio.h> int main() { int n; printf("n="); scanf("%d", &n); int i; for (i = 0; i < n; ++i) { printf("%d %d\n", i, i * i); } }

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!