Presentation 1
Presentation 1
Programming
IF Statement
#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.
int main() {
int num = 10;
} 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>
Syntax
// C Program to demonstrate the use of do...while
loop
#include <stdio.h>
int main()
{
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:
int main()
{
int var = 2;
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);
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); }
int main() {
int rows, i, j;
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;
return 0;