"The best way to cheer yourself up is to try to cheer somebody else up." Mark Twain

Tuesday, March 2, 2010

Write a c++ program to do the following :

(i) read an integer X.
(ii) form an integer Y by reversing the digits of X and integer S having sum of digits of X.
(iii) Output Y and S.

     (For example , if X is equal to 5076, then Y should be     6705 and S should be 18.)

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

void main()
{
int X, Y = 0, S = 0;
int r = 0;

cout<< "\n Enter an integer :";
cin >> X;

for(int i = X; i>0 ; i = i /10)
{
r = i % 10;

S = S + r; 

Y = Y*10 + r;

}

cout << "\n The reverse of " << X << " is " << Y;
cout<< "\n The Sum of the digits = " << S;

getch();

}

No comments:

Post a Comment

C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.
Now Playing: Ballade Pour Adeline

About Me

My photo
I m an IT lecturer of a college. I love social-work. I want to do something beneficial for society before dying , that can promote our society, to some extent.