p6

Write a C program to define a function that will take an integer as argument and return the sum of digits of that integer

#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

No comments:

Post a Comment