Sunteți pe pagina 1din 7

Lab Manual 2

The manual covers: i. Control Structures ii. Conditional Expressions (==, !=, >, <, <=, >=, &&, ||) iii. Selection Statements (if, if-else, switch-case-default) iv. Loop Structures (while, do, for) v. Data File

Exercise 2.1 Write the following program. Compile, link and run it. #include <stdio.h> void main () { int marks; printf("Obtained marks?:"); scanf("%d",&marks); if (marks >= 50) printf("Pass\n"); else printf("Fail. Try again\n"); } Exercise 2.2 Write a program that: i. Gets a number from the user. ii. If the number is greater than 5, the program prints a message that tells the user they typed in a number greater than 5.

Exercise 2.3 Write the following program. Compile, link and run it. #include <stdio.h> void main () {

int marks; printf("Obtained marks?:"); scanf("%d", &marks); if (marks >= 80) printf("Excellent\n"); else if (marks >= 60) printf("Good\n"); else if (marks >= 40) printf("Pass\n"); else printf("Fail\n"); } Exercise 2.4 Write a program that: i. Declare two variables. Initialize the second variable to be 1000. ii. Gets a value of the first variable from the user. iii. If the first variable is 0, the program increments the first variable. iv. If the first variable is 1, the program decrements the first variable and increments the second variable. v. If the first variable is -100, the program increments the first variable and decrements the second integer. vi. In all other cases, the program sets both variables to 0. vii. Print the final result on screen.

Exercise 2.5 Write the following program. Compile, link and run it. #include <stdio.h> void main () { int i; printf("Input any integer: "); scanf("%d",&i);

if (i == 0) printf("Zero\n"); else if (i == 1) printf("One\n"); else if (i == 2) printf("Two\n"); else if (i == 3) printf("Three\n"); else if (i == 4) printf("Four\n"); else if (i == 5) printf("Five\n"); else if (i == 6) printf("Six\n"); else if (i == 7) printf("Seven\n"); else if (i == 8) printf("Eight\n"); else if (i == 9) printf("Nine\n"); else if (i == 10) printf("Ten\n"); else printf("That wasnt a number between 0-10\n"); } Exercise 2.6 Write a program that: i. Gets an integer from the user. ii. If the user types in the number 0, output the string Zero to the screen. iii. If the user types in the number 1, output the string One to the screen. iv. If the user types in the number 2, output the string Two to the screen. v. If the user types in the number 3, output the string Three to the screen. vi. If the user types in the number 4, output the string Four to the screen. vii. If the user types in the number 5, output the string Five to the screen.

viii. ix. x. xi. xii. xiii.

If the user types in the number 6, output the string Six to the screen. If the user types in the number 7, output the string Seven to the screen. If the user types in the number 8, output the string Eight to the screen. If the user types in the number 9, output the string Nine to the screen. If the user types in the number 10, output the string Ten to the screen. If the user types in anything else, output the string That wasnt a number between 0-10. to the screen.

Exercise 2.7 Write the following program. Compile, link and run it. #include <stdio.h> void main () { int i, count, value, even, odd; i = even = odd = 0; printf("Key in how many data:"); scanf("%d", &count); while (i < count) { printf("key in a positif number"); scanf("%d", &value); if (value%2 == 0) even++; else odd++; i++; } printf("Even number: %d\n", even); printf("Odd number : %d\n", odd); } Exercise 2.8 Write a program that: i. Prompts the user for input. ii. Gets a number from the user.

iii. iv. v.

Declares and initializes an integer counter variable. While the counter variable is less than the number the user typed in, output the value of the counter to the screen. Increment the counter.

Exercise 2.9 Write the following program. Compile, link and run it. #include <stdio.h> void main () { int p, n; long int factorial; printf("Key in value of n:"); scanf("%d", &n); if ((n < 0) || (n>15)) printf("n value is out of range\n"); else if (n == 0) printf("0! = 1\n"); else { p = factorial = 1; do { p++; factorial *=p; } while (p != n); printf("%d! = %ld\n",n, factorial); } }

Exercise 2.10 Write a program that: i. Prompts the user for input. ii. Gets a number from the user. iii. Declares an integer counter variable.

iv.

Does the following repeatedly: a. Prints the current value of the counter. b. Increments the counter until the counter is greater than or equal to the number the user typed in.

Exercise 2.11 Write a program that: i. Declares an integer counter variable and initializes it to 0. ii. For each value between the range of 0 to 99 inclusive a. Output the value of the counter. b. If the counter variable is divisible by 5, output an end line.

Exercise 2.12 Write a program to display a table of temperature conversion from Fahrenheit to Celsius or vice versa. i. The starting, ending and incremental temperatures should be requested at run time, along with the required conversion type (C to F or F to C). ii. Temperature conversion can be performed in accordance with the equation C = 1.8 x F + 32.0, where C and F are temperatures expressed in Celsius and Fahrenheit, respectively.

Exercise 2.13 Write a program: i. To read values for R, L and V and also the final and incremental times for which the circuit will be examined and then display. ii. The current i in ampere present the circuit shown in Figure below at time t seconds after the voltage is V is applied is given by the equation i = (V/R)(1 e-Rt/L) where R and L represent the resistance and inductance of the circuit respectively. Write the output into a data file.

iii.

Exercise 2.14 Write a program using if-else and the conditional operator to determine whether the year is a leap year or not. Hint: Any year exactly divisible by 4 is a leap year. However, it that same year is exactly divisible by 100 it is not, unless it is also exactly divisible by 400

S-ar putea să vă placă și