C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 26th, 2012, 04:24 AM
kathyrollo kathyrollo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Location: Metro Manila, Philippines
Posts: 45 kathyrollo User rank is Corporal (100 - 500 Reputation Level)kathyrollo User rank is Corporal (100 - 500 Reputation Level)kathyrollo User rank is Corporal (100 - 500 Reputation Level)kathyrollo User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 18 h 57 m 8 sec
Reputation Power: 4
Question [While Loop] Program to Reverse the Digits of a Number

Code:
// Program to reverse the digits of a number

#include <stdio.h>

int main (void)
{
  int iNum, iRight;
  
  printf ("Enter your number.\n");
  scanf ("%i", &iNum);
  
  while (iNum != 0)
  {
  	iRight = iNum % 10;
  	printf ("%i", iRight);
  	iNum /= 10;
  }
  
  printf ("\n");
  
  return 0;
}

Hi guys,

I'm mostly just beginning C and self-studying with S. Kochan's book, Programming in C (3rd Edition).

I understand until where the code extracts and prints the rightmost digit but I seem confused as to the purpose of this line:

iNum /= 10;

I do know it means iNum = iNum / 10; but I can't quite grasp how it works in the algorithm or "why" it should be there.

Thanks in advance.

Reply With Quote
  #2  
Old December 26th, 2012, 04:31 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,831 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 12 h 51 m 23 sec
Reputation Power: 1774
> but I can't quite grasp how it works in the algorithm or "why" it should be there.
You know you can try
Code:
  while (iNum != 0)
  {
  	iRight = iNum % 10;
  	printf ("%i", iRight);
  	/* commented out iNum /= 10; */
  }


or even
Code:
  while (iNum != 0)
  {
  	iRight = iNum % 10;
  	printf ("%i", iRight);
  	iNum /= 5;
  }

It should help you figure out
- why there is a division
- why the divisor has to be 10
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
  #3  
Old December 26th, 2012, 05:32 AM
kathyrollo kathyrollo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Location: Metro Manila, Philippines
Posts: 45 kathyrollo User rank is Corporal (100 - 500 Reputation Level)kathyrollo User rank is Corporal (100 - 500 Reputation Level)kathyrollo User rank is Corporal (100 - 500 Reputation Level)kathyrollo User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 18 h 57 m 8 sec
Reputation Power: 4
>> why there is a division
Program enters an infinite loop of the last digit.

>> why the divisor has to be 10
Let's see, here's a try of what I think happens.

Say I enter 5678. This part of the code...

iRight = iNum % 10;
printf ("%i", iRight);

...gives a remainder of 8 and prints it first.

iNum /= 10;
  • Divides the user-inputted iNum value, 5678 by 10 resulting to the integer value of 567 which is now the new value of iNum.
  • Since 567 is not equal to 0, the program executes the body of the while loop using the new value 567 giving a remainder of 7 and prints it as the next digit.
  • 567 is divided by 10, resulting to 56, which still holds true for the condition, remainder 6, prints.
  • 56 divided by 10 gives a new value for iNum which is now 5.
  • 5 % 10 is 5, prints. Since 5/10 is 0 by rules of integer division, the loop terminates.
-----
Thank you for the tips, salem. Is my understanding somehow near? >.<
Comments on this post
salem agrees: Looks good to me

Reply With Quote
  #4  
Old December 26th, 2012, 06:12 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,831 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 12 h 51 m 23 sec
Reputation Power: 1774
Looks good to me

Reply With Quote
  #5  
Old December 26th, 2012, 06:21 AM
kathyrollo kathyrollo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Location: Metro Manila, Philippines
Posts: 45 kathyrollo User rank is Corporal (100 - 500 Reputation Level)kathyrollo User rank is Corporal (100 - 500 Reputation Level)kathyrollo User rank is Corporal (100 - 500 Reputation Level)kathyrollo User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 18 h 57 m 8 sec
Reputation Power: 4
Thank you for pointing me to the right direction.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > [While Loop] Program to Reverse the Digits of a Number

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap