Practice Web Page - http://www.cs.tau.ac.il/~efif/courses/Programming_Fall_04
#include <stdio.h> int main() { int num1, num2, dif, div; printf("Enter two positive numbers: "); scanf("%d%d", &num1, &num2); dif = num1 - num2; printf("%d subtracted from %d is %d\n", num2, num1, dif); div = num1 / num2; printf("%d divided by %d is %d\n", num1, num2, div); return 0; } |
#include <stdio.h> int main() { int sum, num; printf("Enter a 2-digit number: "); scanf("%d", &num); sum = num % 10; num = num / 10; sum = sum + num; printf("The digits sum is %d\n", sum); return 0; } |
#include <stdio.h> int main() { int sum, num; printf("Enter a 3-digit number: "); scanf("%d", &num); sum = num % 10; num = num / 10; sum = sum + num % 10; num = num / 10; sum = sum + num % 10; printf("The digits sum is %d\n", sum); return 0; } |
Name | Bytes | Range | |
---|---|---|---|
float | 4 | 3.4E +/- 38 | %f |
double | 8 | 1.7E +/- 308 | %lf |
int | Typically 4 | -231 - 231-1 | %d |
char | 1 | -128 - 127 | %c |
#include <stdio.h> int main() { int miles = 26, yards = 385; double kilometers; kilometers = 1.609 * (miles + yards / 1760.0); /* kilometers = 1.609 * (miles + (double)yards / 1760); */ /* kilometers = 1.609 * (miles + yards / 1760); */ printf("A marathon is %lf kilometers.\n", kilometers); return 0; } |
#include <stdio.h> int main() { int a, b; printf("Enter two Numbers: "); scanf("%d%d", &a, &b); if (a == b) printf("%d==%d\n", a, b); else if (a > b) printf("%d>%d\n", a, b); else printf("%d<%d\n", a, b); return 0; } |
#include <stdio.h> int main() { int i, n, fact = 1; printf("Enter a number: "); scanf("%d", &n); i = 1; while (i <= n) { fact *= i; i++; } printf("The factorial is %d\n", fact); return 0; } |
#include <stdio.h> int main() { int a, b, i, power; printf("Enter 2 numbers: "); scanf("%d%d", &a, &b); i = 1; power = 1; while (i <= b) { power = power * a; i++; } printf("%d to the power of %d is %d\n", a, b, power); return 0; } |
#include <stdio.h> int main() { int num, i, counter, sum; printf("Enter the expected number of integers: "); scanf("%d", &counter); i = 1; sum = 0; while (i <= counter) { printf("Enter a number: "); scanf("%d", &num); sum += num; i++; } printf("The numbers sum is %d\n", sum); return 0; } |