GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Dec-2008, 15:04
esma esma is offline
New Member
 
Join Date: Dec 2008
Posts: 1
esma is on a distinguished road

Write a program that inputs one 3-digit number....


Write a program that inputs one 3-digit number (example: 854), seperates the number into its individual digits (hundreds, tens and ones) and prints each digit as below. (Hint: use combinations of integer division and the remainder operation).

İm tryn but i dont know my fault may you help me
CPP / C++ / C Code:
#include<stdio.h>
int main()
{
     printf("enter");
     int x,y,z,num=x*100+y*10+z;
     scanf("%d",&num);
     printf("hundreds %d\ntens %d\n ones %d",x,y,z);
     getchar();
     getchar();
     return 0;
     }
it doest work like instructure's want
Last edited by LuciWiz : 09-Dec-2008 at 15:07. Reason: Please insert your C code between [cpp] & [/cpp] tags
  #2  
Old 09-Dec-2008, 20:25
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Write a program that inputs one 3-digit number....


That's because int num=x*100+y*10+z; doesn't do anything.

You have to split the number after you read it. Look into the % operator, as in:
val = num % 5
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
  #3  
Old 19-Feb-2010, 04:15
murugaperumal murugaperumal is offline
New Member
 
Join Date: Feb 2010
Posts: 15
murugaperumal has a little shameless behaviour in the past

Re: Write a program that inputs one 3-digit number....


Dear Friend


If you want to display the number into its individual digits (hundreds, tens and ones), you can use the following coding also. But it is working for only three digits.

CPP / C++ / C Code:
#include<stdio.h>
int main()
{
     printf("enter");
     int x,y,z,num=x*100+y*10+z;
     scanf("%d",&num);
     int val=num/100;
     int mod_val=num%100;
     int val1=mod_val/10;
     int mod_val2=mod_val%10;
     printf("hundreds %d\ntens %d\n ones %d\n",val,val1,mod_val2);
     return 0;
}

Last edited by LuciWiz : 19-Feb-2010 at 07:16. Reason: Please insert your C code between [cpp] & [/cpp] tags
 
 

Recent GIDBlogProblems with the Navy (Officers) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Equation solver RazoR C Programming Language 3 18-May-2008 09:24
malloc/free usage, and general good programming behavior lavaka C Programming Language 7 14-Jun-2007 19:50
floating point decimal to ascii conversion crazypal C Programming Language 5 18-Apr-2007 04:59
Dice rolling program (random number generator) crystalattice Python Forum 1 17-Apr-2006 17:34
Apache2 config issues monev Apache Web Server Forum 2 28-Jun-2004 06:19

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:33.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.