CP Lab
CP Lab
BASICS:
2.Main Function: Every C program must have a main() function, where the
program execution begins.
4.Executable Statements: This is where the actual logic of the program resides. It
consists of statements that perform tasks like calculations, input/output
operations, and control flow.
5.Return Statement: The return statement at the end of the main() function
indicates the successful termination of the program. Typically, return 0; is used to
indicate successful execution.
a) AIM: Write a program to find the area of the rectangle, Circle and
Surface area of a Cylinder.
ALGORITHM:
PROGRAM:
#include<stdio.h>
int main()
{
float length,breadth,radius,cylinder_radius,height,area_rectangle,area_circle,
area_cylinder;
printf("Enter length and breadth of rectangle:");
scanf("%f %f",&length,&breadth);
area_rectangle=length*breadth;
printf("\nArea of rectangle is:%.2f",area_rectangle);
printf("\nEnter radius of circle:");
scanf("%f",&radius);
area_circle=3.14*radius*radius;
printf("\nArea of circle is:%.2f",area_circle);
printf("\nEnter radius and height of cylinder:");
scanf("%f %f",&cylinder_radius,&height);
area_cylinder=(2*3.14*cylinder_radius*height)
+(2*3.13*cylinder_radius*cylinder_radius);
printf("\nSurface area of cylinder is:%.2f",cylinder);
return 0;
}
OUTPUT:
ALGORITHM:
PROGRAM:
#include<stdio.h>
int main()
int num;
printf("Enter a number:\n");
scanf("%i",&num);
if(num%2==0)
else
printf("The number is odd");
return 0;
}
OUTPUT:
Enter a number:
19
The number is odd
ALGORITHM:
INPUT: Two numbers
OUTPUT: Display the two numbers after swapping
STEP-1: Start
STEP-2: Read 2 numbers as a and b
STEP-3: Print a and b before swapping
STEP-4: temp=a
STEP-5: a=b
STEP-6: b=temp
STEP-7: Print a and b after swapping
STEP-8: Stop
PROGRAM:
#include<stdio.h>
int main()
int a,b,temp;
scanf("%i",&a);
scanf("%i",&b);
printf("The two numbers before swapping are:");
printf("%i",a);
printf(",");
printf("%i",b);
temp=a;
a=b;
b=temp;
printf("%i",a);
printf(",");
printf("%i",b);
}
OUTPUT:
7 16
ALGORITHM:
INPUT: Two numbers
OUTPUT: Display the two numbers after swapping
STEP-1:Start
STEP-2: Read two numbers as a and b
STEP-3: Display a and b before swapping
STEP-4: a=a+b
STEP-5: b=a-b
STEP-6: a=a-b
STEP-7: Display a and b after swapping
STEP-8: Stop
PROGRAM:
#include<stdio.h>
int main()
int a,b;
scanf("%i",&a);
scanf("%i",&b);
printf("%i",a);
printf(",");
printf("%i",b);
a=a+b;
b=a-b;
a=a-b;
printf("%i",a);
printf(",");
printf("%i",b);
}
OUTPUT:
48
The two numbers before swapping are:4,8
The two numbers after swapping are:8,4
2. SELECTION:
THEORY:
Selection statements in C are fundamental constructs that allow you to control the
flow of your program based on certain conditions. There are three main types of
selection statements in C: if, else if, and else.
1.if statement:
The if statement evaluates a condition and executes a block of code if the condition
is true.
If the condition is true, the code inside the curly braces {} is executed. If the
condition is false, the code block is skipped, and program execution continues after
the if statement.
2.else if statement:
The else if statement allows you to check additional conditions if the preceding if
condition is false. You can have multiple else if blocks to check different conditions.
3.else statement:
The else statement is optional and is used to execute a block of code if none of the
preceding conditions are true. It must be used after an if or else if statement.
If none of the preceding conditions are true, the code inside the else block is
executed.
These selection statements allow you to make decisions and perform different
actions based on different situations in your program, making your code more
dynamic and responsive.
a) AIM: WAP to accept three numbers and display the greatest number.
ALGORITHM:
PROGRAM:
#include<stdio.h>
int main()
int a,b,c;
scanf("%i",&a);
scanf("%i",&b);
scanf("%i",&c);
printf("\n %d is greatest",a);
printf("\n %d is greatest",b);
else
printf("\n %d is greatest",c);
return 0;
}
OUTPUT:
84 is greatest
ALGORITHM:
INPUT: A number
OUTPUT: Display whether the number is prime or not
STEP-1: Start
STEP-2: Read a number as n
STEP-3: if (n==0||n==1) then flag=1
STEP-4: for i=2 to n/2 , if(n%i==0) then flag=1
STEP-5: if(flag==0) print “n is a prime number”
STEP-6: else print “n is not a prime number”
STEP-7: Stop
PROGRAM:
#include <stdio.h>
int main()
int n, i, flag = 0;
scanf("%d", &n);
if (n == 0 || n == 1)
flag = 1;
flag = 1;
break;
if (flag == 0)
else
return 0;
ALGORITHM:
STEP-1: Start
STEP-3: fact=1
STEP-6: Stop
PROGRAM:
#include<stdio.h>
int main()
int fact=1,n,i;
printf("Enter a number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
fact=fact*i;
OUTPUT:
Enter a number:7
3. Conditional Operator:
THEORY:
The conditional operator, also known as the ternary operator, is a type of operator
used in many programming languages to make decisions based on conditions. It
provides a compact way to express conditional statements.The general syntax is as
follows:
?: This is the question mark symbol, which serves as the conditional operator itself. It
separates the condition from the expressions to be evaluated.
Expression1: This is the value to be returned if the condition is true. It can be any
valid expression.
“:”: This is the colon symbol, which separates expression1 from expression2.
Overuse of the conditional operator can make code less readable, so it's best used
for simple conditions. For more complex logic, traditional if-else statements may be
clearer.
ALGORITHM:
INPUT: A character
OUTPUT: Display whether the character entered is a lowercase alphabet or not
STEP-1: Start
STEP-2: Read a character as ch
STEP-3: if(ch>=’a’&&ch<=’z’) print “ch is a lowercase alphabet”
STEP-4: else print “ch is not a lowercase alphabet”
STEP-5: Stop
PROGRAM:
#include<stdio.h>
int main()
char ch;
printf("Enter a character:");
scanf("%c",&ch);
if(ch>='a'&& ch<='z')
else
return 0;
}
OUTPUT:
Enter a character:s
s is a lowercase alphabet
ALGORITHM:
INPUT: An year
OUTPUT: Display whether the entered year is a leap year or not
STEP-1: Start
STEP-2: Read an year as year
STEP-3: if(year%400==0) print “It is a leap year”
STEP-4: else if(year%100==0) print “It is not a leap year”
STEP-5: else if(year%4==0) print “It is a leap year”
STEP-6: else print “It is not a leap year”
STEP-7: Stop
PROGRAM:
#include <stdio.h>
int main()
int year;
scanf("%d", &year);
if (year % 400 == 0)
else if (year % 4 == 0)
else
{
return 0;
}
OUTPUT:
Switch Case:
THEORY:
The switch statement in C is another selection statement that provides a way to
execute different code blocks based on the value of a variable or expression. It's
particularly useful when you have multiple possible conditions to check against a
single variable.
Here's the general syntax of a switch statement:
switch (expression)
{
case constant1:
// code to execute if expression equals constant1
break;
case constant2:
// code to execute if expression equals constant2
break;
// more case statements as needed
default:
// code to execute if expression doesn't match any case
}
2. case constant: Each case statement represents a possible value of the expression.
If the value of the expression matches the constant, the corresponding code block
will be executed. You can have multiple case statements.
3. // code to execute: This is the block of code that will be executed if the value of
the expression matches the corresponding case constant.
5. default: The default case is optional and is used to execute code when none of the
case constants match the value of the expression.
ALGORITHM:
#include <stdio.h>
int main()
char op;
double num1,num2;
scanf("%c",&op);
scanf("%lf %lf",&num1,&num2);
switch (op)
case '+':
break;
case '-':
break;
case '*':
break;
case '/':
default:
printf("Invalid input!");
return 0;
}
OUTPUT:
4.ITERATION:
THEORY:
Iteration in C refers to the process of repeating a block of code multiple times. There
are several ways to implement iteration in C, including for, while, and do-while
loops.
1. for loop:
The for loop is used when the number of iterations is known before the loop
starts. Its syntax consists of three parts: initialization, condition, and increment
(or decrement) statement.
for (initialization; condition; increment/decrement)
{
// code to be executed repeatedly
}
The initialization step is executed only once at the beginning of the loop. The
condition is evaluated before each iteration, and if it's true, the loop continues;
otherwise, the loop terminates. After each iteration, the increment (or
decrement) statement is executed.
2. while loop:
The while loop is used when the number of iterations is not known beforehand,
and the loop continues until a specified condition becomes false. It evaluates the
condition before each iteration.
while (condition)
{
// code to be executed repeatedly
}
The loop continues as long as the condition evaluates to true. If the condition is
false initially, the code inside the loop will not execute at all.
3. do-while loop:
The do-while loop is similar to the while loop, but it evaluates the condition at
the end of each iteration. This means that the code inside the loop will always
execute at least once.
do{
} while (condition);
The loop first executes the code block, then evaluates the condition. If the condition
is true, the loop continues; otherwise, it terminates.
a) AIM: WAP to print first n prime nos.
ALGORITHM:
INPUT: Range
OUTPUT: Print all prime numbers within the range
STEP-1: Start
STEP-2: Read the range as n
STEP-3: for num=1 to n count=0
for i=2 to num/2
if(num%i==0) count++
STEP-4: if(count==0&&num!=1) print num
STEP-5: Stop
PROGRAM:
#include<stdio.h>
int main()
{
scanf("%d", &n);
for(num = 1;num<=n;num++)
count = 0;
for(i=2;i<=num/2;i++)
if(num%i==0)
count++;
break;
printf("%d ",num);
return 0;
}
OUTPUT:
Enter the range: 50
The prime numbers in between the range 1 to 50:2 3 5 7 11 13 17 19 23 29 31 37 41
43 47
ALGORITHM:
INPUT:None
OUTPUT: Print all three digit Armstrong numbers
STEP-1: Start
STEP-2: count=0
STEP-3: for i=100 to 1000
num=i
while(num!=0)
num/=10
count++
num=i
sum=pow(num%10,count)+pow((num%100- num%10)/10,count)
+pow((num%1000-num%100)/100,count);
if(sum==i) print i
STEP-4: Stop
PROGRAM:
#include <math.h>
#include <stdio.h>
int main()
num = i;
while (num != 0)
num /= 10;
count++;
num=i;
sum=pow(num%10,count)+pow((num%100-
num%10)/10,count)+pow((num%1000-num%100)/100,count);
if (sum == i)
count = 0;
return 0;
}
OUTPUT:
*
* *
* * *
* * * *
ALGORITHM:
INPUT: None
OUTPUT: Print the given pattern
STEP-1: Start
STEP-2: for i=1 to 4
for j=1 to 4-i
Print “ “
for k=1 to i
Print “ * “
Print “\n”
Step-3: Stop
PROGRAM:
#include<stdio.h>
int main()
int i,j,k;
for(i=1;i<=4;i++)
for(j=1;j<=4-i;j++)
printf(" ");
for(k=1;k<=i;k++)
printf(" * ");
printf("\n");
}
}
OUTPUT:
*
* *
* * *
* * * *