+ 1
How to make a program for this in C?
Take a number as input. print whether it is divisible by 2,3,5. use only one printf.
35 Antworten
+ 9
I wish it were Python. It'd have been easy
+ 6
Use an if then add a printf
+ 3
printf("%d is divisible by %s\n",n % 2 ? (n % 3 ? (n % 5 ? "neither 5, 3 nor 2" : "5") : "3") : "2"));
+ 3
@Prunier 😂😂
+ 3
Int ar[3]={2,3,5};
Int num; //number
For(i=0;i<=2;i++)
{
If(num%a[i]==0)
Printf("num is divisible by %d", a[i]) ;
Else
Continue;
}
+ 2
well baptsie,ur program has same thing. if I put 30 it just returns 2.
+ 2
kartikey can u type it nd give
I didn't understood u properly
+ 2
I updated the program, see that
+ 2
It is for each loop, the loop will iterate through each element in array.
+ 2
continue skips the next part and jumps to run the next iteration
+ 2
😊
+ 2
#include <string.h>
...
char s[100];
printf("%d is divisible by %s\n",n % 2 && n % 3 && n % 5 ? "neither 5, 3 nor 2" : strcat(strcat(strcat(s, n%2 ? "" : "2 "), n % 3 ? "" : "3 "), n % 5 ? "":"5 "));
But once again, do not do this at home :p
+ 2
At least, only one printf will be called and written :p
+ 2
@Baptiste this code wrks i think😐
+ 2
Oki oki i gt to know
+ 1
I tried. but it doesn't work if no.
like 30. it prints only one of 2,3,5.
+ 1
how to do it?
+ 1
/* let's say the number is n */
if(n % 2){
if(n % 3){
if(n % 5)
printf("%d is neither divisible by 2, 3 nor 5\n",n);
else
printf("%d is divisible by 5\n",n);
}else
printf("%d is divisible by 3\n",n);
}else
printf("%d is divisible by 2\n",n);
Other solution :
int divisible = 0;
if(!(n%5)){
printf("%d is divisible by 5\n",n);
divisible = 1;
}
if(!(n%3)){
printf("%d is divisible by 3\n",n);
divisible = 1;
}
if(!(n&1)){
printf("%d is divisible by 2\n",n);
divisible = 1;
}
if(!divisible)
printf("%d is neither divisible by 2, 3 nor 5\n",n);
+ 1
I said only 1 printf