Sum of digits of a number in c using for loop

Posted By: Matpal - January 25, 2012
#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;
}

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.