Header Ads Widget

Responsive Advertisement

Sum of Digits

 



#include <stdio.h>


int main()
{

  int a,b,c,n,x,y,z, sum=0,su,rem,r,s=0,e,m=0;
    printf("Enter a number(3 digits only) : ");
    scanf("%d",&n);
   
    //Q.1 Without Using Any Loop ;
 
   a=n%10;
   n=n/10;
   b=n%10;
  n=n/10;
  c=n;
    su = a+b+c;
    printf("The sum of is : %d\n " ,su);
   
  //Q2. Using "While" Loop :  
   
    printf("Enter a number : ");
    scanf("%d",&x);
   while(x!=0){
   rem = x%10;
    sum = sum + rem;
    x=x/10;
    }
    printf("The sum of is : %d\n " ,sum);
   
   // Q3. Using "For" Loop :
   
    printf("Enter a number : ");
    scanf("%d",&y);
   for(; y>0 ; ){
   r = y%10;
    s =s+r;
    y=y/10;
    }
    printf("The sum of is : %d\n " ,s);
  
   
    // Q4. Using "Do While" Loop :
       

       printf("Enter a number : ");

    scanf("%d",&z);
   do{
   e = z%10;
    m = m + e;
    z=z/10;
    }while(z!=0);
    printf("The sum of is : %d\n " ,m);
   
   
   
   
    return 0;
}
   
 


Post a Comment

0 Comments