Class 11: C Programming Lab Solutions (Sequential, If-Else, Switch Case)

1. WAP to display your name, age, and address.

#include <stdio.h>
int main() {
char name[50], address[50];
int age;

printf("Enter name: ");
scanf("%s", name);

printf("Enter age: ");
scanf("%d", &age);

printf("Enter address: ");
scanf("%s", address);

printf("\nName: %s\nAge: %d\nAddress: %s", name, age, address);

return 0;
}

Output:

Enter name: Ram
Enter age: 25
Enter address: Kathmandu

Name: Ram
Age: 25
Address: Kathmandu

2. WAP to find area of a circle.

#include <stdio.h>
int main() {
float r, area;

printf("Enter radius: ");
scanf("%f", &r);

area = 3.14 * r * r;
printf("Area = %.2f", area);

return 0;
}

Output:

Enter radius: 5
Area = 78.50

3. WAP to find circumference of a circle.

#include <stdio.h>
int main() {
float r, c;

printf("Enter radius: ");
scanf("%f", &r);

c = 2 * 3.14 * r;
printf("Circumference = %.2f", c);

return 0;
}

Output:

Enter radius: 5
Circumference = 31.40

4. WAP to find area of rectangle.

#include <stdio.h>
int main() {
float l, b;

printf("Enter length and breadth: ");
scanf("%f %f", &l, &b);

printf("Area = %.2f", l * b);

return 0;
}

Output:

Enter length and breadth: 5 4
Area = 20.00

5. WAP to find area of a square.

#include <stdio.h>
int main() {
float s;

printf("Enter side: ");
scanf("%f", &s);

printf("Area = %.2f", s * s);

return 0;
}

Output:

Enter side: 6
Area = 36.00

6. WAP to calculate simple interest.

#include <stdio.h>
int main() {
float p, t, r;

printf("Enter P T R: ");
scanf("%f %f %f", &p, &t, &r);

printf("SI = %.2f", (p * t * r) / 100);

return 0;
}

Output:

Enter P T R: 1000 2 10
SI = 200.00

7. WAP to read marks of five subjects and calculate total marks and percentage.

#include <stdio.h>
int main() {
float m1, m2, m3, m4, m5, total, percentage;

printf("Enter marks: ");
scanf("%f %f %f %f %f", &m1, &m2, &m3, &m4, &m5);

total = m1 + m2 + m3 + m4 + m5;
percentage = total / 5;

printf("Total = %.2f\nPercentage = %.2f%%", total, percentage);

return 0;
}

Output:

Enter marks: 70 80 90 60 50
Total = 350.00
Percentage = 70.00%

8. WAP to read a number if it is positive or negative.

#include <stdio.h>
int main() {
int n;

printf("Enter number: ");
scanf("%d", &n);

if(n >= 0)
printf("Positive");
else
printf("Negative");

return 0;
}

Output:

Enter number: -3
Negative

9. WAP to read a mark in a subject and decide if a student is pass or fail.

#include <stdio.h>
int main() {
float m;

printf("Enter marks: ");
scanf("%f", &m);

if(m >= 40)
printf("Pass");
else
printf("Fail");

return 0;
}

Output:

Enter marks: 35
Fail

10. WAP to read a number and decide if it is odd or even.

#include <stdio.h>
int main() {
int n;

printf("Enter number: ");
scanf("%d", &n);

if(n % 2 == 0)
printf("Even");
else
printf("Odd");

return 0;
}

Output:

Enter number: 8
Even

11. WAP to read a number and decide if it is divisible by 3 or not.

#include <stdio.h>
int main() {
int n;

printf("Enter number: ");
scanf("%d", &n);

if(n % 3 == 0)
printf("Divisible");
else
printf("Not Divisible");

return 0;
}

Output:

Enter number: 7
Not Divisible

12. WAP to read two numbers and decide if they are equal or not.

#include <stdio.h>
int main() {
int a, b;

printf("Enter two numbers: ");
scanf("%d %d", &a, &b);

if(a == b)
printf("Equal");
else
printf("Not Equal");

return 0;
}

Output:

Enter two numbers: 5 5
Equal

13. WAP to read two numbers and find greatest among them.

#include <stdio.h>
int main() {
int a, b;

printf("Enter two numbers: ");
scanf("%d %d", &a, &b);

if(a > b)
printf("%d is greatest", a);
else
printf("%d is greatest", b);

return 0;
}

Output:

Enter two numbers: 10 6
10 is greatest

14. WAP to read three numbers and find the smallest number among them, and also check if they are equal.

#include <stdio.h>
int main() {
int a, b, c;

printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);

if(a == b && b == c)
printf("All equal");
else if(a <= b && a <= c)
printf("Smallest = %d", a);
else if(b <= a && b <= c)
printf("Smallest = %d", b);
else
printf("Smallest = %d", c);

return 0;
}

Output:

Enter three numbers: 3 7 2
Smallest = 2

15. WAP to read marks of 5 subjects and display the grade of students based on the percentage obtained.

#include <stdio.h>
int main() {
float per;

printf("Enter percentage: ");
scanf("%f", &per);

if(per >= 90)
printf("A");
else if(per >= 80)
printf("B");
else if(per >= 70)
printf("C");
else if(per >= 40)
printf("D");
else
printf("Fail");

return 0;
}

Output:

Enter percentage: 75
C

16. WAP to display name of days of a week using switch case.

#include <stdio.h>
int main() {
int d;

printf("Enter day (1-7): ");
scanf("%d", &d);

switch(d) {
case 1: printf("Sunday"); break;
case 2: printf("Monday"); break;
case 3: printf("Tuesday"); break;
case 4: printf("Wednesday"); break;
case 5: printf("Thursday"); break;
case 6: printf("Friday"); break;
case 7: printf("Saturday"); break;
default: printf("Invalid");
}

return 0;
}

Output:

Enter day (1-7): 3
Tuesday

17. WAP to perform addition, subtraction, multiplication, and division operation based on a character input provided.

#include <stdio.h>
int main() {
float a, b;
char op;

printf("Enter operator (+,-,*,/): ");
scanf(" %c", &op);

printf("Enter two numbers: ");
scanf("%f %f", &a, &b);

switch(op) {
case '+': printf("Sum = %.2f", a + b); break;
case '-': printf("Diff = %.2f", a - b); break;
case '*': printf("Mul = %.2f", a * b); break;
case '/': printf("Div = %.2f", a / b); break;
default: printf("Invalid");
}

return 0;
}

Output:

Enter operator: *
Enter two numbers: 4 5
Mul = 20.00

18. WAP to check if an alphabet is Vowel or Consonant using switch case.

#include <stdio.h>
int main() {
char ch;

printf("Enter character: ");
scanf(" %c", &ch);

switch(ch) {
case 'a': case 'e': case 'i': case 'o': case 'u':
case 'A': case 'E': case 'I': case 'O': case 'U':
printf("Vowel");
break;
default:
printf("Consonant");
}

return 0;
}

Output:

Enter character: b
Consonant
Netra Koirala

Netra Koirala

Computer Science Educator

Passionate computer science educator and author. Provides free study notes, practical guides, and tutorials for Class 9, 10, 11, 12, and B.Sc CSIT students in Nepal. Years of teaching experience in computer science fundamentals.

Computer Science notes, tutorials, MCQs, and educational resources for Nepal students. Covering Class 9, SEE preparation, Class 11, Class 12, SLC, programming, DBMS, networking, HTML, JavaScript, PHP, OOP and more.

Featured Post

Grade 10 Computer Science: Specification Grid & Model Questions

Specification Grid & Model Questions of Computer Science | Grade 10 📚 Examination Resource Specification Grid & M...

Followers