You are on page 1of 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 () {

iii. Initialize the second variable to be 1000.4 Write a program that: i. #include <stdio. v. . } Exercise 2. Print the final result on screen. ii. iv. link and run it. If the first variable is 0.5 Write the following program. if (marks >= 80) printf("Excellent\n"). scanf("%d". else if (marks >= 40) printf("Pass\n"). If the first variable is 1. printf("Obtained marks?:"). Compile. vi. the program decrements the first variable and increments the second variable.&i). else if (marks >= 60) printf("Good\n"). &marks).int marks. else printf("Fail\n"). the program increments the first variable. the program sets both variables to 0.h> void main () { int i. In all other cases. Gets a value of the first variable from the user. Declare two variables. scanf("%d". If the first variable is -100. Exercise 2. vii. the program increments the first variable and decrements the second integer. printf("Input any integer: ").

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

output the string “Six” to the screen. if (value%2 == 0) even++. else odd++. xiii. ii. . Compile.7 Write the following program. odd). scanf("%d". Gets a number from the user. &count). odd. If the user types in the number 10. } Exercise 2.8 Write a program that: i. output the string “Eight” to the screen. #include <stdio. } printf("Even number: %d\n". Prompts the user for input. i = even = odd = 0. while (i < count) { printf("key in a positif number"). count. xi. &value). printf("Key in how many data:"). x. value. If the user types in anything else. printf("Odd number : %d\n".” to the screen. ix. even. If the user types in the number 8.viii. even). If the user types in the number 7.h> void main () { int i. If the user types in the number 9. Exercise 2. If the user types in the number 6. scanf("%d". output the string “Nine” to the screen. link and run it. output the string “Seven” to the screen. output the string “That wasn’t a number between 0-10. output the string “Ten” to the screen. xii. i++.

factorial *=p. factorial).n. iii. long int factorial. scanf("%d". #include <stdio. printf("Key in value of n:"). Compile. Increment the counter. } while (p != n). link and run it. Declares an integer counter variable. Declares and initializes an integer counter variable. else { p = factorial = 1. output the value of the counter to the screen.9 Write the following program.h> void main () { int p.10 Write a program that: i. v. iv.iii. . if ((n < 0) || (n>15)) printf("n value is out of range\n"). While the counter variable is less than the number the user typed in. ii. } } Exercise 2. else if (n == 0) printf("0! = 1\n"). n. Gets a number from the user. Prompts the user for input. printf("%d! = %ld\n". &n). do { p++. Exercise 2.

b. iii. Exercise 2. To read values for ‘R’. Output the value of the counter. . Write the output into a data file.13 Write a program: i. ii. b. Temperature conversion can be performed in accordance with the equation C = 1. output an end line. where C and F are temperatures expressed in Celsius and Fahrenheit. Increments the counter until the counter is greater than or equal to the number the user typed in. ii. Declares an integer counter variable and initializes it to 0.0. respectively. The starting.11 Write a program that: i. For each value between the range of 0 to 99 inclusive a. ending and incremental temperatures should be requested at run time. ii. ‘L’ and ‘V’ and also the final and incremental times for which the circuit will be examined and then display.8 x F + 32. Exercise 2.iv. Exercise 2. If the counter variable is divisible by 5. i. 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.12 Write a program to display a table of temperature conversion from Fahrenheit to Celsius or vice versa. Does the following repeatedly: a. along with the required conversion type (C to F or F to C). Prints the current value of the counter.

However.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.Exercise 2. unless it is also exactly divisible by 400 . it that same year is exactly divisible by 100 it is not.