Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> What Is The Purpose Of The Percentage Symbol In C?
htdefiant
post Dec 14 2007, 11:11 PM
Post #1


Advanced Member
*******

Group: Members
Posts: 126
Joined: 21-February 06
Member No.: 18,973



What does the % symbol do?
For example, if num1 has the value of 120 and num2 has the value 2, what will num1%num2 generate?
Thanks
Go to the top of the page
 
+Quote Post
bhavesh
post Dec 15 2007, 08:54 AM
Post #2


Privileged Member
*********

Group: [HOSTED]
Posts: 537
Joined: 21-August 06
From: Ziya's Heart
Member No.: 28,693



It is called the mod operator and gives remainder.

That is num1%num2 will divide num1 from num2 and will display the remainder.
In your example it will return 0, as 120 is completely divisible by 2.
Go to the top of the page
 
+Quote Post
htdefiant
post Dec 15 2007, 03:28 PM
Post #3


Advanced Member
*******

Group: Members
Posts: 126
Joined: 21-February 06
Member No.: 18,973



So, if num2 was 24, it would give me 5 with a remainder of 0?
Go to the top of the page
 
+Quote Post
truefusion
post Dec 15 2007, 04:16 PM
Post #4


Ephesians 6:10-17
Group Icon

Group: [MODERATOR]
Posts: 1,867
Joined: 22-June 05
From: The World of Gentoo
Member No.: 8,528
T17 GFX Crew



QUOTE(htdefiant @ Dec 15 2007, 10:28 AM) *
So, if num2 was 24, it would give me 5 with a remainder of 0?

It wouldn't, or i should say, shouldn't give you the 5. It's purpose is to return the remainder—nothing else. You see this in PHP, too, since PHP is based on C, as well as other programming languages.
Go to the top of the page
 
+Quote Post
htdefiant
post Dec 15 2007, 04:36 PM
Post #5


Advanced Member
*******

Group: Members
Posts: 126
Joined: 21-February 06
Member No.: 18,973



Ah, ok. Thank you very much for your help.
Go to the top of the page
 
+Quote Post
bhavesh
post Dec 15 2007, 04:48 PM
Post #6


Privileged Member
*********

Group: [HOSTED]
Posts: 537
Joined: 21-August 06
From: Ziya's Heart
Member No.: 28,693



I think you are not getting it. Try this program in C

CODE
main()
{
int num1,num2;
printf("Enter num1 : ");
scanf("%d",&num1);
printf("Enter num2 : ");
scanf("%d",&num2);
printf("num1%num2   is  %d", num1%num2);
}


Go to the top of the page
 
+Quote Post
htdefiant
post Dec 15 2007, 04:55 PM
Post #7


Advanced Member
*******

Group: Members
Posts: 126
Joined: 21-February 06
Member No.: 18,973



That program crashes my DOS window for some reason. But I think I understand - if I put in 120 and 24, the output would be 0? Thanks for your patience
Go to the top of the page
 
+Quote Post
rvalkass
post Dec 15 2007, 04:59 PM
Post #8


apt-get moo
Group Icon

Group: [MODERATOR]
Posts: 2,055
Joined: 28-May 05
From: Hertfordshire, England
Member No.: 7,593
Spam Patrol



Yes, as 120 divided by 24 is exactly 5. If you used 50 and 7 then the output would be 1. If you used 99 and 4 then the output would be 3.
Go to the top of the page
 
+Quote Post
htdefiant
post Dec 15 2007, 05:01 PM
Post #9


Advanced Member
*******

Group: Members
Posts: 126
Joined: 21-February 06
Member No.: 18,973



Alright, thank you.
Go to the top of the page
 
+Quote Post
Csshih
post Dec 17 2007, 05:14 PM