Practice Program







  • Program of Product of digits
  • Solution: through while loop
    void main()
    {
        int n,a,product=1;
    
        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.
            product = product*a;
            
            n = n/10;
                 //when we divide a number with 10
                 //then it removes the last digit from
                //the number
        }
    printf("Product of digits is %d",product);
    
    }
    

    Output1
    Enter the Number:321

    Product of digits is 6.
    Output2
    Enter the Number:3421

    Product of digits is 24.
    Output3
    Enter the Number:456

    Product of digits is 120.





    Solution: from do-while loop
    void main()
    {
        int n,a,product=1;
    
        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.
           product = product*a;
            
            n = n/10;
                 //when we divide a number with 10
                 //then it removes the last digit from
                //the number
        }while(n!=0);
    printf("Product of digits is %d",product);
    
    }
    






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

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

    Output1
    Enter the Number:321

    Product of digits is 6.

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

    Output2
    Enter the Number:456

    Product of digits is 120.

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

    Output3
    Enter the Number:326

    Product of digits is 36.