Professional Documents
Culture Documents
Lab Task 1:
i. Output (100 200) b =100 and c = 200 as expected
ii. Output (300 100) problem is, there is colon after “else” which completes its statement and
execute the next statement also.
iii. Program not runs. Problem is, there is colon after “if” statement.
iv. Program not runs. Problem is, that there is one more “if” statement after “if” statement.
Solution is that “if” statement will be replaced with “else if” statement.
v. Program not runs. Problem is, there is colon after “if” statement.
vi. Program not runs. Problem is, there is colon after “if” statement.
vii. Program not runs. Problem is, there is colon after “if” statement.
viii. Output (100) as b<500 then b =100 as expected
ix. Program not runs. Problem is, there is colon after “else if” statement.
x. Output (100) as b<500 then b =100 as expected
Lab Task 2. Write a program which inputs a one-digit number from the user (i.e. 0-9). The
program should them print that number in words, e.g. “Zero” for 0, “One” for 1, “Two” for 2, and so
on. If the user does not enter a one-digit number, then program should display an error: “Invalid
number!”.
Solution:
#include <stdio.h>
int main()
int user_input;
scanf("%d", &user_input);
switch(user_input)
case 0:
printf("Zero");
break;
case 1:
printf("One");
break;
case 2:
printf("Two");
break;
case 3:
printf("Three");
break;
case 4:
printf("Four");
break;
case 5:
printf("Five");
break;
case 6:
printf("Six");
break;
case 7:
printf("Seven");
break;
case 8:
printf("Eight");
break;
case 9:
printf("Nine");
break;
default:
printf("Invalid number!");
return 0;
}
Output :
Lab Task 3. Write a program to swap two numbers. (HINT: if you have two glasses full of red and
green color water, then you cannot swap them without using a third temporary glass. Similarly, you
will need a third variable to hold the original value of one of the variables for swapping)
Solution:
#include <stdio.h>
int main()
int num_1;
int num_2;
scanf("%d", &num_1);
scanf("%d", &num_2);
int swap;
swap = num_1;
num_1 = num_2;
num_2 = swap;
return 0;
Output:
Lab Task 4. A certain grade of steel is graded according to the following conditions:
(i) Hardness must be greater than 50
(ii) Carbon content must be less than 0.7
(iii) Tensile strength must be greater than 5600
Write a program, which will require the user to give values of hardness, carbon content and
tensile strength of the steel under consideration and output the grade of the steel
Solution:
#include <stdio.h>
int main()
float inp_carbon;
scanf("%d", &inp_hardness);
scanf("%f", &inp_carbon);
scanf("%d", &inp_ts);
printf("\nGrade 10");
printf("\nGrade 9");
}
else if (inp_carbon < carbon && inp_ts == tensile){
printf("\nGrade 8");
printf("\nGrade 7");
printf("\nGrade 6");
else{
printf("\nGrade 5");
return 0;
Output: