0% found this document useful (0 votes)
14 views5 pages

Important Questions

The document provides an overview of various C programming concepts including case control structures, looping constructs, conditional operators, and arrays. It includes explanations and example programs for each topic, such as using switch statements, for/while loops with break and continue statements, and finding the largest of three numbers using a conditional operator. Additionally, it covers leap year determination, summing even and odd numbers, reversing a multi-digit number, and calculating the sum and average of an array.

Uploaded by

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

Important Questions

The document provides an overview of various C programming concepts including case control structures, looping constructs, conditional operators, and arrays. It includes explanations and example programs for each topic, such as using switch statements, for/while loops with break and continue statements, and finding the largest of three numbers using a conditional operator. Additionally, it covers leap year determination, summing even and odd numbers, reversing a multi-digit number, and calculating the sum and average of an array.

Uploaded by

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

1. What is case control structure in C?

What is the reason for using break statement at


the end of each case in case control block?
2. List the looping constructs used in C. What are the roles of 'break' and 'continue'
statements in loops? Show with examples.
3. Discuss conditional operator? Write a C program to find largest of three numbers using
conditional operator?
4. Any year is entered through the keyboard. Write a C program to determine whether
the year is a leap year or not.
5. Write a program that calculates the sum of even and odd numbers separately from 1
to n (where n is provided by the user) using a while loop. Display both sums at the end.
6. Write a C program to reverse a given multi-digit number.
7.What is an array? Develop a program to find sum and average of all elements stored
in given integer array.

Answer

1. Case Control Structure in C


What is a Case Control Structure?
The case control structure in C is implemented using the switch statement. It allows
the selection of one block of code to execute from multiple options based on a single
variable or expression.
Reason for Using break:
The break statement prevents the program from executing subsequent cases once a
matching case is found. Without it, the program will execute all following cases until a
break or the end of the switch block (fall-through behavior).
Example:
#include <stdio.h>

int main() {
int choice;
printf("Enter a number (1-3): ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("You chose 1.\n");
break;
case 2:
printf("You chose 2.\n");
break;
case 3:
printf("You chose 3.\n");
break;
default:
printf("Invalid choice.\n");
}

return 0;
}

2. Looping Constructs in C
Loop Types:
1. for loop: Used when the number of iterations is known in advance.
2. while loop: Used when the condition for looping depends on runtime checks.
3. do-while loop: Used when the loop body needs to execute at least once.
Role of break and continue:
 break: Exits the loop prematurely.
 continue: Skips the current iteration and moves to the next one.
Example:
#include <stdio.h>

int main() {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
continue; // Skip printing 5
}
if (i == 8) {
break; // Exit loop when i is 8
}
printf("%d\n", i);
}

return 0;
}

3. Conditional Operator
What is a Conditional Operator?
The conditional operator (?:) is a shorthand for if-else statements. It evaluates a condition
and returns one of two values.
Example to Find Largest of Three Numbers:
#include <stdio.h>
int main() {
int a, b, c, largest;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);

largest = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);

printf("The largest number is: %d\n", largest);

return 0;
}

4. Leap Year Check


Logic:
A year is a leap year if:
 It is divisible by 4 but not by 100, or
 It is divisible by 400.
C Code:
#include <stdio.h>

int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {


printf("%d is a leap year.\n", year);
} else {
printf("%d is not a leap year.\n", year);
}

return 0;
}

5. Sum of Even and Odd Numbers


Logic:
 Iterate from 1 to n (user-provided upper limit).
 Check if each number is even or odd using the modulus operator.
o Add even numbers to an even_sum.
o Add odd numbers to an odd_sum.
 Output both sums.
C Code:
#include <stdio.h>

int main() {
int n, even_sum = 0, odd_sum = 0;
printf("Enter a number: ");
scanf("%d", &n);

int i = 1;
while (i <= n) {
if (i % 2 == 0) {
even_sum += i;
} else {
odd_sum += i;
}
i++;
}

printf("Sum of even numbers: %d\n", even_sum);


printf("Sum of odd numbers: %d\n", odd_sum);

return 0;
}

6. Reversing a Multi-Digit Number


Logic:
 Extract the last digit of the number using the modulus operator (% 10).
 Append it to the reversed number by multiplying the reverse by 10 and adding the
digit.
 Remove the last digit from the original number using integer division (/ 10).
C Code:
#include <stdio.h>

int main() {
int num, reverse = 0;
printf("Enter a number: ");
scanf("%d", &num);

while (num != 0) {
reverse = reverse * 10 + num % 10;
num /= 10;
}

printf("Reversed number: %d\n", reverse);

return 0;
}

7. Array and Operations


What is an Array?
An array is a collection of elements of the same type stored in contiguous memory.
Logic:
 Read an array of integers from the user.
 Use a loop to calculate the sum of all elements.
 Compute the average by dividing the sum by the number of elements.
C Code:
#include <stdio.h>

int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);

int arr[n], sum = 0;


printf("Enter %d elements: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
sum += arr[i];
}

float avg = (float)sum / n;


printf("Sum: %d\n", sum);
printf("Average: %.2f\n", avg);

return 0;
}

You might also like