CP Lab Manual
CP Lab Manual
REGULATION :- R23
PREPARED BY
C.BALAJIM.TECH,
Program
#include <stdio.h>
int main() {
return 0;
Flow chart
Step1:start
Step2: Declare a variable a,b,c and avg as int;
Step3:Read two numbers a,b and c;
Step4avg=(a+b+c)/3;
Step5:Print avg;
Step6:stop
Program:
#include<stdio.h>
int main() {
}
Output:
Enter three variables:
a:10
b:20
c:25
average of 10,20 and 25 is 20
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
float celsius, fahrenheit;
clrscr();
printf("\n
n Enter Temp in Fahrenheit : ");
scanf("%f", &fahrenheit);
celsius = (fahrenheit-32)
32) / 1.8;
printf("\n
n Temperature in Celsius : %.2f ", celsius);
getch();
}
# include <stdio.h>
# include <stdlib.h>
int main(){
//Simple interset program
int P, R, T;
double SI;
printf("Enter the principal: ");
scanf("%d", &P);
printf("Enter the rate: ");
scanf("%d", &R);
printf("Enter the time: ");
scanf("%d", &T);
SI = (P * R * T) / 100;
printf("The Simple interest is %f", SI);
return 0;
}
#include <math.h>
#include <stdio.h>
int main() {
double number, squareRoot;
printf("Enter a number: ");
scanf("%lf", &number);
// computing the square root
squareRoot = sqrt(number);
printf("Square root of %.2lf = %.2lf", number, squareRoot);
return 0;
}
return 0;
}
Out put
Compound interest is 1025
int main(){
area = sqrt(s*(s-sideOne)*(s-sideTwo)*(s-sideThree));
printf("Area of triangle : %0.4f\n", area);
return 0;
}
Output
Enter the length of three sides of triangle
3 4 5
Area of triangle : 6.0000
int main()
float u, a, d;
int t;
printf("\nEnter
nEnter the value of a : ");
printf("\nEnter
nEnter the value of u : ");
printf("\nEnter
nEnter the value of t : ");
d = (u * t) + (a * t * t) / 2;
return 0;
Output:
#include<stdio.h>
int main(){
int a,b,c,big;
printf("\nEnter 3 numbers:");
scanf("%d %d %d",&a,&b,&c);
big=(a>b&&a>c?a:b>c?b:c);
return 0;
#include <stdio.h>
int main()
return 0;
#include<stdio.h>
int main()
{
int num1, num2, num3, num4;
printf("Enter 4 numbers here...\n");
scanf("%d %d %d %d", &num1, &num2, &num3, &num4);
if(num1>num2&&num1>num3&&num1>num4)
{
printf("%d (num1) is greatest",num1);
}
else if(num2>num3&&num2>num4)
{
printf("%d (num2) is greatest",num2);
}
else if(num3>num4)
{
printf("%d (num3) is greatest",num3);
}
else
{
printf("%d (num4) is greatest",num4);
}
return 0;
#include <stdio.h>
int main()
{
int unit;
float amt, total_amt, sur_charge;
return 0;
}
#include <math.h>
#include <stdio.h>
int main() {
double a, b, c, discriminant, root1, root2, realPart, imagPart;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf", &a, &b, &c);
discriminant = b * b - 4 * a * c;
if (discriminant > 0)
{
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("root1 = %.2lf and root2 = %.2lf", root1, root2);
}
else if (discriminant == 0)
{
root1 = root2 = -b / (2 * a);
printf("root1 = root2 = %.2lf;", root1);
}
else
{
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imagPart, realPart,
imagPart);
}
return 0;
}
Output
5.6
#include <stdio.h>
int main() {
char op;
double first, second;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &op);
printf("Enter two operands: ");
scanf("%lf %lf", &first, &second);
switch (op) {
case '+':
printf("%.1lf + %.1lf = %.1lf", first, second, first + second);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf", first, second, first - second);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf", first, second, first * second);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf", first, second, first / second);
break;
// operator doesn't match any case constant
default:
printf("Error! operator is not correct");
}
return 0;
}
#include<stdio.h>
#include<conio.h>
void main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if(((year%4==0) && ((year%400==0) || (year%100!==0))
{
printf("%d is a leap year", &year);
} else {
printf("%d is not a leap year", &year);
}
getch();
}
Output
Enter a year: 2004
2004 is a leap year
Output
Enter any number 6
The number 6 is not a Prime Number
return 0;
}
Output
Enter an integer: 1001
1001 is a palindrome.
Output
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
program
#include <stdio.h>
#include <conio.h>
int main()
{
int a[1000],i,n,min,max;
printf("Enter size of the array : ");
scanf("%d",&n);
printf("Enter elements in array : ");
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
min=max=a[0];
for(i=1; i<n; i++)
{
if(min>a[i])
min=a[i];
if(max<a[i])
max=a[i];
}
printf("minimum of array is : %d",min);
printf("\nmaximum of array is : %d",max);
return 0;
}
S
output
Enter size of the array: 5
Enter elements in array: 1
2
3
4
5
minimum of an array is: 1
maximum of an array is: 5
program
// C Program to Reverse an Array by Printing it from The Last Element to the First Element
#include <stdio.h>
#define N 1000
int main() {
int arr[N];
int n;
printf("Enter the size of the array: ");
scanf("%d", &n);
printf("Enter an array: ");
for (int i = 0; i< n; i++){
scanf("%d", &arr[i]);
}
printf("Reversed array: ");
for (int i = n-1; i>=0; i--){
printf("%d ", arr[i]);
}
return 0;
}
output
Enter the size of the array: 5
Enter an array: 3 8 4 9 6
Reversed array: 6 9 4 8 3
#include <stdio.h>
int main()
{
int n, count = 0;
printf("Enter number of elements in the array: ");
scanf("%d", &n);
int arr[n], temp[n];
if(n==0)
{
printf("No element inside the array.");
exit(0);
}
printf("Enter elements in the array: ");
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
printf("\nArray Before Removing Duplicates: ");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
for (int i = 0; i < n; i++)
{
int j;
for (j = 0; j < count; j++)
{
if (arr[i] == temp[j])
break;
}
return 0;
}
Output
Enter number of elements in the array: 8
Enter elements in the array: 9 3 6 9 5 4 0 5
program
#include <stdio.h>
int main() {
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and 100): ");
scanf("%d", &c);
return 0;
}
Outpur
Enter the number of rows (between 1 and 100): 2
Enter the number of columns (between 1 and 100): 3
10 8 6
program
#include<stdio.h>
#include<stdlib.h>
int main(){
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
system("cls");
printf("enter the number of row=");
scanf("%d",&r);
printf("enter the number of column=");
scanf("%d",&c);
printf("enter the first matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the second matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}
Output
program
#include <stdio.h>
int main(){
int arr[50], num, x, y, temp;
printf("Please Enter the Number of Elements you want in the array: ");
scanf("%d", &num);
printf("Please Enter the Value of Elements: ");
for(x = 0; x < num; x++)
scanf("%d", &arr[x]);
for(x = 0; x < num - 1; x++){
for(y = 0; y < num - x - 1; y++){
if(arr[y] > arr[y + 1]){
temp = arr[y];
arr[y] = arr[y + 1];
arr[y + 1] = temp;
}
}
}
printf("Array after implementing bubble sort: ");
for(x = 0; x < num; x++){
printf("%d ", arr[x]);
}
return 0;
}
Output
Please Enter the Number of Elements you want in the array:5
Please Enter the Value of Elements: 9 5 -2 7 3
Array after implementing bubble sort: -2 3 5 7 9
program
void main(void)
{
char str1[25],str2[25];
int i=0,j=0;
printf("\n Enter First String:");
gets(str1);
printf("\nEnter Second String:");
gets(str2);
while(str1[i]!='\0')
i++;
while(str2[j]!='\0')
{
str1[i]=str2[j];
j++;
i++;
}
str1[i]='\0';
printf("\nConcatenated String is %s",str1);
}
Out put
Enter First String: tech
Enter Second String: college
Concatenated String is tech college
Program:
#include<stdio.h>
main ()
{
char a[50];
clrscr();
printf(“enter a string”);
gets(a);
strrev(a);
printf(“reversed string =%s,a);
getch();
}
Output
enter a string hello
reversed string =olleh
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main(){
char string[20],temp;
int i,length;
printf("Enter String : ");
scanf("%s",string);
length=strlen(string)-1;
for(i=0;i<strlen(string)/2;i++){
temp=string[i];
string[i]=string[length];
string[length--]=temp;
}
printf("Reverse string :%s",string);
getch();
}
Output
enter a string hello
reversed string is olleh
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
void main()
{
int i, n;
int *a, *b, *c;
OUTPUT
#include <stdio.h>
#include <conio.h>
struct student
{
int rollno,tot;
char name[25];
int mark[5];
};
void main()
{
struct student s[5];
int i,n,j;
clrscr();
printf(“Enter the number of students:”);
scanf(“%d”,&n);
printf(“\t*Students Records*\n”);
//take input from user
for(i=0;i<n;i++)
{
printf("\nEnter Student Roll Number: ");
scanf("%d",&s[i].rollno);
printf("\nEnter Student name: ");
scanf("%s",s[i].name);
printf("\nEnter Student 3 subject's marks: ");
for(j=0;j<3;j++)
scanf("%d",&s[i].mark[j]);
}
//calculation
for(i=0;i<n;i++)
{
s[i].tot=0;
for(j=0;j<3;j++)
s[i].tot = s[i].tot+ s[i].mark[j];
}
//Display result
for(i=0;i<n;i++)
{
printf("\t*Students Records*\n");
printf("\n==================================\n");
printf("\nStudent's Roll no. – %d", s[i].rollno);
printf("\nStudent's Name – %s", s[i].name);
printf("\nStudent's Total Marks – %d", s[i].tot);
}
#include <stdio.h>
#include <stdlib.h>
int main() {
ptr[0] = 1;
ptr[1] = 2;
ptr[2] = 3;
ptr[3] = 4;
ptr[4] = 5;
free(ptr);
return 0;
Output
1 2 3 4 5
ii) Demonstrate the differences between structures and unions using a C program.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info ;
struct node *link ; // Self-referential structure link is pointing the same
// structure of the type node
};
struct node *START = NULL ; // start pointer to control the linked list //
struct node* createnode() ;
void insertatlast() ; // insert node at last //
void deleteatfirst() ; // delete node at first //
void viewlist() ;
void insertatfirst() ;
int getlength() ;
int menu() ;
void insertafteranynode() ;
void deleteatlast() ;
void deleteatposition() ;
}
else
{
int i = 1 ;
newnode = createnode() ;
printf ( "Enter Data \n " ) ;
}
else
{
while ( i < position )
{
q=t;
t = t->link ;
i++ ;
}
if ( t == START )
{
START == NULL ;
}
else
{
q->link = t->link ;
free( t ) ;
}
}
}
}
int menu()
{
int ch ;
printf ( " \t \t \t 1.ADD NODE LAST IN LINK \n " ) ;
printf ( " \t \t \t 2.ADD NODE AT FIRST IN LINK \n " ) ;
printf ( " \t \t \t 3.VIEW LIST IN LINK \n " ) ;
printf ( " \t \t \t 4.DELETE NODE AT FIRST IN LINK \n " ) ;
printf( " \t \t \t 5. TO SEE THE LENGTH OF LIST \n " ) ;
#include <stdio.h>
#define INT_BITS 32
struct some {
int a;
char b;
};
int main(void)
{
struct some some1;
struct some *ptr = malloc(sizeof(struct some));
struct some *ptr2 = malloc(sizeof(struct some));
struct some *ptr3 = malloc(sizeof(struct some));
memset(ptr, 0, sizeof(struct some));
memset(ptr2, 0, sizeof(struct some));
ptr->a = 123;
ptr->b = 'b';
*ptr2 = *ptr;
printf("%d %c\n", ptr2->a, ptr2->b);
some1 = *ptr;
printf("%d %c\n", some1.a, some1.b);
*ptr3 = some1;
printf("%d %c\n", ptr3->a, ptr3->b);
return 0;
}
Output
123 b
123 b
123 b