KEMBAR78
Module 3 QB | PDF | Control Flow | Software Engineering
0% found this document useful (0 votes)
19 views12 pages

Module 3 QB

The document provides explanations and examples of various programming concepts in C, including loops (for, while, do-while), jump statements (break, continue, goto), conditional statements (if-else, nested if-else, switch-case), and functions to perform specific tasks like checking Armstrong numbers, calculating LCM, and classifying BMI. It also includes flowcharts for decision-making processes and examples of programs for specific functionalities. Overall, it serves as a guide for understanding control flow and decision-making in C programming.

Uploaded by

kashishgupta9892
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)
19 views12 pages

Module 3 QB

The document provides explanations and examples of various programming concepts in C, including loops (for, while, do-while), jump statements (break, continue, goto), conditional statements (if-else, nested if-else, switch-case), and functions to perform specific tasks like checking Armstrong numbers, calculating LCM, and classifying BMI. It also includes flowcharts for decision-making processes and examples of programs for specific functionalities. Overall, it serves as a guide for understanding control flow and decision-making in C programming.

Uploaded by

kashishgupta9892
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/ 12

Q43. (2 Marks) Explain when to use For loop and while loop.

Answer:

• For loop: Use when the number of iterations is known.

• While loop: Use when the number of iterations is not known and depends on a
condition.

Q44. (2 Marks) List the keywords associated with jump statements in C.

Answer:
The jump statements in C are:

1. break

2. continue

3. goto

4. return

Q45. (2 Marks) Explain the difference between for and while loop.

Answer:

• For loop: Typically used when the number of iterations is known beforehand.

• While loop: Used when the number of iterations is not known and is determined
based on a condition.

Q46. (2 Marks) Illustrate the use of goto statement with suitable example.

Answer:

#include <stdio.h>

int main() {

int i = 1;

start:

if(i <= 5) {
printf("%d ", i);

i++;

goto start; // Goto statement to repeat the loop

return 0;

Q47. (2 Marks) Differentiate between if else and Nested if else.

Answer:

• if-else: Executes one block of code if a condition is true, otherwise executes


another block.

• Nested if-else: An if-else inside another if-else to handle multiple conditions.

Q48. (2 Marks) State the general flowchart for Do-while loop.

Answer:
Flowchart:

• Start → Execute Body → Check Condition → If True, repeat → Else, End.

• The body of the loop is executed at least once.

Q49. (2 Marks) Write Break and Continue statements with example.

Answer:

for (int i = 1; i <= 10; i++) {

if (i == 5) continue; // Skips 5

if (i == 8) break; // Exits loop at 8

printf("%d ", i);

}
Q50. (5 Marks) Explain the flowchart of two way and multiway decision.

Answer:

• Two-way decision: A single condition is tested, leading to one of two possible paths
(yes/no).

• Multi-way decision: Multiple conditions are tested, and the program can choose
from several paths.

o Flowchart: Start → Condition 1 → True/False → If False, Check Condition 2 →


Continue this for multiple conditions.

Q51. (5 Marks) Write a program to check the entered number is Armstrong or not.

Answer:

#include <stdio.h>

#include <math.h>

int main() {

int num, sum = 0, remainder, temp, n = 0;

printf("Enter an integer: ");

scanf("%d", &num);

temp = num;

while (temp != 0) {

temp /= 10;

++n;

temp = num;

while (temp != 0) {

remainder = temp % 10;

sum += pow(remainder, n);


temp /= 10;

if (sum == num)

printf("%d is an Armstrong number.", num);

else

printf("%d is not an Armstrong number.", num);

return 0;

Q52. (5 Marks) Write program to Print multiplication table using do while loop.

Answer:

#include <stdio.h>

int main() {

int num, i = 1;

printf("Enter a number: ");

scanf("%d", &num);

do {

printf("%d * %d = %d\n", num, i, num * i);

i++;

} while (i <= 10);

return 0;

Q53. (5 Marks) Write a program to find LCM of two numbers.

Answer:
#include <stdio.h>

int main() {

int num1, num2, lcm;

printf("Enter two numbers: ");

scanf("%d %d", &num1, &num2);

lcm = (num1 > num2) ? num1 : num2;

while (1) {

if (lcm % num1 == 0 && lcm % num2 == 0) {

printf("LCM of %d and %d is %d", num1, num2, lcm);

break;

lcm++;

return 0;

Q54. (5 Marks) Differentiate between for loop and while loop.

Answer:

Feature For Loop While Loop

Initialization Done once, before the loop starts. Done inside the loop or before loop.

Condition Checked before each iteration. Checked before each iteration.

Automatically updated after each Can be manually updated inside the


Iteration
iteration. loop.

Q55. (5 Marks) Explain Else if Ladder with the help of suitable example.
Answer:

#include <stdio.h>

int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

if (num > 0)

printf("Positive number");

else if (num < 0)

printf("Negative number");

else

printf("Zero");

return 0;

Q56. (5 Marks) Illustrate Infinite loop and Empty loop.

Answer:

• Infinite Loop: A loop that runs indefinitely unless stopped manually.

while (1) { // Infinite loop

printf("This loop runs forever.\n");

• Empty Loop: A loop with no statements inside.

for (int i = 0; i < 10; i++) { } // Empty loop


Q57. (10 Marks) Write a program to classify a person’s BMI into categories
(Underweight, Normal, Overweight, Obese) using a multiway if-else ladder.

Answer:

#include <stdio.h>

int main() {

float weight, height, bmi;

printf("Enter weight (kg): ");

scanf("%f", &weight);

printf("Enter height (m): ");

scanf("%f", &height);

bmi = weight / (height * height);

if (bmi < 18.5)

printf("Underweight\n");

else if (bmi >= 18.5 && bmi < 24.9)

printf("Normal weight\n");

else if (bmi >= 25 && bmi < 29.9)

printf("Overweight\n");

else

printf("Obese\n");

return 0;

Q58. (10 Marks) Explain Nested if Structure with example.


Answer:

#include <stdio.h>

int main() {

int age;

printf("Enter your age: ");

scanf("%d", &age);

if (age >= 18) {

if (age < 60)

printf("You are an adult.");

else

printf("You are a senior citizen.");

} else {

printf("You are a minor.");

return 0;

Q59. (10 Marks) Write C program to print all leap years.

Answer:

#include <stdio.h>

int main() {

int year;
printf("Enter 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;

Q60. (10 Marks) Illustrate Switch case with a menu driven program.

Answer:

#include <stdio.h>

int main() {

int choice;

printf("Menu:\n1. Add\n2. Subtract\n3. Multiply\n4. Divide\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1: printf("Addition selected.\n"); break;

case 2: printf("Subtraction selected.\n"); break;

case 3: printf("Multiplication selected.\n"); break;

case 4: printf("Division selected.\n"); break;

default: printf("Invalid choice.\n"); break;

return 0;
}

Q61. (10 Marks) Write a program for printing odd numbers between 1 to 50. Draw
Flowchart for the same.

Answer:

#include <stdio.h>

int main() {

for (int i = 1; i <= 50; i += 2) {

printf("%d ", i);

return 0;

Q62. (10 Marks) Write a program to calculate the percentage of students and find
grade.

Answer:

#include <stdio.h>

int main() {

int marks[5], total = 0;

printf("Enter marks of 5 subjects: ");

for (int i = 0; i < 5; i++) {

scanf("%d", &marks[i]);

total += marks[i];

float percentage = (total / 500.0) * 100;


printf("Percentage: %.2f\n", percentage);

if (percentage >= 90)

printf("Grade: A");

else if (percentage >= 75)

printf("Grade: B");

else if (percentage >= 60)

printf("Grade: C");

else

printf("Grade: D");

return 0;

Q63. (10 Marks) Write C program to check whether entered number is positive,
Negative or Zero.

Answer:

#include <stdio.h>

int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

if (num > 0)

printf("Positive number\n");

else if (num < 0)


printf("Negative number\n");

else

printf("Zero\n");

return 0;

You might also like