90 likes | 315 Views
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;
E N D
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) printf("%d is divisible by %d\n", num, div); • else printf("%d is not divisible by %d\n", num, div); • } • return(0);
Pick a number between 1 and 5 4,5 1,2,3 1,2
Pick a number between 1 and 5 #include <stdio.h> int main(void) { intansw; int num; printf("Please pick a number between 1-5\n"); 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 %d\n", num); return(0); }
Find smallest of 3 numbers a,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 %d\n", 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 %d\n", 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 %d\n", 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 %d\n", min); return(0); }