In this post, we will learn how to print numbers divisible by 3 and 5 using C Programming language.
A number is divisible by 3 if the sum of its digits is also divisible by 3. For example: 153 is divisible by 3 because 1 + 5 + 3 = 9. 9 is divisible by 3 so, 153 is divisible by 3.
A number is divisible by 5 if it’s unit place is 0 or 5. For example: 150, 275, and 325 etc.
We will be using and(&&) operator to print numbers divisible by both 3 and 5.
So, without further ado, let’s begin this tutorial.
C Program To Print Numbers Divisible by 3 and 5
#include<stdio.h> int main(){ int i, num; //Asking for input printf("Enter the last number: "); scanf("%d", &num); printf("Numbers Divisible by 3 and 5 Between 0 to %d are: \n", num); for (i = 1; i <= num; i++){ if (i % 3 == 0 && i % 5 == 0){ printf("%d ", i); } } return 0; }
Output
Enter the last number: 100
Numbers Divisible by 3 and 5 Between 0 to 100 are:
15 30 45 60 75 90
How Does This Program Work ?
int i, num;
In this program, we have declared two int data type variables named i and num.
//Asking for input printf("Enter the last number: "); scanf("%d", &num);
Then, the user is asked to enter the last value upto which the user wants to find the divisible numbers.
printf("Numbers Divisible by 3 and 5 Between 0 to %d are: \n", num); for (i = 1; i <= num; i++){ if (i % 3 == 0 && i % 5 == 0){ printf("%d ", i); } }
Now, we find the numbers which are exactly divisible by both 3 and 5 using for loop and && operator.
Conclusion
I hope after going through this post, you understand how to print numbers divisible by 3 and 5 using C Programming language.
If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to help you.
Also Read: