Page 1 of 1

Reputation:

# program should print the last two digit of any integer

Posted 08 January 2011 - 08:55 AM

hello

i have a program should print the last two digit of any integer like : x=1235

the o/p is 53

i tried this

```
#include<iostream.h>
main()
{
int x,y,w,i,j,z;
cin>>x;
for(i=0;i<x;i++)
{
y=x%2;
x=x/2;
z=x;
}
cout<<z;
}

```

but it is wrong

Is This A Good Question/Topic? 0

## Replies To: program should print the last two digit of any integer

### #2 jimblumberg

Reputation: 5466
• Posts: 17,017
• Joined: 25-December 09

## Re: program should print the last two digit of any integer

Posted 08 January 2011 - 09:11 AM

Quote

but it is wrong

What is wrong?

Does the program compile without errors/warnings?

Does the program produce output?

If it is producing output what is the output?

What did you expect the program to produce?

What did you input into the program?

Jim

This post has been edited by jimblumberg: 08 January 2011 - 09:12 AM

### #3 horace

• D.I.C Lover

Reputation: 768
• Posts: 3,832
• Joined: 25-October 06

## Re: program should print the last two digit of any integer

Posted 08 January 2011 - 09:12 AM

not sure what you are trying to do
if you want the last two digits of an integer you could do
```z=x%100;

```

if you enter 1253 you get 53
do you want them in reverse order ? should be fairly simple to swop the digits

Reputation:

## Re: program should print the last two digit of any integer

Posted 08 January 2011 - 09:41 AM

horace

thank you very much that solved my problem

i have another program that should solve this series

1^2/2+3^4/4.....n^n+1/n+1

```{cout<<"enter your series value\n";
cin>>n;
for(int i=1;i<=n;i+=2)
{
p=1;
for(int j=1;j<=(i+1);j++)
p*=n;
s+=p/n+1;
}
cout<<s;

```

can you check it for me plz is it right?

Reputation:

## Re: program should print the last two digit of any integer

Posted 08 January 2011 - 09:46 AM

horace, on 08 January 2011 - 08:12 AM, said:

not sure what you are trying to do
if you want the last two digits of an integer you could do
```z=x%100;

```

if you enter 1253 you get 53
do you want them in reverse order ? should be fairly simple to swop the digits

yes please how to swap it?

### #6 horace

• D.I.C Lover

Reputation: 768
• Posts: 3,832
• Joined: 25-October 06

## Re: program should print the last two digit of any integer

Posted 08 January 2011 - 10:51 AM

a, on 08 January 2011 - 03:46 PM, said:

horace, on 08 January 2011 - 08:12 AM, said:

not sure what you are trying to do
if you want the last two digits of an integer you could do
```z=x%100;

```

if you enter 1253 you get 53
do you want them in reverse order ? should be fairly simple to swop the digits

yes please how to swap it?

try using %, * and / operators

Reputation:

## Re: program should print the last two digit of any integer

Posted 08 January 2011 - 11:38 AM

okay thank you sir

### #8 lepinat0r

Reputation: 1
• Posts: 40
• Joined: 21-December 10

## Re: program should print the last two digit of any integer

Posted 08 January 2011 - 11:47 AM

Not sure if you know or not, but % = modulus.

What it does is take the firstNumber/secondNumber and returns the remainder.
```x = 1253
x%100 = 53 // means that 100 goes into x 12 times leaving 53 left over.
```

edit: Just posted this to make sure you knew what was going on and didn't copy the code blindly and leave without learning something

This post has been edited by lepinat0r: 08 January 2011 - 11:48 AM

Reputation:

## Re: program should print the last two digit of any integer

Posted 08 January 2011 - 12:18 PM

lepinat0r

yes i know that thank very much

Reputation:

## Re: program should print the last two digit of any integer

Posted 08 January 2011 - 12:26 PM

this is the right code

i am soooooo happyyyyyyyy
```cout<<"enter your integer :\n";
cin>>x;
for(i=0;i<x;i++)
{
z=x%100;
y=z%10;
z=z/10;
}

```

thnx all

### #11 lepinat0r

Reputation: 1
• Posts: 40
• Joined: 21-December 10

## Re: program should print the last two digit of any integer

Posted 08 January 2011 - 01:37 PM

a, on 08 January 2011 - 11:26 AM, said:

this is the right code

i am soooooo happyyyyyyyy
```cout<<"enter your integer :\n";
cin>>x;
for(i=0;i<x;i++)
{
z=x%100;
y=z%10;
z=z/10;
}

```

thnx all

I like strings , so you could also do this with them.

-- Precondition: Number is >= 10

```#include <iostream>
#include <string>

using namespace std;

int main() {

// Variables
string userInput;
int len;
string secondToLast;
string last;

// Gets user input
cout << "Please enter an integer: ";
cin >> userInput;

// Sets length of string
len = userInput.length();

// User substrings to fidn the last 2 numbers
secondToLast = userInput.substr(len - 2, 1);	// (start position, length of string)
last =      userInput.substr(len - 1, 1);		// (start position, length of string)

// Output
cout << "The second to last number is: " << secondToLast << "." << endl;
cout << "The last number is:           " << last << "." << endl;

return 0;
}

```