hey hi...i was supposed to read a 4 digit number and then find tje sum of its digts.....tell me what went wrong.....keep it too simple..... Code: #include<stdio.h> main() { int num,a,b,c,d,e; printf("enter the number"); scanf("%d",&num); a=(num/1000); b=num/100; c=num/10; d=num%1000; e=a+b+c+d; printf("\n%d",&a); printf("\n%d",&b); printf("\n%d",&c); printf("\n%d",&d); printf("the sum is %d",&e); return(0); }
Hi! 1. You use address of variables when you print numbers (&). 2. Take any number and try to calculate manually using your formula. It doesn't work. Try to use loop to calculate the sum.
i think this would help u :- Code: #include<stdio.h> #include<conio.h> void main() { clrscr(); int num; int a,b,c,d,e,f,g,h; printf("enter a no"); scanf("%d",&num); a=num/1000; printf("d is %d \n",a); b=num/100; e=b%10; printf("e is %d \n",e); c=num/10; f=c%10; printf("b is %d \n ",f); d=num%1000; g=d%10; printf("a is % d \n",g); h=a+e+f+g; printf ("the sum is%d ",h); getch(); }
hello... it is the solution for your problem... Code: #include<stdio.h> #include<conio.h> void main(void) { clrscr(); float num,a,b,c,d,e; printf("enter the number"); scanf("%f", &num); a= num/1000; b=num/100; c=num/10; d=num/1000; e=a+b+c+d; printf("\n%1f", a); printf("\n%1f", b); printf("\n%1f", c); printf("\n%1f", d); printf("\nthe sum is %f", e); getch(); }
Code: #include<stdio.h> #include<conio.h> void main(void) { clrscr(); int num,a,b,c,d,e; printf("enter a 4 digit number : "); //1234 scanf("%d",&num); a=num/1000; //1 b=(num - (a*1000))/100; //(1234 - 1*1000)/100 = 234/100 = 2 c=(num - (a*1000) - (b*100))/10; //(1234 - 1*1000 - 2*100)/10 = 34/10 = 3 d=(num - (a*1000) - (b*100) - (c*10))/1; //(1234 - 1*1000 - 2*100 - 3*10)/1 = 34/1 = 1 e=a+b+c+d; printf("\n%d",a); printf("\n%d",b); printf("\n%d",c); printf("\n%d",d); printf("\nthe sum is %d\n",e); return 0; }
its the solution.......!!! Code: #include<stdio.h> #include<conio.h> void main(void) { int a,b,c,d,e,num; clrscr(); printf("enter a 4 digit number : "); scanf("%d",&num); a=num/1000; b=(num - (a*1000))/100; c=(num - (a*1000) - (b*100))/10; d=(num - (a*1000) - (b*100) - (c*10))/1; e=a+b+c+d; printf("\n%d",a); printf("\n%d",b); printf("\n%d",c); printf("\n%d",d); printf("\nthe sum is %d\n",e); getch(); }