i have to write program in C using do-while loop,sum of i^2,where step is -3, i>3,i<43. and stop it (break) if i=23. Can someone help me? @Computer Science
put here your program.
do you mean my code? int main() { int i,sum; do { printf("%d\n",sum); sum+=i*i; } while(i>3); { i=43; i-=3; if(i==23) break; } return(0); }
its incorrect
#include <stdio.h> int main() { int i,sum; //inicializing variables sum=0; i=43; //first value step=-3; //decrement do { printf("%d\n",sum); sum+=sum+i*i; i=i+step; } while(i>23); return(0); }
i dint test it! there may be something wrong with the language, but the logic is this.
hmm, something is wrong because it gives 6 results instead of one.
oh you want so see only the final result... i didint consider that.. but the result are correct?
no, it gives 43882,but the correct answer is 9779 It works just with while and break: i=43; while (i>=3) { if (i==23) break; sum2+=i*i; i-=3; } printf(" %d\n", sum2); but unfortunely i need it with do-while
prehisto, i tested the code here and its working! I tested to i=26 and the result was 676 i=29 >>>>1517
#include <stdio.h> #include <iostream.h> int main() { int i,sum,step; //inicializing variables sum=0; i=28; //first value step=-3; //decrement do { sum=sum+i*i; i=i+step; } while(i>23); printf("%d\n",sum); system("PAUSE"); return(0);
test yourself
ok
so?
the problem is that i m using Codeblocks,and you re probably using somethinf else,i m correcting syntax right now.
im using dev c++
thats the way. I need to go out right now... if works click on 'good answer' =)
ok :)
good luck
Just so you know, the compiler shouldn't matter much... especially if you're on the same operating system. Remember how do ... while loops work. I mean, yes, getting the answer right is good, but if you want to program you need to understand these structures well, especially comparisons between them Here's a problem if you have time and want to get a better understanding. Try writing the code you did above with a for loop and a while loop as well. Then consider the differences. idk, taking the problem a little bit farther when you finish can sometimes do great things for understanding
Here are the problem specifications: 1. do-while loop 2. sum i*i 3. i starts at 43 4. i changes by -3 with each step 5. break if i == 23 Start at the beginning. Write a do-while loop: do { printf("in loop\n"); } while (true); Then use the second specification. sum i*i. So, you'll need variables for sum and i. int i; int sum; do { sum += i*i; printf("i = %d, sum = %d\n", i, sum); } while (true); Add in the third spec. int i = 43; int sum; do { sum += i*i; printf("i = %d, sum = %d\n", i, sum); } while (true); Add in the fourth spec. int i = 43; int sum; do { sum += i*i; printf("i = %d, sum = %d\n", i, sum); i += -3; } while (true); Add in the last spec. int i = 43; int sum; do { sum += i*i; printf("i = %d, sum = %d\n", i, sum); i += -3; if (i == 23) break; } while (true); Don't write the whole thing at once and expect it to work. When it doesn't work you won't know where to start looking. Write something small that works, then add on to it. Test the code after each change. Does it do what you expect? If so, add another step. If not, figure out why the code behaves the way it's behaving, then try to fix it. Don't move on until it works. If you're slow and systematic you'll have greater success. If you guess at everything before testing anything, you'll almost always have problems. I didn't do everything exactly right, and I didn't compile or run the code. But you can take each step I wrote and follow the process I described, and tweak the code until it works correctly, and matches your problem specification.
Join our real-time social learning platform and learn together with your friends!