0% found this document useful (0 votes)
5 views

Q.1 Explain Simple if Statement With Syntax. Draw the Flowchart and…

The document explains various control flow statements in C programming, including the simple if statement, if-else statement, else-if ladder, nested if-else, switch statement, while loop, do-while loop, and for loop. Each section provides syntax, examples, and flowcharts to illustrate how these statements work. Additionally, it compares the break and continue statements, highlighting their distinct purposes in loop control.

Uploaded by

shravansirvi61
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Q.1 Explain Simple if Statement With Syntax. Draw the Flowchart and…

The document explains various control flow statements in C programming, including the simple if statement, if-else statement, else-if ladder, nested if-else, switch statement, while loop, do-while loop, and for loop. Each section provides syntax, examples, and flowcharts to illustrate how these statements work. Additionally, it compares the break and continue statements, highlighting their distinct purposes in loop control.

Uploaded by

shravansirvi61
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Q.1 Explain Simple if statement with syntax.

Draw the flowchart and illustrate it with the help of


suitable example

Simple if Statement in C

The if statement in C is used to execute a block of code only if a specified condition is true. If
the condition is false, the code inside the if block is skipped, and the program continues with the
rest of the code.

Syntax of if Statement:

if (condition) {
// block of code to be executed if the condition is true
}

Example of Simple if Statement:

#include <stdio.h>

int main() {
int number = 10;

// Checking if the number is positive


if (number > 0) {
printf("The number is positive.\n");
}

return 0;
}

Example of Simple if Statement:

#include <stdio.h>

int main() {
int number = 10;

// Checking if the number is positive


if (number > 0) {
printf("The number is positive.\n");
}
return 0;
}

Example with False Condition:

Here’s how the code would look with a false condition:

#include <stdio.h>

int main() {
int number = -5;

// Checking if the number is positive


if (number > 0) {
printf("The number is positive.\n");
} else {
printf("The number is not positive.\n");
}

return 0;
}

Q.2 Explain If-Else statement with syntax. Draw the flowchart and illustrate it with the help of
suitable example

If-Else Statement in C

The if-else statement is a decision-making structure in C programming that


allows you to execute one block of code if a condition is true and another block if
the condition is false.

This is useful when you need to perform one operation when a condition is met,
and a different operation when it is not.

Syntax of If-Else Statement:

if (condition) {
// Block of code to be executed if the condition is true
} else {
// Block of code to be executed if the condition is false
Example of If-Else Statement:

#include <stdio.h>

int main() {
int number = -5;

// Check if the number is positive or negative


if (number > 0) {
printf("The number is positive.\n");
} else {
printf("The number is negative or zero.\n");
}

return 0;
}

Explanation:

• The program checks if number > 0.


• If true, it prints "The number is positive.".
• If false (e.g., when number = -5), it prints "The number is negative or
zero.".

Flowchart for If-Else Statement

A flowchart visually represents the decision-making process of the if-else statement.

+----------------------------+
| Start |
+----------------------------+
|
v
+----------------------------+
| Evaluate condition (e.g., |
| number > 0) |
+----------------------------+
|
+------------+------------+
| |
Yes No
| |
+-------------+ +-------------+
| Execute | | Execute |
| if block | | else block |
+-------------+ +-------------+
| |
v v
+-----------------+ +-----------------+
| End | | End |
+-----------------+ +-----------------+
Example of If-Else Statement

#include <stdio.h>

int main() {
int age = 18;

// Check if the person is eligible to vote


if (age >= 18) {
printf("You are eligible to vote.\n");
} else {
printf("You are not eligible to vote.\n");
}

return 0;
}

Q.3 Explain Else-If Ladder with syntax. Draw the flowchart and illustrate it with the help of
suitable example.

Else-If Ladder in C

The else-if ladder is a control structure in C that is used to check multiple


conditions. It is an extension of the if-else statement, where more than two
possible outcomes are handled. This structure allows for multiple conditions to
be evaluated sequentially.

Syntax of Else-If Ladder:

if (condition1) {
// Block of code to be executed if condition1 is true
} else if (condition2) {
// Block of code to be executed if condition2 is true
} else if (condition3) {
// Block of code to be executed if condition3 is true
} else {
// Block of code to be executed if none of the above conditions are true
}

• if: The first condition is checked.


• else if: If the first condition is false, the program checks the next condition.
You can have as many else if conditions as needed.
• else: If all conditions fail (i.e., are false), the block of code inside the else is
executed. This is optional.

Example of Else-If Ladder:

#include <stdio.h>

int main() {
int marks = 75;

// Using else-if ladder to categorize grades


if (marks >= 90) {
printf("Grade: A\n");
} else if (marks >= 75) {
printf("Grade: B\n");
} else if (marks >= 50) {
printf("Grade: C\n");
} else if (marks >= 35) {
printf("Grade: D\n");
} else {
printf("Grade: F\n");
}

return 0;

Flowchart for Else-If Ladder

The flowchart illustrates how conditions are checked sequentially in an else-if ladder.

+-----------------------------+
| Start |
+-----------------------------+
|
v
+-----------------------------+
| Evaluate condition1 (e.g., |
| marks >= 90) |
+-----------------------------+
|
+----------+----------+
| |
Yes No
| |
+-----------------+ +-----------------+
| Execute block 1 | | Evaluate condition2 |
+-----------------+ +-----------------+
|
+----------+----------+
| |
Yes No
| |
+-----------------+ +-----------------+
| Execute block 2 | | Evaluate condition3 |
+-----------------+ +-----------------+
|
+----------+----------+
| |
Yes No
| |
+-----------------+ +-----------------+
| Execute block 3 | | Evaluate condition

Q.4 Explain Nested If-Else with syntax. Draw the flowchart and illustrate it with the help of
suitable example.

Nested If-Else Statement in C

A nested if-else statement occurs when an if or else block contains another if or


else block. This allows for multiple levels of decision-making and is useful when
there are more complex conditions to evaluate.

Syntax of Nested If-Else:

if (condition1) {
// Code block executed if condition1 is true
if (condition2) {
// Code block executed if condition2 is true
} else {
// Code block executed if condition2 is false
}
} else {
// Code block executed if condition1 is false
}

• First if: The outer condition is evaluated first.


• Second if (nested): If the first condition is true, the second if is evaluated. If
false, the else block corresponding to the second if is executed.
• The structure can be extended to multiple levels of nested conditions.

Example of Nested If-Else:

#include <stdio.h>

int main() {
int age = 25;
int salary = 35000;

// Nested if-else to check eligibility for loan


if (age >= 18) {
if (salary >= 30000) {
printf("Eligible for loan.\n");
} else {
printf("Salary is insufficient for loan.\n");
}
} else {
printf("Not eligible for loan due to age.\n");
}

return 0;
}
Flowchart for Nested If-Else Statement

The flowchart illustrates how nested conditions are evaluated in the nested if-else structure:

+-------------------------------+
| Start |
+-------------------------------+
|
v
+-------------------------------+
| Is age >= 18? |
+-------------------------------+
|
+----------+----------+
| |
Yes No
| |
+----------------+ +----------------------+
| Is salary >= 30k? | | Print: "Not eligible |
+----------------+ | for loan due to age" |
| +----------------------+
+------------+----------+
| |
Yes No
| |
+-------------------+ +-------------------------------+
| Print: "Eligible | | Print: "Salary insufficient |
| for loan" | | for loan" |
+-------------------+ +-------------------------------+
|
v
+----------------+
| End |
+----------------+

Another Example of Nested If-Else

#include <stdio.h>

int main() {
int number = 15;

// Nested if-else to check divisibility


if (number % 2 == 0) {
if (number % 5 == 0) {
printf("Number is divisible by both 2 and 5.\n");
} else {
printf("Number is divisible by 2 but not by 5.\n");
}
} else {
printf("Number is not divisible by 2.\n");
}

return 0;
}

Q.5 Explain Switch statement with syntax. Draw the flowchart and illustrate it with the help of
suitable example

Switch Statement in C

The switch statement in C is a control flow structure that allows you to execute
one out of multiple possible blocks of code based on the value of a variable. It is
an alternative to using many if-else-if statements, especially when there are
many conditions to check based on a single variable.

Syntax of Switch Statement:

switch (expression) {
case constant1:
// Block of code executed if expression == constant1
break;
case constant2:
// Block of code executed if expression == constant2
break;
case constant3:
// Block of code executed if expression == constant3
break;
default:
// Block of code executed if no case matches

Example of Switch Statement:

#include <stdio.h>

int main() {
int day = 3;

// Switch statement to print the name of the day based on its number
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
case 6:
printf("Saturday\n");
break;
case 7:
printf("Sunday\n");
break;
default:
printf("Invalid day\n");
}

return 0; }
Flowchart for Switch Statement

The flowchart below illustrates how the switch statement works:

+-----------------------------+
| Start |
+-----------------------------+
|
v
+-----------------------------+
| Evaluate expression |
+-----------------------------+
|
v
+-----------------------------+
| Is expression == case 1? |
+-----------------------------+
|
+--------+--------+
| |
Yes No
| |
+----------------+ +----------------+
| Execute case 1 | | Is expression |
| block | | == case 2? |
+----------------+ +----------------+
| |
v v
+----------------+ +----------------+
| Break | | Execute case 2 |
+----------------+ | block |
| +----------------+
v |
+-----------------------------+
| End |
+-----------------------------+

Another Example of Switch Statement

#include <stdio.h>

int main() {
int num = 2;

// Switch statement to check if a number is odd or even


switch (num % 2) {
case 0:
printf("Even number\n");
break;
case 1:
printf("Odd number\n");
break;
default:
printf("Invalid input\n");
}
return 0;
}

Q.8 Explain while loop with syntax and illustrate it using a suitable example.

While Loop in C

A while loop in C is a control flow statement that repeatedly executes a block of code as long as
a given condition evaluates to true. The condition is checked before the execution of the loop
body, making it a pre-test loop.

Syntax of while Loop:

while (condition) {
// Code to be executed as long as the condition is true
}

• condition: An expression that is evaluated before each iteration. If the


condition is true, the body of the loop is executed.
• The loop continues to execute as long as the condition remains true.
• If the condition is false at the beginning (or at any point during the loop), the
loop is not executed, and the program moves on to the next statement after the
loop.

Example of while Loop:

#include <stdio.h>

int main() {
int count = 1;

// While loop to print numbers from 1 to 5


while (count <= 5) {
printf("%d\n", count);
count++; // Increment the count
}

return 0;
}

Q.9 Explain do-while with syntax and illustrate it using a suitable example.
do-while Loop in C

The do-while loop in C is similar to the while loop but with one key difference: in a do-while loop,
the condition is checked after the loop body is executed. This guarantees that the loop body
will be executed at least once, even if the condition is false initially.

Syntax of do-while Loop:

do {
// Code to be executed
} while (condition);

• Code Block: The block of code inside the do section will execute first,
regardless of the condition.
• condition: The condition is evaluated after the code block has been
executed. If the condition is true, the loop will repeat. If the condition is false, the
loop will terminate.
• Semicolon: The do-while loop ends with a semicolon (;) after the while
condition.

Example of do-while Loop:

#include <stdio.h>

int main() {
int count = 1;

// Using do-while loop to print numbers from 1 to 5


do {
printf("%d\n", count);
count++; // Increment the count
} while (count <= 5);

return 0;
}

Q.11 Explain syntax of for loop and illustrate it using suitable example.

for Loop in C

The for loop in C is a control flow statement that allows code to be repeatedly executed based
on a given condition. It is often used when the number of iterations is known in advance or when
you need to iterate over a range of values.

Syntax of for Loop:

for (initialization; condition; increment/decrement) {


// Code to be executed in each iteration
}

• Initialization: This step is executed only once, at the beginning of the loop. It
is typically used to initialize the loop control variable.
• Condition: The condition is evaluated before every iteration. If the condition
is true, the loop body is executed. If it is false, the loop terminates.
• Increment/Decrement: This step is executed after each iteration of the loop.
It is used to update the loop control variable, usually incrementing or decrementing
it.

Example of for Loop:

#include <stdio.h>

int main() {
// Using a for loop to print numbers from 1 to 5
for (int i = 1; i <= 5; i++) {
printf("%d\n", i);
}

return 0;
}

Q.14 Compare the break and continue statements, providing examples to illustrate their
differences.

in C

Both break and continue are control flow statements used in loops and switch statements, but
they have distinct purposes and behaviors.

break Statement:

• The break statement is used to exit the loop (or switch statement) entirely,
regardless of the loop condition.
• Once the break is encountered, the control is transferred to the first
statement after the loop or switch.

continue Statement:

• The continue statement is used to skip the remaining part of the current
iteration in a loop and proceed to the next iteration.
• When continue is encountered, the loop condition is checked again, and if it
is true, the next iteration of the loop starts.

Example of break Statement:

#include <stdio.h>

int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break; // Exit the loop when i is 3
}
printf("%d\n", i); // This will print 1 and 2
}

printf("Loop terminated.\n");
return 0;
}

Output:

1
2
Loop terminated.

Example of continue Statement:

#include <stdio.h>

int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue; // Skip the rest of the loop when i is 3
}
printf("%d\n", i); // This will print all numbers except 3
}
Return 0;
}

Output:

1
2
4
5

Q.16 Write a C Program using for loop to calculate factorial of a number

#include <stdio.h>

int main() {
int num, factorial = 1;

// Input from user


printf("Enter a positive integer: ");
scanf("%d", &num);

// Check if the number is negative, as factorial is not defined for negative numbers
if (num < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
// Calculate factorial using for loop
for (int i = 1; i <= num; i++) {
factorial *= i; // Multiply factorial by current number
}
// Output the result
printf("Factorial of %d is %d\n", num, factorial);
}

return 0;
}

Q.17 Write a C Program using Nested for loop to display the


following pattern.
*
**
***
****
*****

#include <stdio.h>

int main() {
int i, j;

// Outer loop for rows


for (i = 1; i <= 5; i++) {
// Inner loop for printing stars in each row
for (j = 1; j <= i; j++) {
printf("*");
}
// Move to the next line after printing each row
printf("\n");
}

return 0;
}

Q.118 Write a C Program using swich case operation to generate calendar.

#include <stdio.h>

int main() {
int month, year;

// Asking for user input for month and year


printf("Enter the month (1-12): ");
scanf("%d", &month);
printf("Enter the year: ");
scanf("%d", &year);

// Using switch-case to handle different months


switch (month) {
case 1: // January
case 3: // March
case 5: // May
case 7: // July
case 8: // August
case 10: // October
case 12: // December
printf("Month %d has 31 days.\n", month);
break;

case 4: // April
case 6: // June
case 9: // September
case 11: // November
printf("Month %d has 30 days.\n", month);
break;

case 2: // February
// Checking for leap year for February
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
printf("February %d has 29 days (Leap Year).\n", year);
} else {
printf("February %d has 28 days.\n", year);
}
break;

default:
printf("Invalid month entered. Please enter a month between 1 and 12.\n");
}

return 0;
}

You might also like