Write a c program to find out sum of digit of given number





Code 1:
1. C program to add digits of a number
2. C program for sum of digits of a number
3. C program to calculate sum of digits

#include<stdio.h>
int main(){
  int num,sum=0,r;
  printf("Enter a number: ");
  scanf("%d",&num);
  while(num){
      r=num%10;
      num=num/10;
      sum=sum+r;
  }
  printf("Sum of digits of number:  %d",sum);
  return 0;
}

Sample output:
Enter a number: 123
Sum of digits of number:  6

Code 2:
1. Sum of digits of a number in c using for loop

#include<stdio.h>
int main(){
  int num,sum=0,r;
  printf("Enter a number: ");
  scanf("%d",&num);

  for(;num!=0;num=num/10){
      r=num%10;
      sum=sum+r;
  }
  printf("Sum of digits of number:  %d",sum);
  return 0;
}

Sample output:
Enter a number: 567
Sum of digits of number:  18

Code 3:
1. Sum of digits in c using recursion

#include<stdio.h>

int getSum(int);
int main(){
  int num,sum;
  printf("Enter a number: ");
  scanf("%d",&num);

  sum = getSum(num);

  printf("Sum of digits of number:  %d",sum);
  return 0;
}

int getSum(int num){

    static int sum =0,r;

    if(num!=0){
      r=num%10;
      sum=sum+r;
      getSum(num/10);
    }

    return sum;
}

Sample output:
Enter a number: 45
Sum of digits of number:  9






32 comments:

ABHISHEK YADAV said...

good program

Anonymous said...

thanx dude

Gauri said...

please tell me how to write a programme to generate Fibonacci series using recursion

Kamala said...

Sir, Plz tell me When there is the requirement to see the numbers from 1 to 2000 using for...loop it displays one screen i want to see it using pagewise. what is the procedure.

Kamala kanta Rath

vivek arya said...

cool but each information is necessary to us so upload every one programme//

Rimi said...

explain me the logic

pratik1106 said...

what is the use of while loop here?

Unknown said...

Can any body tell me if i want to sum of a digit of a fraction no then what shal i hav to do?
E.g 123456.789
1+2+3+4+5+6+7+8+9=45

Unknown said...

EXCELLENT PROGRAMINGWITH ANSWERS
IT'S LOT OF
HELP YOU

Anonymous said...

please tell me how to write c program to find second largest digit of given number using function

Shailendra Singh said...

literally..a when ever i see his answers, i find myself nothing....his logic is too good..

Anonymous said...

Thanks a lots..its help me as a beginner on programming.

sara said...

Really appreciate ur work !
Thanks

Anonymous said...

Check your programme in which u used while loop:while (num) it should be while (num!=0).

Anand said...

It is correct.
Every non-zero value is treated as true.
So the loop iterates till num reaches 0.

Unknown said...

can anyone please help me write a code to calculate the sum of the input given number using 3 LOOPING STATEMENT??? thank you!

Unknown said...

code1 is not working for six digit number like123456 (producing garbage value)...can anyone help me

Unknown said...

pls say why we are using while loop here.....................

Anonymous said...

how to print diagonal elements of 3*3 matrix

Anonymous said...

hw to print sum of integers in the range 1 to 100 till it encounters -1. print all sum of numbers before it encounters -1.

Arpit Sharma said...

Must!! Urget. Can anyone??

WAP to accept the value under 100 and multiply the digit till you get the result in single digit.

Ex: 28, 2*8=16 =1*6 = 6

Unknown said...
This comment has been removed by the author.
Unknown said...
This comment has been removed by the author.
Unknown said...
This comment has been removed by the author.
Unknown said...

plz tell me the program to add two numbers each of 100 digits.

Unknown said...

plz tell me the program to add two numbers each of 100 digits.

Unknown said...

plz tell me the program to add two numbers each of 100 digits.

Unknown said...

Int I=1,j=1,r;
While(I<=12)
{ while(j<=12)
{
r=pow(I,j);
Printf("%d power %d= %d",I,j,r);
J++;
}
I++;
}
// I need the output to be 1 up to 12 of powers so where is the error//

Unknown said...

Because it's works for particular digits only...you have to use for loop...

Unknown said...

Sorry, but the condition while(n!=0) will not work when we enter numbers like 4098,902,8904 which includes 0 in it. It will only do sum of digits of numbers after 0. So it is not working for me. Can u plzz help me to sort out this. :(

Unknown said...

One more think Please add a feature here to reply others.

Anonymous said...

How to perform 13/5 ans should display 2.6