switch
1. Enlist any 4 decision-making statements available in C
Here are 4 common decision-making statements in C:
• if statement: Executes a block of code if a condition is true.
• if-else statement: Executes one block of code if a condition is true, and another block if it's false.
• nested if-else statement: Combines multiple if-else statements to make more complex decisions.
• switch statement: Selects one of several code blocks to execute based on the value of an expression.
2. Explain the syntax of if statement with a flowchart.
The syntax of an if statement is:
C
if (condition) {
// code to be executed if the condition is true
}
Use code with caution.
Here's a flowchart to illustrate the if statement:
Code snippet
flowchart LR
A[Start] --> B{Condition}
B -- Yes --> C[Execute code]
B -- No --> D[End]
Use code with caution.
3. Write the syntax of if-else statement.
The syntax of an if-else statement is:
C
if (condition) {
// code to be executed if the condition is true
} else {
// code to be executed if the condition is false
}
Use code with caution.
4. Write a Program to enter the age of a person and check whether the person is eligible to vote.
C
#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
if (age >= 18) {
printf("You are eligible to vote.\n");
} else {
printf("You are not eligible to vote.\n");
}
return 0;
}
Use code with caution.
5. Write a Program to enter an integer number and check whether the number is even or odd.
C
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
if (number % 2 == 0) {
printf("%d is even.\n", number);
} else {
printf("%d is odd.\n", number);
}
return 0;
}
Use code with caution.
6. Write a C program to check whether a number is positive, negative, or zero using nested if-else
statements.
C
#include <stdio.h>
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (number > 0) {
printf("%d is positive.\n", number);
} else {
if (number < 0) {
printf("%d is negative.\n", number);
} else {
printf("%d is zero.\n", number);
}
}
return 0;
}
Use code with caution.
7. Write a C program to check whether the character entered is an alphabet, digit, or special
character.
C
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
printf("%c is an alphabet.\n", ch);
} else if (ch >= '0' && ch <= '9') {
printf("%c is a digit.\n", ch);
} else {
printf("%c is a special character.\n", ch);
}
return 0;
}
Use code with caution.
8. Write a C program to enter student marks and find percentage and grade using if-else-if. Use the
following information:
C
#include <stdio.h>
int main() {
int marks;
float percentage;
printf("Enter the marks obtained: ");
scanf("%d", &marks);
percentage = (float)marks / 100 * 100;
if (percentage >= 80) {
printf("Grade A\n");
} else if (percentage >= 60) {
printf("Grade B\n");
} else if (percentage >= 40) {
printf("Grade C\n");
} else {
printf("Fail\n");
}
return 0;
}
Use code with caution.
9. Write a C program to print the total number of days in a month using a switch case.
C
#include <stdio.h>
int main() {
int month;
printf("Enter the month number (1-12): ");
scanf("%d", &month);
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
printf("31 days\n");
break;
case 4:
case 6:
case 9:
case 11:
printf("30 days\n");
break;
case 2:
printf("28/29 days\n");
break;
default:
printf("Invalid month number\n");
}
return 0;
}
Use code with caution.
10. Write a program using a switch case statement to display the result of a student based on the
grade entered by the user.
C
#include <stdio.h>
int main() {
char grade;
printf("Enter the grade: ");
scanf("%c", &grade);
switch (grade) {
case 'A':
printf("Excellent!\n");
break;
case 'B':
printf("Good job.\n");
break;
case 'C':
printf("Satisfactory\n");
break;
case 'D':
printf("Needs improvement\n");
break;
case 'F':
printf("You failed\n");
break;
default:
printf("Invalid grade\n");
}
return 0;
}
11. What will be the output when you will execute the following C code?
C
void main() {
int check = 2;
switch(check) {
case 2: printf("1");
break;
case 3: printf("2");
break;
}
}
Use code with caution.
Output: 1
Explanation: The value of check is 2, which matches the first case. So, the code inside the first case,
printf("1");, is executed.
12. What will be the output when you will execute the following C code?
C
void main() {
int fruit = 1;
switch(fruit + 2) {
case 4: printf("banana");
case 5: printf("orange");
case 8: printf("grape");
default: printf("apple");
}
}
Use code with caution.
Output: bananaorangegrape
Explanation: The value of fruit + 2 is 3. Since there's no case 3, the control falls through to the next
case, case 4. The code inside case 4, case 5, and case 8 is executed sequentially.
13. Syntax of for loop with flowchart
Syntax:
C
for (initialization; condition; increment/decrement) {
// code to be executed
}
Use code with caution.
Flowchart:
Code snippet
flowchart LR
A[Start] --> B{Initialization}
B --> C{Condition}
C -- Yes --> D{Code}
D --> E{Increment/Decrement}
E --> C
C -- No --> F[End]
Use code with caution.
14. Syntax of switch case statement
C
switch (expression) {
case constant1:
// code to be executed if expression == constant1
break;
case constant2:
// code to be executed if expression == constant2
break;
// ... more cases
default:
// code to be executed if no case matches
}
Use code with caution.
15. Nested loops
Nested loops are loops within loops. They are used to perform repetitive tasks that require multiple
iterations.
Example:
C
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
printf("%d ", i * j);
}
printf("\n");
}
Use code with caution.
This code will print the multiplication table from 1 to 3.
16. Difference between while loop and do-while loop
Feature while loop do-while loop
Condition
Before the loop body After the loop body
check
Executes the loop body if the Executes the loop body at least once, then checks
Execution
condition is true the condition
Export to Sheets
17. Program to perform arithmetic operations using switch case
C
#include <stdio.h>
int main() {
int num1, num2, choice;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
printf("Enter your choice:\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Sum = %d\n", num1 + num2);
break;
// ... other cases for subtraction, multiplication, and division
}
return 0;
}
Use code with caution.
18. Program to display colors using switch case (VIBGYOR)
C
#include <stdio.h>
int main() {
int color_code;
printf("Enter a color code (1-7): ");
scanf("%d", &color_code);
switch (color_code) {
case 1:
printf("Violet\n");
break;
// ... other cases for indigo, blue, green, yellow, orange, and red
}
return 0;
}
Use code with caution.
19. Program to display vowels and consonants using switch case
C
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
switch (ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
printf("%c is a vowel\n", ch);
break;
default:
printf("%c is a consonant\n", ch);
}
return 0;
}
Use code with caution.
20. Program to display names of days in a week using switch case
C
#include <stdio.h>
int main() {
int day_num;
printf("Enter a day number (1-7): ");
scanf("%d", &day_num);
switch (day_num) {
case 1:
printf("Monday\n");
break;
// ... other cases for Tuesday, Wednesday, Thursday, Friday, Saturday, and
Sunday
}
return 0;
}
Use code with caution.
21. C Program to perform different arithmetic operations using switch case
(Similar to the program in 17)
22. C Program to display colors for the letters in (VIBGYOR) using switch case
(Similar to the program in 18)
23. C program to find the sum of natural numbers from 1 to N using a for loop
C
#include <stdio.h> void main ()
printf("Enter numbers");
int main() { scanf("%d",&n);
int n, sum = 0; for(i-1;i<=n;i++)
{
printf("Enter a number: "); sun=sum+i;
scanf("%d", &n); printf("sum=%d",sum);
}
for (int i = 1; i <= n; i++) {
}
sum += i;
}
printf("Sum of natural numbers from 1 to %d = %d\n", n, sum);
return 0;
}
Use code with caution.
24. Program to find the sum of the individual digits of a given number using a while loop
C
#include <stdio.h>
int main() {
int num, digit, sum = 0;
printf("Enter a number: ");
scanf("%d", &num);
while (num > 0) {
digit = num % 10;
sum += digit;
num /= 10;
}
printf("Sum of digits = %d\n", sum);
return 0;
}
Use code with caution.
25. Nested loop to display multiplication tables from 1 to 4
(Similar to the example in 15)
26. Program to check whether a given number is a palindrome or not
C
#include <stdio.h>
int main() {
int num, reversed_num = 0, original_num;
printf("Enter a number: ");
scanf("%d", &num);
original_num = num;
while (num > 0) {
reversed_num = reversed_num * 10 + num % 10;
num /= 10;
}
if (original_num == reversed_num) {
printf("%d is a palindrome\n", original_num);
} else {
printf("%d is not a palindrome\n", original_num);
}
return 0;
}
Use code with caution.
27. Program to display multiplication tables from 2 to 5 except 3
C
#include <stdio.h>
int main() {
for (int i = 2; i <= 5; i++) {
if (i == 3) {
continue; // Skip the iteration for i = 3
}
for (int j = 1; j <= 10; j++) {
printf("%d x %d = %d\n", i, j, i * j);
}
printf("\n");
}
return 0;
}
28. Here's a C program to display the given pattern using a for loop:
C
#include <stdio.h>
int main() {
int rows = 5;
for (int i = rows; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
Use code with caution.
29. Here's a C program to print the given pattern using nested for loops:
C
#include <stdio.h>
int main() {
int rows = 4;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}
Use code with caution.
30. Here's a C program to print the Fibonacci series for a given N value:
C
#include <stdio.h>
int main() {
int n, first = 0, second = 1, next;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (int i = 0; i < n; i++) {
printf("%d ", first);
next = first + second;
first = second;
second = next;
}
printf("\n");
return 0;
}
Use code with caution.
31. Here's a C program to compute the sum of a geometric progression for given x and n values:
C
#include <stdio.h>
#include <math.h>
int main() {
int x, n;
double sum = 0.0;
printf("Enter the value of x: ");
scanf("%d", &x);
printf("Enter the value of n: ");
scanf("%d", &n);
for (int i = 0; i <= n; i++) {
sum += pow(x, i);
}
printf("Sum of the geometric progression: %.2lf\n", sum);
return 0;
}
1. What is a function? Explain the difference between user-defined and library functions.
A function is a block of code that performs a specific task. It's like a mini-program within a larger program.
This helps to organize code, make it reusable, and easier to understand.
• User-defined functions: These are functions that you create yourself to perform specific tasks.
• Library functions: These are pre-written functions that are part of the C library. You can use them
to perform common tasks like input/output, mathematical calculations, and string manipulation.
2. What is a function? Write a function to find the sum of two numbers.
A function is a block of code that performs a specific task. Here's a function to find the sum of two numbers:
C
int sum(int a, int b) {
int result = a + b;
return result;
}
Use code with caution.
3. What are actual parameters and formal parameters? Illustrate with an example.
• Actual parameters: The values that are passed to a function when it is called.
• Formal parameters: The variables that receive the values of the actual parameters.
C
int sum(int a, int b) { // a and b are formal parameters
int result = a + b;
return result;
}
int main() {
int x = 5, y = 10;
int z = sum(x, y); // x and y are actual parameters
printf("Sum: %d", z);
return 0;
}
Use code with caution.
4. What do you mean by parameter passing in functions? Explain call by value with a suitable
example.
Parameter passing is the process of transferring data from the calling function to the called function. In call
by value, a copy of the actual parameter is passed to the formal parameter. Changes made to the formal
parameter do not affect the actual parameter.
C
void swap(int a, int b) {
int temp = a;
a = b;
b = temp;
}
int main() {
int x = 10, y = 20;
swap(x, y);
printf("x = %d, y = %d\n", x, y); // x and y remain unchanged
return 0;
}
Use code with caution.
5. Explain the function without parameters and without return value with example.
A function without parameters and without a return value is a simple function that performs a task but
doesn't receive any input or return any output.
C
void greet() {
printf("Hello, world!\n");
}
Use code with caution.
6. Explain the function with parameters and without return value with example.
A function with parameters and without a return value receives input values but doesn't return any output.
C
void printName(char name[]) {
printf("Name: %s\n", name);
}
Use code with caution.
7. Explain the function without parameters and with return value with example.
A function without parameters and with a return value performs a task and returns a value.
C
int getRandomNumber() {
return rand() % 100;
}
Use code with caution.
8. Explain the function with parameters and with return value with example.
A function with parameters and with a return value receives input values and returns an output value.
C
int multiply(int a, int b) {
return a * b;
}
Use code with caution.
9. Illustrate function declaration, function definition, and function calling with a suitable example in
C.
• Declaration: Declares the function's name, return type, and parameters.
• Definition: Defines the function's body, including the code to be executed.
• Calling: Invokes the function to execute its code.
C
// Declaration
int sum(int a, int b);
// Definition
int sum(int a, int b) {
return a + b;
}
// Calling
int main() {
int x = 5, y = 10;
int z = sum(x, y);
printf("Sum: %d", z);
return 0;
}
Use code with caution.
10. Write a program to calculate the area of a square using a function. with return type without parameter
C int square (); // fun declaration;
#include <stdio.h>
void main ()
int calculateArea(int side) { { int area , side;
return side * side; printf(" Enter side of square");
} scanf("%d",&side);
square(); // fun Call ;
int main() {
int side, area;
}
int square() // fun defination
printf("Enter the side of the square: "); {
scanf("%d", &side); area = side*side;
printf("area=%d",area);
area = calculateArea(side);
}
printf("Area of the square: %d\n", area);
return 0; Enter side of square 4
} area = 16
Use code with caution.
11. Write a program to find the product of two numbers using functions without arguments, without
return type.
product(inta,intb); // fun declaration
C void main ()
#include <stdio.h> { int mult;
printf("a and b = ");
void product() { scanf("%d%d,"&a,&b);
int a, b, product; product(4,5);
}
printf("Enter two numbers: "); product(inta,intb)
scanf("%d %d", &a, &b); {
mult=a*b;
product = a * b; printf("mult=%d",mult);
}
printf("Product: %d\n", product);
} a and b = 4 5
mult = 20
int main() {
product();
return 0;
}
Use code with caution.
12. Explain return with a suitable example.
The return statement is used to exit a function and return a value to the calling function.
C
int add(int a, int b) {
int sum = a + b;
return sum; // Returns the sum to the calling function
}
Use code with caution.
13. Explain the scope of variables with a suitable example.
The scope of a variable determines where it can be accessed in a program.
• Local variables: Declared inside a function and can only be accessed within that function.
• Global variables: Declared outside a function and can be accessed from any part of the program.
C
int global_var = 10; // Global variable
int myFunction() {
int local_var = 20; // Local variable
printf("Global variable: %d\n", global_var);
printf("Local variable: %d\n", local_var);
return 0;
}
Use code with caution.
14. Explain the concept of Local and Global variables using a function.
(Same as 13)
Write a syntax and flowchart of following statements
1 - IF statement
2 - IF else
3 - IF else ladder
4 - IF else nested
5 - While loop
6 - DO whilel loop