Practice Program







  • Program of Sum of digits
  • Solution: through while loop
    void main()
    {
        int n,a,sum=0;
    
        printf("Enter the  Number: ");
        scanf("%d",&n);
    
    
        while(n!=0)
        {
            a = n%10;//as modulo operator gives
                     //last digit of number
                    //when we modulo number with 10.
            sum = sum+a;
            
            n = n/10;
                 //when we divide a number with 10
                 //then it removes the last digit from
                //the number
        }
    printf("Sum of digits is %d",sum);
    
    }
    

    Output1
    Enter the Number:321

    Sum of digits is 9.
    Output2
    Enter the Number:3421

    Sum of digits is 10
    Output3
    Enter the Number:456

    Sum of digits is 15





    Solution: from do-while loop
    void main()
    {
        int n,a,sum=0;
    
        printf("Enter the  Number: ");
        scanf("%d",&n);
    
    
        do
        {
            a = n%10;//as modulo operator gives
                     //last digit of number
                    //when we modulo number with 10.
            sum = sum+a;
            
            n = n/10;
                 //when we divide a number with 10
                 //then it removes the last digit from
                //the number
        }while(n!=0);
    printf("Sum of digits is %d",sum);
    
    }
    






    defining a function 'count_digit_sum' which will return sum of digits.
    int count_digit_sum(int n)
    {
        int a,sum = 0;
         while(n!=0)
        {
            a = n%10;//as modulo operator gives
                     //last digit of number
                    //when we modulo number with 10.
            sum = sum+a;
    
            n = n/10;
                 //when we divide a number with 10
                 //then it removes the last digit from
                //the number
        }
    
        return sum;
    }
    

    Using above define function 'count_digit_sum':
    void main()
    {
        int n,result;
    
        printf("Enter the  Number: ");
        scanf("%d",&n);
    
    
        //calling function
        result = count_digit_sum(n);
    
    printf("Sum of digits is %d",result);
    
    }
    

    Output1
    Enter the Number:321

    Sum of digits is 9.

    Using above define function 'count_digit_sum':
    void main()
    {
        int n,result;
    
        printf("Enter the  Number: ");
        scanf("%d",&n);
    
    
        //calling function
        result = count_digit_sum(n);
    
    printf("Sum of digits is %d",result);
    
    }
    

    Output2
    Enter the Number:456

    Sum of digits is 15.

    Using above define function 'count_digit_sum':
    void main()
    {
        int n,result;
    
        printf("Enter the  Number: ");
        scanf("%d",&n);
    
    
        //calling function
        result = count_digit_sum(n);
    
    printf("Sum of digits is %d",result);
    
    }
    

    Output3
    Enter the Number:326

    Sum of digits is 11.