Write a C Program to Find the Sum of The Digits of a Number



Sum of The Digits, C Program, Sumit Kar, Timus Rak, while, for






/*Using While loop*/



#include <stdio.h>


void main()

{

 int n, s = 0;


 printf("Enter a number: ");

 scanf("%d",&n);


 while (n > 0)

 {

  s = s + n%10;

  n = n/10;

 }


 printf("Sum of digits is: %d", s);

}








/*Using For loop*/







#include <stdio.h>


void main()


{


 int n, s;




 printf("Enter a number: ");


 scanf("%d",&n);




 for(s=0;n>0;n=n/10)


 {


  s = s + n%10;


 }




 printf("Sum of digits is: %d", s);


}



Comments

Popular posts from this blog

Write a Program to Add two 3x3 Matrix using C

C program for Unit Conversion

Write a Program to Add two 5x5 Matrix using C