+ 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.

3rd Sep 2017, 8:32 AM
Shantanu Shinde
Shantanu Shinde - avatar
35 Antworten
3rd Sep 2017, 9:33 AM
Kartikey Sahu
Kartikey Sahu - avatar
+ 9
I wish it were Python. It'd have been easy
3rd Sep 2017, 9:02 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 6
Use an if then add a printf
3rd Sep 2017, 8:40 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 3
printf("%d is divisible by %s\n",n % 2 ? (n % 3 ? (n % 5 ? "neither 5, 3 nor 2" : "5") : "3") : "2"));
3rd Sep 2017, 9:02 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 3
@Prunier 😂😂
3rd Sep 2017, 9:48 AM
Kartikey Sahu
Kartikey Sahu - avatar
+ 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; }
3rd Sep 2017, 9:52 AM
Nikhil Gowda
Nikhil Gowda - avatar
+ 2
well baptsie,ur program has same thing. if I put 30 it just returns 2.
3rd Sep 2017, 9:24 AM
Shantanu Shinde
Shantanu Shinde - avatar
+ 2
kartikey can u type it nd give I didn't understood u properly
3rd Sep 2017, 9:25 AM
Shantanu Shinde
Shantanu Shinde - avatar
+ 2
I updated the program, see that
3rd Sep 2017, 9:39 AM
Kartikey Sahu
Kartikey Sahu - avatar
+ 2
It is for each loop, the loop will iterate through each element in array.
3rd Sep 2017, 9:40 AM
Kartikey Sahu
Kartikey Sahu - avatar
+ 2
continue skips the next part and jumps to run the next iteration
3rd Sep 2017, 9:43 AM
Kartikey Sahu
Kartikey Sahu - avatar
+ 2
😊
3rd Sep 2017, 9:45 AM
Kartikey Sahu
Kartikey Sahu - avatar
+ 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
3rd Sep 2017, 9:47 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 2
At least, only one printf will be called and written :p
3rd Sep 2017, 9:49 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 2
@Baptiste this code wrks i think😐
3rd Sep 2017, 10:46 AM
Nikhil Gowda
Nikhil Gowda - avatar
+ 2
Oki oki i gt to know
3rd Sep 2017, 10:49 AM
Nikhil Gowda
Nikhil Gowda - avatar
+ 1
I tried. but it doesn't work if no. like 30. it prints only one of 2,3,5.
3rd Sep 2017, 8:42 AM
Shantanu Shinde
Shantanu Shinde - avatar
+ 1
how to do it?
3rd Sep 2017, 8:47 AM
Shantanu Shinde
Shantanu Shinde - avatar
+ 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);
3rd Sep 2017, 8:56 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 1
I said only 1 printf
3rd Sep 2017, 8:57 AM
Shantanu Shinde
Shantanu Shinde - avatar