CS18
Eight bits in a byte.
Signed value:
Unsigned value:
#include <iomanip.h> #include <stdlib.h> int gcd(int a, int b); int main(void) { int i = 7; int j = 9; cout << "gcd: " << gcd(i, j); return 0; } int gcd(int a, int b) { int r = a % b; int ans; if (r == 0) ans = b; else ans = gcd(b, r); return ans; }
while ( <for condition> )
.
{
.
}
.
int main() { int a, b, c; ... sum(a, b, c); // leave sum of b and c in a } // returns sum of a and b through sum void sum (int sum, int a, int b) { sum = a + b; }
int main() { int a, b, c; ... sum(&a, b, c); // leave sum of b and c in a } // returns sum of a and b through sum void sum (int* sum, int a, int b) { *sum = a + b; }
int sum(int array[], int start, int end) { int mid = (start + end) / 2; if (start == end) return array[start]; else return sum(array, start, mid) + sum(array, mid + 1, end); }
You have entered seventy eight.Use switch statements in your program with a minimum of cases, not one switch statement with 80 cases.
#include <iostream.h> int main() { int n = 0; int tens; int ones; while (n < 20 || n > 99) { cout << "Enter a number in the range 20..99: "; cin >> n; } ones = n % 10; tens = n / 10; cout << "You have entered "; switch (tens) { case 2: cout << "twenty "; break; case 3: cout << "thirty "; break; case 4: cout << "forty "; break; case 5: cout << "fifty "; break; case 6: cout << "sixty "; break; case 7: cout << "seventy "; break; case 8: cout << "eighty "; break; case 9: cout << "ninety "; break; } switch (ones) { case 0: cout << ".\n"; break; case 1: cout << "one.\n"; break; case 2: cout << "two.\n"; break; case 3: cout << "three.\n"; break; case 4: cout << "four.\n"; break; case 5: cout << "five.\n"; break; case 6: cout << "six.\n"; break; case 7: cout << "seven.\n"; break; case 8: cout << "eight.\n"; break; case 9: cout << "nine.\n"; break; } return 0; }