C++ tutorial, C++ programs, C++ Exercises, C++ Example Programs

Saturday, March 24, 2018

C++ Program To Find Product of Digits of Given Number

Q: Input an integer number. Find and show the product / multiplication of digits of given number. That is we have to multiply digits of number.

      For example, if given number is 123 then answer will be Product of digits = 6
      because 1 x 2 x 3 = 6.
C++ Program to calculate product of digits of a number
C++ Program to calculate product of digits of a number


The Actual C++ Code of C++ Program Product of Digits of a given number

/*
Program to input a number, then display poduct of its digits.
(c) Www.EasyCppProgramming.Blogspot.Com
*/

#include<iostream.h>
#include<conio.h>

void main()

{

     int num, temp, product=1, r, q;

     clrscr();

     cout<<"Enter a number to multiply its Digits=";

     cin>>num;

     temp = num;

     while ( temp>0 )

     {
       q = temp/10;

       r = temp % 10;

       product = product * r;

       temp = q;

     }

     cout<<"\n The product of digits of given number "<<num<<" = "<<product;

     getch();
     }
C++ Program to calculate product of digits of a number output sample run
C++ Program to calculate product of digits of a number

The Working of  C++ Code of product of Digits of a given number Program

This program is of reverse number logic.

1. We will divide 123 by 10 and get quotient=12, r = 3
2. multiply r=3 with product which is one so product=3

3. Now put q =12 into dividend number
4. so divide 12 by 10, quotient=1 and remainder r=2.
5. Multiply r=2 with product which is 3 so product = 6.
6. now put q=1 into dividend number

7. Now divide 1 by 10, quotient=0 and r=1.
8. so multiply r=1 with product which is 6 therefore product becomes 6 x 1 = 6
9. now put quotient=0 into dividend number called temp variable
10. so that while loop test condition while (temp>0) becomes false and loop terminates.

11. Therefore display the answer product of digits of number 123 = 6
/*
Program to input a number, then display product of its digits.
(c) Www.EasyCppProgramming.Blogspot.Com
*/
#include<iostream.h>     // include header files
#include<conio.h>

void main()
{
     int num, temp, product=1, r, q;
     clrscr();
     cout<<"Enter a number to multiply its Digits=";
     cin>>num;                 // input number
     temp = num;             // copy number into temp variable
     while ( temp>0 )      // loop while temp is greater than zero
     {
       q = temp/10;      // calculate quotient = temp number / 10, if num is 123 then q=12
       r = temp % 10;  //  calculate remainder r = 123 % 10, r = 3
       product = produt * r;  // product = 1* 3; so last digit 3 of 123 is multiplied,
                                                                                       //product=3
                               
       temp = q;      // set next time dividend     temp=12
     }
     cout<<"\n The product of digits of given number "<<num<<"  =  "<<product;
                                                                                                      //product=6
     getch();
     }
Share:

0 comments:

Post a Comment

We Love To Hear From You!

EasyCPPprogramming.blogspotcom

Labels