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

Presentation 1

The document provides a comprehensive overview of control structures in C programming, including if statements, loops, and switch cases. It includes example programs demonstrating the use of if, if-else, nested if-else, while loops, do-while loops, for loops, and the break and continue statements. Additionally, it discusses advanced topics like Armstrong numbers, Kaprekar numbers, and calculating LCM and GCD.

Uploaded by

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

Presentation 1

The document provides a comprehensive overview of control structures in C programming, including if statements, loops, and switch cases. It includes example programs demonstrating the use of if, if-else, nested if-else, while loops, do-while loops, for loops, and the break and continue statements. Additionally, it discusses advanced topics like Armstrong numbers, Kaprekar numbers, and calculating LCM and GCD.

Uploaded by

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

Control Structure in C

Programming
IF Statement

• The if in C is a decision-making statement that is used to execute a block


of code based on the value of the given expression.
Syntax for IF statement
Write a program in c to display number negative number, if user enter
positive number then won’t display

#include <stdio.h>

int main()
{
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (number < 0)
{
printf("You entered a negative number: %d\n", number);
}
return 0;
}
IF ELSE Statement
• The if-else statement is a decision-making statement
that is used to decide whether the part of the code
will be executed or not based on the specified
condition (test expression).
• If the given condition is true, then the code inside the
if block is executed, otherwise the code inside the
else block is executed.
Syntax for if-else
write a program in c to display even and odd number using if-else

#include <stdio.h>

int main()
{
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (number % 2 == 0)
{
printf("%d is an even number.\n", number);
}
else
{
printf("%d is an odd number.\n", number);
}
return 0;
}
Nested IF –ELSE
• Nested if else statements allow for more complex decision-making within the program. You
can nest if else statements with other if else statements, creating conditions at multiple
levels.

Syntax of Nested IF-ELSE


if (condition1) {
// Code block for condition1 being true
if (condition2) {
// Code block for condition1 and condition2
both being true
} else {
// Code block for condition1 being true and
condition2 being false
}
} else {
// Code block for condition1 being false
}
write a program to find postitive or negative number. if the number is positive also find whether the number is even or odd.
#include <stdio.h>

int main() {
int num = 10;

// Outer if-else statement to check if num is greater than 0


if (num > 0) {
printf("Number is positive.\n");

// Nested if-else statement to check if num is even or odd


if (num % 2 == 0) {
printf("Number is even.\n");
} else {
printf("Number is odd.\n");
}

} else {
// Execute if num is not greater than 0
printf("Number is non-positive.\n");
}

return 0;
}
Nested IF- ELSE-IF/IF-ELSE-IF ladder statement
if else if ladder in C programming is used to test a series of conditions sequentially. Furthermore,
if a condition is tested only when all previous if conditions in the if-else ladder are false. If any of
the conditional expressions evaluate to be true, the appropriate code block will be executed, and
the entire if-else ladder will be terminated.
Syntax – if-else-if ladder
if (condition1) {
// Code block executed if condition1 is true
}
else if (condition2) {
// Code block executed if condition1 is false and
// condition2 is true
}
else if (condition3) {
// Code block executed if condition1 and condition2
is
// false and condition3 is true
}
else {
// Code block executed if all conditions are false
}
// C program to illustrate if-else-if ladder statement
#include <stdio.h>

int main()
{
int i = 20;

if (i == 10)
printf("i is 10");
else if (i == 15)
printf("i is 15");
else if (i == 20)
printf("i is 20");
else
printf("i is not present");
}
• C program uses an if-else if ladder to determine and print the grade based on the given
marks.
Here's a breakdown of the logic:

• -if the marks < 0 and marks > 100 -program output “invalid marks”
• - If the `marks` are between 90 and 100 , the program outputs `"A+ Grade"`.
• - If the `marks` are between 80 and 89 , it outputs `"A Grade"`.
• - If the `marks` are between 70 and 79 , it outputs `"B Grade"`.
• - If the `marks` are between 60 and 69 , it outputs `"C Grade"`.
• - If the `marks` are between 50 and 59 , it outputs `"D Grade"`.
• - Any marks below 50 result in the program printing `"F Failed"`.

• Write c program that checks a single character username and an integer password.
Logic-
- If- Correct username (`'a'`) and if -correct password (`12345`)
- Output: `"Login successful"`
- else-Correct username (`'a'`) but incorrect password
- Output: `"Password is incorrect, Try again."`
- else-Incorrect username (anything other than `'a'`)
- Output: `"Username is incorrect, Try again."`
write a program in c to print grade of students
#include <stdio.h>

int main() printf("D Grade");


{ Else
int marks; {
printf("Enter the student's marks (0-100): "); printf("Grade: F\n");
scanf("%d", &marks); }
if (marks < 0 || marks > 100) }
{
printf("Invalid marks! Please enter marks between 0 and 100.\n"); return 0;
} }
else
{
int marks = 91;
if (marks <= 100 && marks >= 90)
printf("A+ Grade");
else if (marks < 90 && marks >= 80)
printf("A Grade");
else if (marks < 80 && marks >= 70)
printf("B Grade");
else if (marks < 70 && marks >= 60)
printf("C Grade");
Loops
-Loops used in c programming to repeat specific block until some end condition meet.
Entry Controlled loops: In Entry controlled loops the test condition is checked before entering the main body of the
loop. For Loop and While Loop is Entry-controlled loops.
Exit Controlled loops: In Exit controlled loops the test condition is evaluated at the end of the loop body. The loop body
will execute at least once, irrespective of whether the condition is true or false. do-while Loop is Exit Controlled loop.
While loop:
This loop can be used to iterate a part of code while the given condition remains
true.
Syntax-
Write a program to print numbers using while loop
#include <stdio.h> // C program to demonstrate while loop
#include <stdio.h>
int main()
{ int main()
int i = 0; {
while (i < 5) // Initialization of loop variable
{ int i = 0;
printf(“C Programming\n");
i++; // setting test expression as (i < 5), means the loop
} // will execute till i is less than 5
return 0; while (i < 5) {
}
// loop statements
printf(“C Programming\n");

// updating the loop variable


i++;
}
return 0;
}
Do While loop-
The do-while loop is similar to a while loop but the only difference lies in the do-while loop test condition
which is tested at the end of the body. In the do-while loop, the loop body will execute at least
once irrespective of the test condition.

Syntax
// C Program to demonstrate the use of do...while
loop
#include <stdio.h>

int main()
{

// loop variable declaration and initialization


int i = 0;
// do while loop
do {
printf(“C Program\n");
i++;
}
while (i < 3);

return 0;
}
FOR Loop
In C programming, loops are responsible for performing repetitive tasks using a short
code block that executes until the condition holds true. In this article, we will learn
about for loop in C.
Write a program to print 5 numbers:

#include <stdio.h> Output:


1
int main() 2
{ 3
4
for(int i = 1; i <= 5; i++) 5
{
printf("%d\n", i);
}
return 0;
}
Break statement-
The break in C is a loop control statement that breaks out of
the loop when encountered. It can be used inside loops or
switch statements to bring the control out of the block. The
break statement can only break out of a single loop at a
time
// C Program to demonstrate break statement with for loop // using break inside while loop to terminate after 2
#include <stdio.h>
#include <stdio.h> int main()
{
int main()
{ printf("\nbreak in while loop\n");
int i = 1;
printf("break in for loop\n"); while (i < 20) {
for (int i = 1; i < 5; i++) if (i == 3)
{ break;
if (i == 3) { else
break; printf("%d ", i);
} i++;
else { }
printf("%d ", i); return 0;
} }
}
continue statement-
The C continue statement resets program control to the beginning of the loop
when encountered. As a result, the current iteration of the loop gets skipped and
the control moves on to the next iteration. Statements after the continue
statement in the loop are not executed.
// C program to explain the use
// of continue statement with for loop #include <stdio.h>

#include <stdio.h> int main()


{
int main() int i = 0;
{ // while loop to print 1 to 8
// for loop to print 1 to 8 while (i < 8) {
for (int i = 1; i <= 8; i++) { // when i = 4, the iteration will be skipped and
// when i = 4, the iteration will be skipped and for for
// will not be printed // will not be printed
if (i == 4) { i++;
continue; if (i == 4) {
} continue;
printf("%d ", i); }
} printf("%d ", i);
printf("\n"); }
return 0;
}
Switch-
Switch case statement evaluates a given expression and based on
the evaluated value(matching a certain condition), it executes the
statements associated with it. Basically, it is used to perform
different actions based on different conditions(cases).
// C Program to demonstrate the behaviour of switch case
// without break
#include <stdio.h>

int main()
{

int var = 2;

// switch case without break


switch (var)
{
case 1:
printf("Case 1 is executed.\n");
case 2:
printf("Case 2 is executed.\n");
case 3:
printf("Case 3 is executed.");
case 4:
printf("Case 4 is executed.");
}
return 0;
}
// C program to print the day using switch case 5:
#include <stdio.h> printf("Friday");
break;
// Driver Code case 6:
int main() printf("Saturday");
{ break;
int day = 2; case 7:
printf("Sunday");
printf("The day with number %d is ", day); break;
switch (day) default:
{ printf("Invalid Input");
case 1: break;
printf("Monday"); }
break; return 0;
case 2: }
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
case 4:
printf("Thursday");
break;
A: Write a program to check whether a given number is Armstrong number or not.
For Example 371 is 3^3+7^3+1^3=371.

B: Write a program to convert a decimal number to binary or convert a binary number to decimal

C: Twin primes are consecutive odd numbers both of which are prime numbers. Write a program
which inputs two positive integers A and B and outputs all twin primes in range A to B.

D: Write a program to find out whether a number is kaprekar or not. Consider an n-digit number
k. Square it and add the right n digits to the left n or n-1 digits. If the resultant sum is k, then k is
called a Kaprekar number. For example, 9 is a Kaprekar number since
9^2=81 and 8+1=9
and 297 is a Kaprekar number since
297^2=88209 and 88+209=297
The first few are 1, 9, 45, 55, 99, 297, and 703.

E: Take two numbers as input and calculate their LCM and GCD (HCF).

F: Note that 12*42 = 21*24 and 12*63 = 21*36 and 12*84 = 21*48 and so on. There is a
property that (10a+b)*(10c+d) =(10b+a)(10d+c) where a and b are unequal and c and d are also
unequal. Write a program which outputs them all between 10 to 99.
A: Write a program to check whether a given number is Armstrong // Reset originalNumber to the input
number or not. number
For Example 371 is 3 originalNumber = number;
3+7
3+1 // Calculate the sum of each digit raised
3=371. to the power of numDigits
#include <stdio.h> while (originalNumber != 0) {
#include <math.h> remainder = originalNumber % 10;
int main() { sum += pow(remainder, numDigits);
int number, originalNumber, remainder, sum = 0, numDigits = 0; originalNumber /= 10;
}
// Input number from user
printf("Enter a number: "); // Check if the sum is equal to the
scanf("%d", &number); original number
if (sum == number) {
// Store the original number printf("%d is an Armstrong number.\
originalNumber = number; n", number);
} else {
// Calculate the number of digits printf("%d is not an Armstrong
while (originalNumber != 0) { number.\n", number);
originalNumber /= 10; }
numDigits++;
} return 0;
}
P2. Write a program to find out whether a number is kaprekar or not.
Consider an n-digit
number k. Square it and add the right n digits to the left n or n-1 digits.
If the resultant sum is
k, then k is called a Kaprekar number. For example, 9 is a Kaprekar
number since
9^2=81 and 8+1=9
and 297 is a Kaprekar number since
297^2=88209 and 88+209=297

The first few are 1, 9, 45, 55, 99, 297, and 703.
Algorithm for Finding a Kaprekar Number
Input: Read the number k.
Step 1: Calculate the square of the number k. Let square = k * k.
Step 2: Count the number of digits in k. Let n be the number of digits in
k.
Step 3: Split the square of k into two parts:
Right part: The last n digits of square.
Left part: The remaining digits of square (or 0 if there are no
remaining digits).
Step 4: Add the left part and the right part.
Step 5: If the sum equals k, then k is a Kaprekar number.
Output: Display whether k is a Kaprekar number.
Take two numbers as input and calculate their LCM and GCD
(HCF).

1. Input = num1,num2
2. Take 2 -temp variable to store num1 and num2
value
3. Find gcd
4. while()
remainder = num1 % num2; // Find remainder
num1 = num2; // Update num1 to num2
num2 = remainder; // Update num2 to remainder
5. lcm=(temp1*temp2)/gcd
6. output-Print the value of gcd and lcm
#include <stdio.h> gcd = num1; // The last non-zero remainder is
the GCD
int main() {
int num1, num2, gcd, lcm, temp1, temp2; // Calculate LCM using the formula: LCM(a,
b) = (a * b) / GCD(a, b)
// Input two positive integers lcm = (temp1 * temp2) / gcd;
printf("Enter two positive integers:\n");
scanf("%d %d", &num1, &num2); // Output the results
printf("GCD (HCF) of the two numbers: %d\
// Store the original values for later use n", gcd);
temp1 = num1; printf("LCM of the two numbers: %d\n",
temp2 = num2; lcm);

// Calculate GCD using Euclidean algorithm return 0;


while (num2 != 0) { }
int remainder = num1 % num2; // Find remainder
num1 = num2; // Update num1 to num2
num2 = remainder; // Update num2 to remainder
}
C program to generate the Fibonacci series:

n=7
0, 1, 1, 2, 3, 5, 8
#include <stdio.h>
// Loop to generate the
int main() { remaining terms
int n, i; for (i = 3; i <= n; ++i) {
int t1 = 0, t2 = 1, nextTerm; nextTerm = t1 + t2;
printf(", %d", nextTerm
// Taking input from user for the number of terms t1 = t2;
printf("Enter the number of terms: "); t2 = nextTerm;
scanf("%d", &n); }

// Printing the first two terms of the Fibonacci series printf("\n");


printf("Fibonacci Series: %d, %d", t1, t2); return 0;
}
*
**
***
****
*****
Write a program to print above pattern
#include <stdio.h>

int main() {
int rows, i, j;

// Asking the user for the number of rows


printf("Enter the number of rows: ");
scanf("%d", &rows);

// Loop to print the pattern


for (i = 1; i <= rows; ++i) {
for (j = 1; j <= i; ++j) {
printf("* ");
}
printf("\n");
}

return 0;
}
simple C program to print a number pattern up to 5

1
12
123
1234
12345
#include <stdio.h>

int main() {
int i, j;

// Outer loop for each row


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

return 0;

You might also like