program should print the last two digit of any integer

program should print the last two digit of any integer

Page 1 of 1

10 Replies - 6583 Views - Last Post: 08 January 2011 - 01:37 PM Rate Topic: -----

#1 Guest_a*


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;
cout<<"enter your integer :\n";
cin>>x;
for(i=0;i<x;i++)
{
y=x%2;
x=x/2;
z=x;
}
cout<<z;
}





but it is wrong
:nottalkingtoyou:
:dontgetit:

Is This A Good Question/Topic? 0

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

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4280
  • View blog
  • Posts: 13,443
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • 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
Was This Post Helpful? 0
  • +
  • -

#4 Guest_a*


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?
Was This Post Helpful? 0

#5 Guest_a*


Reputation:

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

Posted 08 January 2011 - 09:46 AM

View Posthorace, 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?
Was This Post Helpful? 0

#6 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

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

Posted 08 January 2011 - 10:51 AM

View Posta, on 08 January 2011 - 03:46 PM, said:

View Posthorace, 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
Was This Post Helpful? 0
  • +
  • -

#7 Guest_a*


Reputation:

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

Posted 08 January 2011 - 11:38 AM

okay thank you sir
Was This Post Helpful? 0

#8 lepinat0r  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • 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 :D

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

Was This Post Helpful? 0
  • +
  • -

#9 Guest_a*


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 :D
Was This Post Helpful? 0

#10 Guest_a*


Reputation:

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

Posted 08 January 2011 - 12:26 PM

this is the right code :D

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;
}
cout<<"your last two integers :\n"<<y<<z;



thnx all
Was This Post Helpful? 0

#11 lepinat0r  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 21-December 10

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

Posted 08 January 2011 - 01:37 PM

View Posta, on 08 January 2011 - 11:26 AM, said:

this is the right code :D

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;
}
cout<<"your last two integers :\n"<<y<<z;



thnx all


I like strings :D, 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;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1