You are on page 1of 7

Lab Task

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;

printf("Enter a number 0-9 : ");

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;

printf("Enter first number : ");

scanf("%d", &num_1);

printf("Enter second number : ");

scanf("%d", &num_2);

printf("\n\nYour first number is %d \n", num_1);

printf("Your second number is %d \n\n", num_2);


printf("Swapping... \n\n");

int swap;

swap = num_1;

num_1 = num_2;

num_2 = swap;

printf("first number is %d\n", num_1);

printf("second number is %d\n", num_2);

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

The grades are as follows:

➢ Grade is 10 if all three conditions are met


➢ Grade is 9 if conditions(i) and (ii) are met
➢ Grade is 8 if conditions (ii) and (iii) are met
➢ Grade is 7 if conditions (i) and (iii) are met
➢ Grade is 6 if only one condition is met
➢ Grade is 5 if none of the conditions are met

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()

int hardness = 50;

float carbon = 0.7;

int tensile = 5600;

int inp_hardness, inp_ts;

float inp_carbon;

printf("Enter hardness of Steel : ");

scanf("%d", &inp_hardness);

printf("Enter Carbon Content in Steel : ");

scanf("%f", &inp_carbon);

printf("Enter Tensile Strength of Steel : ");

scanf("%d", &inp_ts);

printf("\nYours Steel Hardness %d\n", inp_hardness);

printf("Yours Steel Carbon Content %f\n", inp_carbon);

printf("Yours Steel Tensile Strength %d\n", inp_ts);

if (inp_hardness == hardness && inp_carbon < carbon && inp_ts == tensile){

printf("\nGrade 10");

else if (inp_hardness == hardness && inp_carbon < carbon){

printf("\nGrade 9");

}
else if (inp_carbon < carbon && inp_ts == tensile){

printf("\nGrade 8");

else if (inp_hardness == hardness && inp_ts == tensile){

printf("\nGrade 7");

else if (inp_hardness == hardness || inp_carbon < carbon || inp_ts == tensile){

printf("\nGrade 6");

else{

printf("\nGrade 5");

return 0;

Output:

You might also like