The number divisible by 3 then print hello if number divisible by 5 then print world if 3 and 5 are divisible then print hello world
Answers
Answered by
1
Answer:
Follwoing C program will perform the desired function:
#include<stdio.h>
int main()
{
int num;
printf("Enter any number\n");
scanf("%d",&num);
if(num % 15 == 0)
{
printf("hello world");
}
else if(num % 3 == 0)
{
printf("hello");
}
else if(num % 5 == 0)
{
printf("world");
}
return 0;
}
Explanation:
There are three important things that we keep in our minds while writing the code for this program.
- First, if the number is divisible by 3 then we print "hello".
- For doing this, we check the divisibility of numbers by using the remainder operator.
- Second thing, if the number is divisible by 5 then we print "world".
- For doing this, we check the divisibility of numbers by using the remainder operator.
- The third and the most important thing is when we check the divisibility by both 3 and 5. Since the LCM of 3 and 5 is 15.
- So if a number is divisible by 15 then it will surely be divisible by 3 and 5 both.
- That is why we have written the first condition with %15 and if it is true then we print "hello world".
#SPJ3
Similar questions