Practice Web Page - http://www.cs.tau.ac.il/~efif/courses/Programming_Fall_04
for (i = 0; i < 10; i = i + 1) printf ("i is %d\n", i); |
#include <stdio.h> int main() { int i, n, fact = 1; printf("Enter a number: "); if (scanf("%d", &n) != 1) { printf("ERROR: failed to rean a number!\n"); return -1; } for (i = 1; i <= n; i++) fact *= i; printf("Factorial of is %d\n", fact); return 0; } |
#include <stdio.h> #define MONDAY 1 #define TUESDAY 2 #define WEDNESDAY 3 #define THURSDAY 4 #define FRIDAY 5 #define SATURDAY 6 #define SUNDAY 7 int main() { int today = MONDAY; if ((today == SATURDAY) || (today == SUNDAY)) { printf("Weekend\n"); } else { printf("Go to work or school\n"); } return 0; } |
#include |
#include <stdio.h> int main() { int i, j; int num; if (scanf("%d", &num) != 1) { printf("ERROR: failed to read a number!\n"); return -1; } for (i = 0; i < num; i++) { for (j = 0; j < num - i - 1; j++) printf(" "); for (j = 0; j < 2 * i + 1; j++) printf("*"); printf("\n"); } return 0; } |
#include |
#include <stdio.h> #include <math.h> int main() { int i, prime, sq; printf("Enetr a number: "); if (scanf("%d", &prime) != 1) { printf("ERROR: failed to read a number!\n"); return -1; } sq = (int) sqrt(prime); for (i = 2 ; i <= sq; i++) { if (prime % i == 0) { printf("%d is not prime (divided by %d)\n", prime, i); break; } } if (i > sq) printf("%d is prime\n", prime); return 0; } |
#include <stdio.h> #include <math.h> int main() { int i, j, sq, last; printf("Enetr a number: "); if (scanf("%d", &last) != 1) { printf("ERROR: failed to read a number!\n"); return -1; } for (i = 2; i <= last; i++) { sq = (int) sqrt(i); for (j = 2 ; j <= sq; j++) if (i % j == 0) break; if (j > sq) printf("the number %d is prime\n", i); } return 0; } |