Reversing a number in c++
Posted
by
Marcel Bujnowski
on Stack Overflow
See other posts from Stack Overflow
or by Marcel Bujnowski
Published on 2013-10-19T03:46:06Z
Indexed on
2013/10/19
3:53 UTC
Read the original article
Hit count: 117
c++
I created a program to show the sum and show the reversed number a person has typed. The sum function works but the revers function is not. Can anyone give me any tips on how to fix it. I created a program to show the sum and show the reversed number a person has typed. The sum function works but the revers function is not. Can anyone give me any tips on how to fix it.
#include<iostream>
#include<iomanip>
using namespace std;
void printSum(int n, bool reverse);
int sm(int n);
int reverseInt(int n);
void printAddTable(int n);
int main()
{
int reverse;
int sum=0;
int n;
cout<<"Enter N value:"<<endl;
cin>>n;
if(n>0)
{
reverse = true;
printSum( n, reverse); // calls the printSum Method
}
else
{
//cout<<"enter positive Number only:"<<endl;
}
sum = sm(n); //err // calls the sum Method
reverse = reverseInt(n); // calls the reverseInt Method
cout<<"The reverse value is:"<<reverse;
printAddTable(n); // calls the printAddTable Method
//getch()
}
//end of main()
void printSum(int n, bool reverse)
{
int sum=0;
// print sum of reverse numbers
for(int i=n; i>=1; i--)
{
sum=sum+i;
cout<<i<< " "<<"+"<<" ";
}
cout<<sum;
}
int sm(int n)
{int sum =0;
for(int i=1; i<=n; i++)
{
sum = sum + i ;
cout << endl;
cout<<i<<" "<<"+"<<" "<<endl; // print n positive integers
cout << endl;
}
cout<< "Are " <<n<< " positive integers"<<endl;
cout<< "Sum is "<<sum <<endl;
return sum;
}
int reverseInt(int n)
{
int reminder=0;
int sum =0;
while(n<=0)
{
reminder = n/10;
sum = (sum * 10) + reminder; // it returns the reverse number
n = n % 10;
}
return sum;
}
void printAddTable(int n)
{
for(int i=1; i<=n; i++)
{
cout<<i<<endl;
for(int j=1; j<=n; j++) // print n X n add table
{
cout<<i+j<<endl;
}
}
}
{
© Stack Overflow or respective owner