reverse ,add,no of digits ,with c

nahsorhseda

Member
Messages
116
Reaction score
0
Points
16
consider i have a number...
int a=3453;
i want that no. to be reversed ie:3543
i want the digits to be added ie:3+4+5+3=15
and i want to count the no. of digits in th given no. ie .there are 4 digits in this no.


please dont use any inbuilt functions to do this,u may use loops and if statements.....!

any help would be appreciated
 

nahsorhseda

Member
Messages
116
Reaction score
0
Points
16
you guessed it right
ive been banging my head on the keyboard for several hrs,searched the net,but couldnt find any help
so if one can pls do so
 

eminemix

Member
Messages
350
Reaction score
0
Points
16
This should work

Code:
#include <iostream.h>
void main(){
long i,k=0,s=0,nr=0;
cin>>i;
while(i>=9)
    {
    k+=i%10;
    i=i/10;
    k=k*10;
    }
    if(i!=0)
    k=k+i;
cout<<"Reversed number is "<<k<<endl;
    while(k!=0)
          {
                s = s+k%10;
                k = k/10;
                nr++;
          }
cout<<"The sum is "<<s<<endl;
cout<<"There are "<<nr<<" digits";
}

I could do a php version as well if it helps.
 

Attachments

  • solution.txt
    318 bytes · Views: 47
Last edited:

nahsorhseda

Member
Messages
116
Reaction score
0
Points
16
thanks dude the code is great and works ,but have no idea about "cout" or "cin"
could u explain in simple "printf" and what is "long"?
 

MasterMax1313

New Member
Messages
84
Reaction score
0
Points
0
cout and cin are console out and console in, members of the iostream.h header file. long is a long integer, bigger than int. though it you wanted to, using his code, you could probably use short, int, long, unsigned char. when you start getting into floating point numbers (float and double), his code ceases to work for the decimal, though it wouldn't be hard to adjust for that.
 

eminemix

Member
Messages
350
Reaction score
0
Points
16
Oh, ok i will use printf and scanf ;)
Like MasterMax said, long is a big integer.

The new code
Code:
#include <iostream.h>
#include <stdio.h>
void main(){
long i,k=0,s=0,nr=0;
cin>>i;
while(i>=9)
	 {
	 k+=i%10;
	 i=i/10;
	 k=k*10;
	 }
	 if(i!=0)
	 k=k+i;
printf("Reversed number is %ld \n", k);
	 while(k!=0)
			 {
					 s = s+k%10;
					 k = k/10;
					 nr++;
			 }
printf("The sum is %d \n", s);
printf("There are %d digits", nr);
}

Hope this helps.
 

Attachments

  • new_version.txt
    355 bytes · Views: 40

woiwky

New Member
Messages
390
Reaction score
0
Points
0
Shouldn't it be while(i > 9) instead of (i >=9)? Else, for 9 or any number that would be reversed to end in 9, k would be multiplied by 10 when it shouldn't be and have an extra 0 on the end. On top of that, since you're not using unsigned longs, you might as well make it while((i > 9) || (i < -9)) so it can work with negative numbers as well. Also, I believe if you enter 0, the number of digits would never increment and it would incorrectly state that the number has 0 digits. This could be fixed by making the loop a do-while, or placing this before the loop: nr = (k ? 0 : nr + 1);
And out of curiosity, why don't you use the *=, /=, etc, shortcuts?

With those changes, it'd look something like this:

Code:
void main(){
    long i,k=0,s=0,nr=0;
    cin>>i;
    while((i > 9) || (i < -9)) {
        k += i%10;
        i /= 10;
        k *= 10;
    }
    if(i) {
        k += i;
    }
    printf("Reversed number is %ld \n", k);
    /*
    do {
        s += k%10;
        k /= 10;
        nr++;
    }
    while(k);
    */
    nr = (k ? 0 : nr + 1);
    while(k) {
        s += k%10;
        k /= 10;
        nr++;
    }
    printf("The sum is %d \n", s);
    printf("There are %d digits", nr);
}

I left the do-while commented out in case you prefer that.
 

nahsorhseda

Member
Messages
116
Reaction score
0
Points
16
iam using turbo c++ ,it says one cannot use iostream.h for .c files
is there another way out
Edit:
also tell me where i can search for solutions for such complex math relted problems ,google is no good for this
if you can enlist some websites?
 
Last edited:

Faceman

New Member
Messages
1
Reaction score
0
Points
0
iam using turbo c++ ,it says one cannot use iostream.h for .c files
is there another way out
Edit:
also tell me where i can search for solutions for such complex math relted problems ,google is no good for this
if you can enlist some websites?

Just remove the #include <iostream.h> and it should work..
 

sumitmehta

New Member
Messages
215
Reaction score
1
Points
0
Just try this. I am sorry but the program is in shell script but you will be able to understand it as it is fairly simple.

Code:
#!/bin/bash
#reversing a number through mathematical process

echo "Enter a number"
read num
n=$num
rev=0
s=0;

while test $num -ne 0
do
    r=`expr $num % 10`
    rev=`expr $rev \* 10 + $r`
    s=`expr $s + $r`
    num=`expr $num / 10`
    
done

echo "Reverse of $n is $rev"
echo "Sum of digits is $s"
 

nahsorhseda

Member
Messages
116
Reaction score
0
Points
16
oh thanks guys i understood how to get the reverse frm the first response itself
but can anyone please enlist some websites which helps me solve such problems
 
Top