help pls in turbo c
help pls in turbo c
help i need to create this output using loops
square cube 1 1 1 2 4 8 3 9 27 4 16 64 5 25 125
10 Jul 08, 3:48PM
Easy.
#include<stdio.h> int main() { for(int i=1;i<6;i++) { printf("\n%d\t%d\t%d",i,i*i,i*i*i); } }
10 Jul 08, 4:40PM
thanks sir.. i need another help i want a prgram that actually adds the number input then displays the sum when 0 is enterd. all i have is this old code for adding the sum.
#include<stdio.h> #include<conio.h> main() { int sum,n1,n2,n3,n4; clrscr(); printf("Enter numbers: "); scanf("%d%d%d%d",&n1,&n2,&n3,&n4); sum=n1+n2+n3+n4; printf("\n The sum is: %d", sum); getch(); }
10 Jul 08, 4:57PM
so use a while loop, as in
... while(1) { scanf("%d",&n1); sum+=n1; if(n1==0) break; } ....I hope this isn't home work.
10 Jul 08, 4:59PM
but it still needs to have 4 input first... can it be if 0 is entered it will give the sum already?? nope its not a homework.. i want to make it clear cause my prof discuss to fast i cant understand some of the program she is teaching..
10 Jul 08, 5:37PM
You want the program to keep adding until zero is entered.
i.e. You could enter:
3 4 2 3 7 8 9 10 0
OR just
2 4 0
Note you would hit the 'return' key between each of the above numbers.
10 Jul 08, 5:40PM
can you give me an example??
10 Jul 08, 5:48PM
I did earlier. The while loop I showed is everything you need. You just need to initialise the sum variable and set it to zero, then print the results afterwards.
I'm happy to help with minor problems - however you can only learn to program through experimenting, making mistakes, correcting these and learning as a result.
What does you code currently look like?
10 Jul 08, 5:54PM
it looks like this
#include<stdio.h> #include<conio.h> main() { int sum,n1,n2,n3,n4; clrscr(); printf("Enter numbers: "); scanf("%d%d%d%d",&n1,&n2,&n3,&n4); sum=n1+n2+n3+n4; while(1) { scanf("%d",&n1); sum+=n1; if(n1==0) break; } printf("\n The sum is: %d", sum); getch(); }
10 Jul 08, 6:25PM
You've a few too many lines. 2 to be precise.
You should also include
sum=0;I think for you to learn you need to take that all away and think through what each line is doing for you. You do know how to compile this don't you?
Login