Divisibility Find out if a number Numb is
Divisibility • Find out if a number, Numb, is divisible by another number, Div. • Is 432 divisible by 3? • Is 432 divisible by 4? • • 432 / 3 = ? 432 / 4 = ? 432 % 3 = ? 432 % 4 = ?
Divisibility #include <stdio. h> int main(void) { int num, div; printf("Please enter a number and a divider: "); scanf("%d%d", &num, &div); if(num%div == 0) else } return(0); printf("%d is divisible by %dn", num, div); printf("%d is not divisible by %dn", num, div);
Pick a number between 1 and 5 >3 4, 5 1, 2, 3 >4 5 >2 4 3 1, 2 >1 2 1
Pick a number between 1 and 5 #include <stdio. h> int main(void) { int answ; int num; printf("Please pick a number between 1 -5n"); printf("Is the number greater than 3 (1/0): "); scanf("%d", &answ); if(answ == 1) { printf("Is the number greater than 4 (1/0): "); scanf("%d", &answ); if(answ == 1) num=5; else num=4; } else { printf("Is the number greater than 2 (1/0): "); scanf("%d", &answ); if(answ == 1) num=3; else { printf("Is the number greater than 1 (1/0): "); scanf("%d", &answ); if(answ == 1) num=2; else num=1; } } printf("The number you picked is %dn", num); return(0); }
Find smallest of 3 numbers a<b a, c b, c a<c a b<c c b c
Find smallest of 3 numbers #include <stdio. h> int main(void) { int a, b, c, min; printf("Please enter three numbers: "); scanf("%d%d%d", &a, &b, &c); if(a<b) { if(a<c) { min = a; } else { min = c; } } else { if(b < c) { min = b; } else { min = c; } } printf("The smallest of the numbers entered is %dn", min); return(0); }
Find smallest of 3 numbers - redone #include <stdio. h> int main(void) { int a, b, c, min; printf("Please enter three numbers: "); scanf("%d%d%d", &a, &b, &c); if(a<b) { if(a < c) min = a; else min = c; } else { if(b < c) min = b; else min = c; } printf("The smallest of the numbers entered is %dn", min); return(0); }
Find smallest of 3 numbers - simpler #include <stdio. h> int main(void) { int a, b, c, min; printf("Please enter three numbers: "); scanf("%d%d%d", &a, &b, &c); if(a<b) if(a < c) min = a; else min = c; else if(b < c) min = b; else min = c; printf("The smallest of the numbers entered is %dn", min); return(0); }
Find smallest of 3 numbers – even simpler #include <stdio. h> int main(void) { int a, b, c, min; printf("Please enter three numbers: "); scanf("%d%d%d", &a, &b, &c); min = c; if (a<b && a<c) min = a; else if (b<a && b<c) min = b; printf("The smallest of the numbers entered is %dn", min); } return(0);
- Slides: 9