Some answers: Keyboard. Mouse. Microphone. Touchscreen display. Trackball. Joystick. Scanner.
Some answers: Monitor. Printer / plotter. Speakers.
#include <stdio.h> int main () { int i, j, k; double a, b, c; char x, y, z; i = 10; j = 100; k = 5; a = i; b = j; c = k; x = 'a'; y = 'b'; z = 'c'; printf ("%d\n", j / i); printf ("%d\n", i / j); printf ("%d\n", k % 2); printf ("%f\n", c / a); printf ("%f\n", b / c / a); printf ("%f\n", b / (c / a)); if (x >= y && z >= y) printf ("yes\n"); else printf ("no\n"); printf ("%d\n", x < y || y > z); return 0; }
Solution: 10 0 1 0.500000 2.000000 200.000000 no 1
Solution: #include <stdio.h> int main () { int years; printf ("Enter your dog's age: "); scanf ("%d", &years); printf ("Your dog is %d in dog years\n", years * 7); return 0; }
#include <stdio.h> int main () { int i; i = 1; while (i <= 10) { printf ("%d\n", i); i++; } return 0; }Rewrite this code to do the same thing using for instead of while.
Solution: #include <stdio.h> int main () { int i; for (i=1; i<=10; i++) { printf ("%d\n", i); } return 0; }
#include <stdio.h> int main () { int n, i, found_a_factor; printf ("Enter an integer: "); scanf ("%d", &n); found_a_factor = 0; for (i=2; i <= n-1; i++) if (n % i == 0) found_a_factor = 1; if (found_a_factor == 0) printf ("prime\n"); else printf ("not prime\n"); return 0; }This code prompts the user for an integer and prints "prime" if the integer is prime, "not prime" otherwise. Using this code as a starting point, write a program that prints the prime numbers between 2 and 1000. Hint: get rid of the scanf. Hint 2: get rid of the part that says "prime" or "not prime" and replace it something that only prints n if it is prime. Hint 3: you'll need another loop.
Solution: #include <stdio.h> int main () { int n, i, found_a_factor; for (n=2; n<=1000; n++) { found_a_factor = 0; for (i=2; i <= n-1; i++) if (n % i == 0) found_a_factor = 1; if (found_a_factor == 0) printf ("%d\n", n); } return 0; }