Q-02: Write a C/C++ program to print the sum and product of digits of an integer.
    

Using - C


#include<stdio.h> void main() { int num, x, sum=0, product=1; printf("Enter an Integer: "); scanf("%d",&num); while(num!=0) { x = num % 10; sum += x; product *= x; num /= 10; } printf("Sum of Digits: %d",sum); printf("\nProduct of Digits: %d",product); }

OUTPUT

Enter an Integer: 356
Sum of Digits: 14
Product of Digits: 90
        

Using - C++


#include<iostream> using namespace std; int main() { int num, x, sum=0, product=1; cout<<"Enter an Integer: "; cin>> num; while(num!=0) { x = num % 10; sum += x; product *= x; num /= 10; } cout<<"Sum of Digits: "<< sum<< endl; cout<<"Product of Digits: "<< product; return 0; }

OUTPUT

Enter an Integer: 254
Sum of Digits: 11
Product of Digits: 40