xisto Community

What Is The Purpose Of The Percentage Symbol In C?

Recommended Posts

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

Share on other sites

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.

Share on other sites

So, if num2 was 24, it would give me 5 with a remainder of 0?

Share on other sites

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.

Share on other sites

Ah, ok. Thank you very much for your help.

Share on other sites

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

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

Share on other sites

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

Share on other sites

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.

Share on other sites

Alright, thank you.

Share on other sites

You can also try those numbers on google, but I'm not sure it will return the same results as it would in a program.Just enter what you want to calculate onto the search bar and press search.Google has its built in calculator.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account