C++ program to find the sum of digits, reverse & prime or not of a number
Aim: To write a program to find the reverse and sum of digits of a number. Also, check whether it is prime or not.
Algorithm:
Step 1: Start.
Step 2: Declare a class number.
Step 3: Define and declare the function getdata() to read the number.
Step 4: Define and declare the function prime() to print whether the number is prime or not.
Step 5: Define and declare the function putdata() to print the sum of digits and reverse of the number.
Step 6: Create an object for the class number.
Step 7: Call the functions getdata(), prime(), and putdata() for the object.
Step 8: Stop.
Program Code:
#include<iostream.h> #include<conio.h> class number{ int n,d,s,r,i,f; public: void getdata(); void prime(); void putdata(); };void number::getdata() { cout<<"Enter the number : "; cin>>n; } void number::prime() { f=0; for(i=2;i<=n/2;i++) { if(n%i==0) { f=1; break; } } if(f==1) cout<<"\nNumber is not prime\n"; else cout<<"\nNumber is prime\n"; } void number::putdata() { s=0; r=0; while(n>0) { d=n%10; s=s+d; r=r*10+d; n=n/10; } cout<<"\nSum of digit = "<<s; cout<<"\nReverse of the number = "<<r; } void main() { number obj; clrscr(); obj.getdata(); obj.prime(); obj.putdata(); getch(); }
Output:
Enter the number : 736
Number is not prime
Sum of digits = 16
Reverse of the number = 637