0% found this document useful (0 votes)
20 views30 pages

Level 1 - Key

The document contains a series of C programming exercises covering variables, data types, input/output statements, control flow, and nested conditional statements. Each exercise includes a code snippet, test cases, and expected outputs for various inputs. The exercises aim to help learners practice and understand fundamental programming concepts in C.

Uploaded by

anitha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views30 pages

Level 1 - Key

The document contains a series of C programming exercises covering variables, data types, input/output statements, control flow, and nested conditional statements. Each exercise includes a code snippet, test cases, and expected outputs for various inputs. The exercises aim to help learners practice and understand fundamental programming concepts in C.

Uploaded by

anitha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 30

1.

Variables and Datatypes

Q1. Write a C program to declare two integer variables and display their sum.
#include <stdio.h>
int main() {
int a, b;
printf("Enter two integers: ");
scanf("%d %d", &a, &b);
printf("Sum = %d\n", a + b);
return 0;
}
Test cases:
- Input: 10, 20 → Output: 30
- Input: -5, 15 → Output: 10
- Input: 0, 0 → Output: 0

Q2. Write a program to declare a float variable, input its value, and display it with 2 decimal
places.
```c
#include <stdio.h>
int main() {
float num;
printf("Enter a float number: ");
scanf("%f", &num);
printf("You entered: %.2f\n", num);
return 0;
}
```
Test cases:
- Input: 12.345 → Output: 12.35
- Input: 0 → Output: 0.00
- Input: -56.789 → Output: -56.79

---

Q3. Write a C program to calculate the area of a rectangle using integer inputs for length
and width.
```c
#include <stdio.h>
int main() {
int length, width;
printf("Enter length and width: ");
scanf("%d %d", &length, &width);
printf("Area = %d\n", length width);
return 0;
}
```
Test cases:
- Input: 5, 10 → Output: 50
- Input: 3, 4 → Output: 12
- Input: 7, 0 → Output: 0

---

2. Input and Output Statements


Q4. Write a program to input a character and print its ASCII value.
```c
#include <stdio.h>
int main() {
char c;
printf("Enter a character: ");
scanf("%c", &c);
printf("ASCII value of %c = %d\n", c, c);
return 0;
}
```
Test cases:
- Input: A → Output: ASCII value of A = 65
- Input: z → Output: ASCII value of z = 122
- Input: 3 → Output: ASCII value of 3 = 51

---

Q5. Write a program to read three integers and print their average.
```c
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three integers: ");
scanf("%d %d %d", &a, &b, &c);
printf("Average = %.2f\n", (a + b + c) / 3.0);
return 0;
}
```
Test cases:
- Input: 5, 10, 15 → Output: Average = 10.00
- Input: -2, 0, 2 → Output: Average = 0.00
- Input: 1, 1, 1 → Output: Average = 1.00

---

Q6. Write a program to input a string and print it.


```c
#include <stdio.h>
int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);
printf("You entered: %s\n", str);
return 0;
}
```
Test cases:
- Input: Hello → Output: You entered: Hello
- Input: CProgramming → Output: You entered: CProgramming
- Input: Code123 → Output: You entered: Code123

---

3. Control Flow - If-Else Statements


Q7. Write a program to check if a number is positive, negative, or zero.
```c
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0)
printf("Positive\n");
else if (num < 0)
printf("Negative\n");
else
printf("Zero\n");
return 0;
}
```
Test cases:
- Input: 5 → Output: Positive
- Input: -7 → Output: Negative
- Input: 0 → Output: Zero

---

Q8. Write a program to check if a number is even or odd.


```c
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0)
printf("Even\n");
else
printf("Odd\n");
return 0;
}
```
Test cases:
- Input: 4 → Output: Even
- Input: 7 → Output: Odd
- Input: 0 → Output: Even

---

Q9. Write a program to check if a person is eligible to vote (age >= 18).
```c
#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
if (age >= 18)
printf("Eligible to vote\n");
else
printf("Not eligible to vote\n");
return 0;
}
```
Test cases:
- Input: 20 → Output: Eligible to vote
- Input: 16 → Output: Not eligible to vote
- Input: 18 → Output: Eligible to vote

---

4. Control Flow - Switch Statement

Q10. Write a program to input a day number (1-7) and print the corresponding day of the
week.
```c
#include <stdio.h>
int main() {
int day;
printf("Enter day number (1-7): ");
scanf("%d", &day);
switch (day) {
case 1: printf("Sunday\n"); break;
case 2: printf("Monday\n"); break;
case 3: printf("Tuesday\n"); break;
case 4: printf("Wednesday\n"); break;
case 5: printf("Thursday\n"); break;
case 6: printf("Friday\n"); break;
case 7: printf("Saturday\n"); break;
default: printf("Invalid day\n");
}
return 0;
}
```
Test cases:
- Input: 1 → Output: Sunday
- Input: 4 → Output: Wednesday
- Input: 8 → Output: Invalid day

---

Q11. Write a program to input a character and print whether it is a vowel or consonant
using a switch statement.
```c
#include <stdio.h>
int main() {
char c;
printf("Enter a character: ");
scanf(" %c", &c);
switch (c) {
case 'a': case 'e': case 'i': case 'o': case 'u':
case 'A': case 'E': case 'I': case 'O': case 'U':
printf("Vowel\n");
break;
default:
printf("Consonant\n");
}
return 0;
}
```
Test cases:
- Input: a → Output: Vowel
- Input: B → Output: Consonant
- Input: e → Output: Vowel

---

5. Nested Conditional Statements

Q12. Write a program to find the largest of three numbers using nested if-else statements.
```c
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a >= b) {
if (a >= c)
printf("%d is the largest\n", a);
else
printf("%d is the largest\n", c);
} else {
if (b >= c)
printf("%d is the largest\n", b);
else
printf("%d is the largest\n", c);
}
return 0;
}
```
Test cases:
- Input: 10, 20, 30 → Output: 30 is the largest
- Input: 50, 25, 10 → Output: 50 is the largest
- Input: 15, 15, 15 → Output: 15 is the largest

---

Q13. Write a program to check if a year is a leap year using nested if-else statements.
```c
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0)
printf("%d is a leap year\n", year);
else
printf("%d is not a leap year\n", year);
} else
printf("%d is a leap year\n", year);
} else
printf("%d is not a leap year\n", year);
return 0;
}
```
Test cases:
- Input: 2024 → Output: 2024 is a leap year
Here are 20 more test case-based C programming questions, continuing from the previous
topics of variables and datatypes, input/output statements, control flow (if-else, switch),
and nested conditional statements:

6. Control Flow - Nested Conditional Statements (Continued)

Q14. Write a program to categorize a person's age into a life stage (Child, Teenager, Adult,
Senior) using nested if-else.
```c
#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
if (age < 13)
printf("Child\n");
else if (age < 20)
printf("Teenager\n");
else if (age < 60)
printf("Adult\n");
else
printf("Senior\n");
return 0;
}
```
Test cases:
- Input: 10 → Output: Child
- Input: 16 → Output: Teenager
- Input: 45 → Output: Adult
- Input: 75 → Output: Senior

---

Q15. Write a program to check if a number is divisible by both 3 and 5 using nested if-else.
```c
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 3 == 0) {
if (num % 5 == 0)
printf("Divisible by both 3 and 5\n");
else
printf("Divisible by 3 but not by 5\n");
} else
printf("Not divisible by 3\n");
return 0;
}
```
Test cases:
- Input: 15 → Output: Divisible by both 3 and 5
- Input: 9 → Output: Divisible by 3 but not by 5
- Input: 7 → Output: Not divisible by 3

---

Q16. Write a program to find the smallest of three numbers using nested if-else statements.
```c
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a <= b) {
if (a <= c)
printf("%d is the smallest\n", a);
else
printf("%d is the smallest\n", c);
} else {
if (b <= c)
printf("%d is the smallest\n", b);
else
printf("%d is the smallest\n", c);
}
return 0;
}
```
Test cases:
- Input: 10, 20, 5 → Output: 5 is the smallest
- Input: 15, 5, 30 → Output: 5 is the smallest
- Input: 25, 25, 25 → Output: 25 is the smallest

---

Q17. Write a program to categorize a number as positive, negative, or zero using nested if-
else.
```c
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0)
printf("Positive\n");
else if (num < 0)
printf("Negative\n");
else
printf("Zero\n");
return 0;
}
```
Test cases:
- Input: 10 → Output: Positive
- Input: -8 → Output: Negative
- Input: 0 → Output: Zero

---
7. Control Flow - Switch Statements (Continued)

Q18. Write a program to input a grade character (A, B, C, D, F) and print the corresponding
performance using a switch statement.
```c
#include <stdio.h>
int main() {
char grade;
printf("Enter a grade (A, B, C, D, F): ");
scanf(" %c", &grade);
switch (grade) {
case 'A': printf("Excellent\n"); break;
case 'B': printf("Good\n"); break;
case 'C': printf("Average\n"); break;
case 'D': printf("Below Average\n"); break;
case 'F': printf("Fail\n"); break;
default: printf("Invalid grade\n");
}
return 0;
}
```
Test cases:
- Input: A → Output: Excellent
- Input: C → Output: Average
- Input: F → Output: Fail
- Input: G → Output: Invalid grade
---

Q19. Write a program to input a number (1-12) and print the corresponding month name
using a switch statement.
```c
#include <stdio.h>
int main() {
int month;
printf("Enter month number (1-12): ");
scanf("%d", &month);
switch (month) {
case 1: printf("January\n"); break;
case 2: printf("February\n"); break;
case 3: printf("March\n"); break;
case 4: printf("April\n"); break;
case 5: printf("May\n"); break;
case 6: printf("June\n"); break;
case 7: printf("July\n"); break;
case 8: printf("August\n"); break;
case 9: printf("September\n"); break;
case 10: printf("October\n"); break;
case 11: printf("November\n"); break;
case 12: printf("December\n"); break;
default: printf("Invalid month\n");
}
return 0;
}
```
Test cases:
- Input: 5 → Output: May
- Input: 11 → Output: November
- Input: 13 → Output: Invalid month

---

8. Input/Output Statements (Continued)

Q20. Write a program to input a number and print its square and cube.
```c
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Square = %d\n", num num);
printf("Cube = %d\n", num num num);
return 0;
}
```
Test cases:
- Input: 3 → Output: Square = 9, Cube = 27
- Input: 5 → Output: Square = 25, Cube = 125
- Input: -2 → Output: Square = 4, Cube = -8

---
Q21. Write a program to input two floating-point numbers and display their product.
```c
#include <stdio.h>
int main() {
float a, b;
printf("Enter two numbers: ");
scanf("%f %f", &a, &b);
printf("Product = %.2f\n", a b);
return 0;
}
```
Test cases:
- Input: 2.5, 3.6 → Output: Product = 9.00
- Input: -4.1, 5.2 → Output: Product = -21.32
- Input: 0, 3.3 → Output: Product = 0.00

---

Q22. Write a program to input an integer and print its binary representation.
```c
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
printf("Binary: ");
for (int i = 31; i >= 0; i--)
printf("%d", (num >> i) & 1);
printf("\n");
return 0;
}
```
Test cases:
- Input: 5 → Output: Binary: 00000000000000000000000000000101
- Input: 10 → Output: Binary: 00000000000000000000000000001010
- Input: 255 → Output: Binary: 00000000000000000000000011111111

---

9. Control Flow - If-Else (Continued)

Q23. Write a program to check whether a number is prime using if-else.


```c
#include <stdio.h>
int main() {
int num, flag = 0;
printf("Enter a number: ");
scanf("%d", &num);
if (num <= 1)
printf("Not Prime\n");
else {
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
flag = 1;
break;
}
}
if (flag == 0)
printf("Prime\n");
else
printf("Not Prime\n");
}
return 0;
}
```
Test cases:
- Input: 7 → Output: Prime
- Input: 9 → Output: Not Prime
- Input: 1 → Output: Not Prime

---

Q24. Write a program to check if a number is a palindrome using if-else.


```c
#include <stdio.h>
int main() {
int num, reversed = 0, original, remainder;
printf("Enter an integer: ");
scanf("%d", &num);
original = num;
while (num != 0) {
remainder

Here are 10 more test case-based C programming questions:


### 10. Control Flow - If-Else Statements (Continued)

**Q25.** Write a program to find if a number is positive, negative, or zero using a nested if-
else statement.
```c
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0)
printf("Positive\n");
else if (num < 0)
printf("Negative\n");
else
printf("Zero\n");
return 0;
}
```
*Test cases:*
- Input: 25 → Output: Positive
- Input: -13 → Output: Negative
- Input: 0 → Output: Zero

---

**Q26.** Write a program to input the length and width of a rectangle, and determine if it is
a square.
```c
#include <stdio.h>
int main() {
int length, width;
printf("Enter length and width: ");
scanf("%d %d", &length, &width);
if (length == width)
printf("It is a square\n");
else
printf("It is not a square\n");
return 0;
}
```
*Test cases:*
- Input: 5, 5 → Output: It is a square
- Input: 7, 10 → Output: It is not a square
- Input: 4, 4 → Output: It is a square

---

**Q27.** Write a program to check if a character is an uppercase or lowercase letter using


if-else.
```c
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
scanf(" %c", &ch);
if (ch >= 'A' && ch <= 'Z')
printf("Uppercase\n");
else if (ch >= 'a' && ch <= 'z')
printf("Lowercase\n");
else
printf("Not a letter\n");
return 0;
}
```
*Test cases:*
- Input: A → Output: Uppercase
- Input: g → Output: Lowercase
- Input: 5 → Output: Not a letter

---

**Q28.** Write a program to find whether a given year is a leap year or not using if-else.
```c
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
printf("%d is a leap year\n", year);
else
printf("%d is not a leap year\n", year);
return 0;
}
```
*Test cases:*
- Input: 2020 → Output: 2020 is a leap year
- Input: 1900 → Output: 1900 is not a leap year
- Input: 2000 → Output: 2000 is a leap year

---

**Q29.** Write a program to input two numbers and check whether they are divisible by
each other using if-else.
```c
#include <stdio.h>
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
if (a % b == 0)
printf("%d is divisible by %d\n", a, b);
else if (b % a == 0)
printf("%d is divisible by %d\n", b, a);
else
printf("Neither number is divisible by the other\n");
return 0;
}
```
*Test cases:*
- Input: 10, 5 → Output: 10 is divisible by 5
- Input: 4, 16 → Output: 16 is divisible by 4
- Input: 7, 3 → Output: Neither number is divisible by the other

---

### 11. Switch Statements (Continued)

**Q30.** Write a program to input a number from 1 to 4 and print the corresponding
season (1: Winter, 2: Spring, 3: Summer, 4: Autumn) using a switch statement.
```c
#include <stdio.h>
int main() {
int season;
printf("Enter a number (1-4): ");
scanf("%d", &season);
switch (season) {
case 1: printf("Winter\n"); break;
case 2: printf("Spring\n"); break;
case 3: printf("Summer\n"); break;
case 4: printf("Autumn\n"); break;
default: printf("Invalid input\n");
}
return 0;
}
```
*Test cases:*
- Input: 1 → Output: Winter
- Input: 3 → Output: Summer
- Input: 5 → Output: Invalid input

---

**Q31.** Write a program to input a number from 1 to 3 and print the corresponding traffic
light (1: Red, 2: Yellow, 3: Green) using a switch statement.
```c
#include <stdio.h>
int main() {
int light;
printf("Enter a number (1-3): ");
scanf("%d", &light);
switch (light) {
case 1: printf("Red\n"); break;
case 2: printf("Yellow\n"); break;
case 3: printf("Green\n"); break;
default: printf("Invalid input\n");
}
return 0;
}
```
*Test cases:*
- Input: 1 → Output: Red
- Input: 3 → Output: Green
- Input: 4 → Output: Invalid input

---
**Q32.** Write a program to input a number from 1 to 5 and print the corresponding grade
description (1: Excellent, 2: Good, 3: Average, 4: Poor, 5: Fail) using a switch statement.
```c
#include <stdio.h>
int main() {
int grade;
printf("Enter a number (1-5): ");
scanf("%d", &grade);
switch (grade) {
case 1: printf("Excellent\n"); break;
case 2: printf("Good\n"); break;
case 3: printf("Average\n"); break;
case 4: printf("Poor\n"); break;
case 5: printf("Fail\n"); break;
default: printf("Invalid input\n");
}
return 0;
}
```
*Test cases:*
- Input: 2 → Output: Good
- Input: 5 → Output: Fail
- Input: 6 → Output: Invalid input

---

### 12. Input/Output Statements (Continued)


**Q33.** Write a program to input a character and check if it is a digit, a letter, or a special
character.
```c
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
scanf(" %c", &ch);
if (ch >= '0' && ch <= '9')
printf("Digit\n");
else if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
printf("Letter\n");
else
printf("Special character\n");
return 0;
}
```
*Test cases:*
- Input: 5 → Output: Digit
- Input: A → Output: Letter
- Input: $ → Output: Special character

---

**Q34.** Write a program to input the radius of a circle and print its circumference and
area.
```c
#include <stdio.h>
#define PI 3.14159
int main() {
float radius;
printf("Enter radius: ");
scanf("%f", &radius);
printf("Circumference = %.2f\n", 2 * PI * radius);
printf("Area = %.2f\n", PI * radius * radius);
return 0;
}
```
*Test cases:*
- Input: 3 → Output: Circumference = 18.85, Area = 28.27
- Input: 5 → Output: Circumference = 31.42, Area = 78.54
- Input: 0 → Output: Circumference = 0.00, Area = 0.00

---

**Q35.** Write a program to input a number and find the sum of its digits.
```c
#include <stdio.h>
int main() {
int num, sum = 0;
printf("Enter a number: ");
scanf("%d", &num);
while (num != 0) {
sum += num % 10;
num /= 10;
}
printf("Sum of digits = %d\n", sum);
return 0;
}
```
*Test cases:*
- Input: 123 → Output: Sum of digits = 6
- Input: 456 → Output: Sum of digits = 15
- Input: 9 → Output: Sum of digits = 9

You might also like