PPS Mod-2
PPS Mod-2
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
if (number % 2 == 0) {
printf("%d is even.\n", number);
}
}
if else condition:
We have to give an if condition.
if the condition is TRUE, then the statements inside if block will execute.
If it is FALSE, the statements inside else block will execute.
Syntax:
…
if(condition) {
statements;
}
else {
statements;
}
…
Eg: Even or odd
Largest of two numbers
#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
if (age >= 18) {
printf("You are eligible to vote.\n");
} else
{
printf("You are not eligible to vote.\n");
}
return 0;
}
else if ladder :
• The else if ladder statement executes one condition from multiple statements.
• The execution starts from top and checked for each if condition.
• The statement of if block is executed first. If the condition is TRUE, the statements in if block is executed first.
• If it is FALSE, next else if condition is executed.
Syntax :
if (condition) {
statements;
}
else if (condition) {
statements;
}
else if (condition) {
statements;
}
.
.
else {
statements;
}
#include<stdio.h>
void main()
{
int marks;
printf("Enter your marks ");
scanf("%d",&marks);
if(marks<0 || marks>100)
{
printf("Invalid Entry,the marks must be between 0-100");
}
else if(marks<50)
{
printf("Grade F");
}
else if(marks>=50 && marks<60)
{
printf("Grade D");
}
else if(marks>=60 && marks<70)
{
printf("Grade C");
}
else if(marks>=70 && marks<80)
{
printf("Grade B");
}
else if(marks>=80 && marks<90)
{
printf("Grade A");
}
else
{
printf("Grade A+");
}
}
Nested if statement:
• An if statement within another if statement is known as Nested if statement.
• Syntax:
if (condition) {
if (condition) {
statements;
}
else {
statements;
}
}
else {
statements;
}
Eg:Largest of three numbers
//nested if-else
#include <stdio.h>
int main()
{
int A, B, C;
if (A >= B) {
if (A >= C)
printf("%d is the largest number.", A);
else
printf("%d is the largest number.", C);
}
else {
if (B >= C)
printf("%d is the largest number.", B);
else
printf("%d is the largest number.", C);
}
return 0;
}
switch statement :
• A switch statement is a type of selection, used to allow the value of a variable to change the control flow of program
execution.
• It allows to choose only one choice among many choices.
• It consists of optional default statement.
• break keyword is used inside cases to terminate from switch.
Syntax :
switch (expression) {
case label 1: statements;
break;
case label 2: statements;
break;
.
.
case label n: statements;
break;
default : statements;
break;
}
#include <stdio.h>
int main()
{
char ch;
/* Switch value of ch */
switch(ch)
{
case 'a':
printf("Vowel");
break;
case 'e':
printf("Vowel");
break;
case 'i':
printf("Vowel");
break;
case 'o':
printf("Vowel");
break;
case 'u':
printf("Vowel");
break;
default:
printf("Consonant");
}
return 0;
}
#include <stdio.h>
int main() {
int choice;
printf("Welcome to Our Restaurant!\n");
printf("Menu:\n");
printf("1. Pizza - $100.00\n");
printf("2. Burger - $50.00\n");
printf("3. Pasta - $40.00\n");
printf("4. Salad - $99.00\n");
printf("5. Exit\n");
printf("Please enter your choice (1-5): ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("You have selected Pizza. Price: $100.00\n");
break;
case 2:
printf("You have selected Burger. Price: $50.00\n");
break;
case 3:
printf("You have selected Pasta. Price: $40.00\n");
break;
case 4:
printf("You have selected Salad. Price: $99.00\n");
break;
case 5:
printf("Thank you for dining with us. Goodbye!\n");
break;
default:
printf("Invalid choice. Please select a valid option.\n");
}
return 0;
}
Iterative statements or looping
statements
An iterative statement repeatedly executes some statements which are
inside the loop until the control statement fails.
These are of 3 types
1. while loop
2. do – while loop
3. for loop
There are mainly two types of loops in C Programming:
• 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 :
It is also known as entry controlled loop.
• Syntax :
while (condition) {
statements / body of loop; Repeat
}
• First the while condition is evaluated.
• If it is TRUE, then the body of while loop will execute. After that the control again
transfer to while condition and once again the while condition is evaluated.
• This process repeats until while condition is FALSE.
• If it is FALSE, the statements after while block will execute.
• To repeat the loop we have to give an updating statement, otherwise it will be an
infinite loop.
• Infinite while loop: if the while condition is always true, then the loop will execute
forever.
//Natural numbers
n=100,i=0
while(i<=n)
{
printf("%d It is a natural number,i");
i++;
}
//Reversing the numbers:
#include <stdio.h>
int main(){
int i=100;
while(i>=0)
{
printf("%d\n",i);
i--;
}
}
Sum of N numbers:
#include <stdio.h>
int main() {
int n, i=1, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
while (i <= n) {
sum =sum + i;
++i;
}
while (i <= n) {
product = product * i;
i++;
}
return 0;
}
Example:Amstrong number.
The sum of the digits of the cubes of the given number is equal to the given number is known as
amstrong number.
#include<stdio.h>
int main() {
int num, ams, rem, sum = 0;
printf("Enter a three-digit integer: ");
scanf("%d", &num);
ams = num;
while (num!= 0) {
// remainder contains the last digit
rem = num % 10;
sum =sum + rem * rem * rem;
// removing last digit from the orignal number
num=num/10;
}
if (sum == ams)
printf("%d is an Armstrong number.", ams);
else
printf("%d is not an Armstrong number.", ams);
return 0;
}
A palindrome number is a number that remains the same when
digits are reversed.
#include<stdio.h>
main()
{
int num,rem,rev=0,temp;
clrscr();
printf("enter the number");
scanf("%d",&num);
temp=num;
while(temp>0)
{
rem=temp%10;
rev=rev*10+rem;
temp=temp/10;
}
if(num==rev)
printf("given number is palindrome number");
else
printf("given number is not palindrome number");
getch();
}
do – while loop :
• Usually for and while loop checks the condition at the starting of the loop. But the do – while
loop checks the condition at the ending (bottom) of the loop.
• So it is also known as Exit controlled statement.
• do – while loop will execute at least one time.
• Syntax :
….
do {
statements / body of loop;
} while (condition);
• First the statements inside do while loop will execute, after that the while condition is
evaluated. If the while condition is TRUE, then the body will execute otherwise the statements
after the loop will execute.
• To repeat the loop we have to give an updating statement, otherwise it will be an infinite loop.
• Infinite do – while loop : if the do - while condition is always true, then the loop will execute
forever.
//Natural Numbers.
#include<stdio.h>
int main(){
int i=1;
do{
printf("%d \n",i);
i++;
}while(i<=10);
return 0;
}
//print table for the given number
#include<stdio.h>
int main(){
int i=1,number;
printf("Enter a number: ");
scanf("%d",&number);
do{
printf("%d \n",(number*i));
i++;
}while(i<=10);
return 0;
}
for loop :
• Syntax :
for ( initialization ; condition ; increment / decrement ) {
statements / body of the loop;
}
• The initialization statement is executed only once.
• Then the condition is evaluated. If it is FALSE, the loop for loop will terminate.
• If it is TRUE, then body of loop will execute. Then it will go to increment /
decrement.
• Again the loop will repeat until the condition is FALSE.
• Infinite for loop : if the condition in for loop is always true, then the loop will
execute forever.
Difference between loops:
//Factorial Program
#include<stdio.h>
int main()
{
int i,fact=1,num;
printf("Enter a number: ");
scanf("%d",&num);
for(i=1;i<=num;i++){
fact=fact*i;
}
printf("Factorial of %d is %d",num,fact);
return 0;
}
//Fibonacci Series
#include<stdio.h>
int main()
{
int f1=0,f2=1,f3,i,number;
printf("Enter the number of elements:");
scanf("%d",&number);
printf("\n%d %d",f1,f2);
for(i=2;i<number;++i)
{
f3=f1+f2;
printf(" %d",f3);
f1=f2;
f2=f3;
}
return 0;
}
Nesting of loop
When a loop written inside the body of another loop then, it is known as nesting of loop. Any type of loop
can be nested in any type such as while, do while, for. For example nesting of for loop can be represented as
void main()
{
int i,j;
for(i=0;i<2;i++)
● Modularity
● Clarity
● Parallel Development
● Scalability
Introduction to problem solving
Algorithms are step-by-step sets of instructions or rules for solving a particular problem or
performing a specific task. They are fundamental in computer science and mathematics and play
a crucial role in various fields, including computer programming, data analysis, artificial
intelligence, and more. Here are some key points to understand about algorithms:
Purpose: Algorithms are designed to solve specific problems or accomplish particular tasks
efficiently and effectively. They can be used to perform calculations, sort data, search for
information, make decisions, and much more.
Steps: Algorithms consist of a finite sequence of well-defined steps or instructions. Each step
is clear and unambiguous, with a specific purpose or operation.
Input and Output: Algorithms take some input, process it according to the defined steps, and
produce an output. The input could be data, variables, or other information relevant to the
problem being solved.
Efficiency: One of the key considerations in designing algorithms is
efficiency. An efficient algorithm should use as few resources (such as
time and memory) as possible to produce the desired output. This is
particularly important in computer science and related fields.
Program Verification:
Program verification is the process of ensuring that a program or software system behaves as
intended, meets its specifications, and does not have any critical bugs or errors. This is crucial
for building reliable and safe software.