SEM1 - CPCL Lab Practicals
SEM1 - CPCL Lab Practicals
CODE:
#include <stdio.h>
#include <conio.h>
void main() {
int a, b;
int sum;
clrscr();
sum = a+b;
printf("\nSum = %d", sum);
getch();
}
OUTPUT:
P2 – Find the average of three numbers
CODE:
#include <stdio.h>
#include <conio.h>
void main() {
int a, b, c;
float avg;
clrscr();
avg = (a + b + c) / 3;
printf("\nAverage = %f", avg);
getch();
}
OUTPUT:
P3 – Calculate simple interest for the principal, rate and time
given by the user
CODE:
#include <stdio.h>
#include <conio.h>
void main() {
int p, t;
float r, si;
clrscr();
OUTPUT:
P4 – Calculate areas of Rectangle, Square and Circle
CODE:
#include <stdio.h>
#include <conio.h>
void main() {
int r, b, l, side;
float areaC, areaR, areaS;
clrscr();
printf("Radius: ");
scanf("%d", &r);
areaC = 3.14*r*r
printf("\nArea of Circle is: %f", areaC);
printf("\n");
printf("Length: ");
scanf("%d", &l);
printf("\nBreadth: ");
scanf("%d", &b);
areaR = l*b;
printf("\nArea of rectangle is: %f", areaR);
printf("\n");
OUTPUT:
P5 – Program to find if number is positive or negative
#include <stdio.h>
#include <conio.h>
void main() {
int x;
if (x>0) {
printf("Number is +ve"); }
else {
printf("Number is -ve");
}
getch();
}
OUTPUT:
P6 – Find the largest number out of the three given values
CODE:
#include <stdio.h>
#include <conio.h>
void main() {
int a, b, c;
clrscr();
if(a==b==c) {
printf("\n All values are equal.");
} else if((a>b)&&(a>c)) {
printf("\n%d is largest.",a);
} else if((b>a)&&(b>c)) {
printf("\n%d is largest.",b);
} else {
printf("\n%d is largest.",c);
}
getch();
}
OUTPUT:
P7 – Check whether the number is even or odd
CODE:
#include <stdio.h>
#include <conio.h>
void main() {
int a;
clrscr();
if(a%2 == 0) {
printf("\n%d is even",a);
} else {
printf("\n%d is odd",a);
}
getch();
}
OUTPUT:
P8 – Find root of quadratic equation
CODE:
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main() {
int a, b, c, d;
float e, f;
clrscr();
d = ((b*b) - (4*a*c));
if(d>0) {
e = (-b + sqrt(d)/2*a);
f = (-b - sqrt(d)/2*a);
printf("\nReal Roots");
printf("\nRoot1 = %f", e);
printf("\nRoot2 = %f", f);
} else if(d == 0) {
f = (-b / 2*a);
printf("\nEqual Roots");
printf("\nRoot1 = %f", f);
printf("\nRoot2 = %f", f);
} else {
printf("\nImaginary Roots");
}
getch();
}
OUTPUT:
P9 – Design a basic calculator using switch statement
CODE:
#include <stdio.h>
#include <conio.h>
void main() {
int a, b;
float c;
char z;
clrscr();
printf("Enter the operator:");
scanf("%c", &z);
printf("Enter the numbers:");
scanf("%d%d", &a, &b);
switch (z) {
case '+': {
c = a+b;
printf("Addition of the number is = %f", c);
break;
} case '-': {
c=a-b;
printf("Subtraction of number is = %f", c);
break;
} case '*': {
c = a*b;
printf("Multiplication of number is = %f", c);
break;
} case '/': {
c = a/b;
printf("Division of number is = %f", c);
break;
}
default: {
printf("Invalid");
}
}
getch();
}
OUTPUT:
P10 – Swap the two integers entered by the user
CODE:
#include <stdio.h>
#include <conio.h>
void main() {
int a, b, c;
clrscr();
c = a; a = b; b = c;
printf("\nSwapped values are: ");
printf("\nA = %d", a);
printf("\nB = %d", b);
getch();
}
OUTPUT:
P11 – Print even/odd numbers using loop
#include <stdio.h>
#include <conio.h>
#include <stdio.h>
#include <conio.h
void main() {
int a;
for(a=1;a<=9;a+=2) {
printf("%d\n",a); }
for(a=2;a<=10;a++) {
if(a%2==0){
printf("%d\n",a);
}
}
getch();
}
OUTPUT:
P12 – Factorial
CODE:
#include <stdio.h>
#include <conio.h>
void main() {
int num, fact = 1;
clrsrc();
printf("Enter the number:");
scanf("%d",&num);
OUTPUT:
P13 – Fibonacci series
CODE:
#include <stdio.h>
#include <conio.h>
void main() {
int n;
clrscr();
printf("Enter number: ");
scanf("%d",&n);
printf("\nFibonnaci series for %d terms is:\n", n);
for(int i = 1; i <= n; i++) {
printf("%d ",fibonacci(i));
}
getch();
}
int fibonacci(int n) {
if(n==1 || n==0) {
return n;
} else {
return fibonacci(n-1) + fibonacci(n-2);
}
}
OUTPUT:
P14 – Reversing digits of a number
CODE:
#include <stdio.h>
#include <conio.h>
void main() {
int n, reverse = 0;
clrscr();
printf("Enter a number: \n");
scanf("%d", &n);
while (n != 0) {
reverse = reverse * 10;
reverse = reverse + n%10;
n = n/10;
}
OUTPUT:
P15 – Program to add the digits of a number
#include <stdio.h>
#include <conio.h>
void main() {
int a,b=0,c;
printf("Enter number=");
scanf("%d", &a);
do {
c=a%10; a=a/10; b=b+c; }
while (a!=0);
printf("\nSum=%d", b);
getch();
}
OUTPUT:
P16 – Use break and continue
CODE:
OUTPUT:
OUTPUT:
P17A – Find factorial using “while” loop
CODE:
#include <stdio.h>
#include <conio.h>
void main() {
int num, i, fact=1;
clrsrc();
printf("Enter the number\n");
scanf("%d",&num);
i = num;
while(i >= 1) {
fact *= i;
i--;
}
printf("The factorial of given number %d is %d\n", num, fact);
getch();
}
OUTPUT:
P17B – Find factorial using “do while” loop
CODE:
#include <stdio.h>
#include <conio.h>
void main() {
int num, i, fact=1;
clrsrc();
printf("Enter the number\n");
scanf("%d",&num);
i = num;
do {
fact *= i;
i--;
}
while(i >= n);
OUTPUT:
P18 – Print pattern using nesting of loops
CODE:
#include <stdio.h>
#include <conio.h>
void main() {
int a = 1, b, rows;
clrscr();
printf("Enter number of rows: ");
scanf("%d", &rows);
b = rows - 1;
OUTPUT:
P19 – Check whether a number is prime or not
CODE:
#include <stdio.h>
#include <conio.h>
#include<math.h>
void main() {
int prime = 1, n, i;
printf("Enter value:\n");
scanf("%d", &n);
getch();
}
OUTPUT:
P20 – Multiplication Table
CODE:
#include <stdio.h>
#include <conio.h>
void main() {
int n, table[10];
printf("Enter a number: \n");
scanf("%d", &n);
OUTPUT:
P21 – Product of two numbers using functions
CODE:
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main() {
int y;
y=mul(15,5);
printf("Product is %d", y);
getch();
}
OUTPUT:
P22 – Print text using functions
CODE:
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main () {
clrscr();
char str1[12] = "Hello";
char str2[12] = "World";
char str3[12];
int len;
strcpy(str3, str1);
printf("strcpy(str3, str1): %s\n", str3 );
strcat( str1, str2);
printf("strcat(str1, str2): %s\n", str1 );
len = strlen(str1);
printf("strlen(str1): %d\n", len );
getch();
}
OUTPUT:
P24 – Factorial using recursion
CODE:
#include <stdio.h>
#include <conio.h>
void main() {
int n;
clrscr();
printf("Enter number:");
scanf("%d",&n);
printf("Factorial of %d is: %d", n, factorial(n));
getch();
}
int factorial(int n) {
if(n == 1) {
return n;
} else {
return n * factorial(n - 1);
}
}
OUTPUT:
P24 – Nesting of Functions
CODE:
#include<stdio.h>
#include<conio.h>
float ratio(int x,int y,int z);
int diff(int x,int y);
void main() {
int a,b,c;
printf("\tENTER THE VALUES OF A,B AND C:");
scanf("%d%d%d",&a,&b,&c);
printf("\tRATIO IS:%f",ratio(a,b,c));
getch();
}
OUTPUT:
24.Difference between Call
by Value and Call by Reference using Functions
#include<stdio.h>
#include<conio.h>
void swap(int x,int y);
void change(int *p,int *q);
void main() {
int a,b;
printf("\nENTER THE VALUES OF A AND B:");
scanf("%d%d",&a,&b);
printf("\nEXAMPLE OF CALL BY VALUE FUNCTION.");
printf("\nBEFORE SWAPPING VALUES OF A AND B ARE: %d %d ", a,b);
swap(a,b);
printf("\nAFTER SWAPPING VALUES OF A AND B ARE: %d %d ",a,b);
printf("\n\n\nEXAMPLE OF CALL BY REFERENCE FUNCTION.");
printf("\nBEFORE SWAPPING VALUES OF A ND B ARE %d %d ",a,b);
change(&a,&b);
printf("\nAFTER SWAPPING VALUES OF A NAND B ARE %d %d ",a,b);
getch();
}
void swap(int x,int y) {
int z; z=x; x=y; y=z;
}
#include<conio.h>
#include<stdio.h>
void main() {
int ar[100],i,n;
OUTPUT:
26. Linear Search in an Array
#include<stdio.h>
#include<conio.h>
void main() {
int ar[100],i,n,x,f=0;
OUTPUT:
27. Bubble Sort Algorithm
#include<stdio.h>
#include<conio.h>
void main() {
int ar[100],i,n,j,temp;
printf("ENTER THE NUMBER OF ELEMENTS:");
scanf("%d",&n);
printf("\nENTER THE ELEMENTS:");
for(i=0;i<n;i++) {
scanf("%d",&ar[i]); }
printf("\nTHE ELEMENTS ARE:");
for(i=0;i<n;i++) {
printf("%d",ar[i]); }
for(i=0;i<n;i++) {
for(j=0;i<n-1-j;j++) { if(ar[j]>ar[j+1]) {
temp=ar[j];
ar[j]=ar[j+1];
ar[j+1]=temp;
}}}
printf("\n\nTHE SORTED ARRAY LIST IS:");
for(i=0;i<n;i++) {
printf("%d",ar[i]);
}
getch();}
OUTPUT:
28. Entering elements in matrix
#include <stdio.h>
#include <conio.h>
void main() {
clrscr();
int i, j, row, col ;
printf("Enter the order of the matrix: ") ;
scanf("%dx%d", &row, &col);
int mat[row][col];
for(i = 0 ; i < row ; i++) {
for(j = 0 ; j < col ; j++) {
printf("Enter the number for column %d of row %d:", j+1, i+1);
scanf("%d", &mat[i][j]); }}
OUTPUT:
29. Addition of matrices
#include <stdio.h>
#include <conio.h>
void main() {
clrscr();
int i, j, row, col;
printf("Enter the order of the matrices: ");
scanf("%dx%d", &row, &col);
int mat1[row][col], mat2[row][col], mat3[row][col];
printf("Enter the values for Matrix 1:\n");
for(i = 0 ; i < row ; i++) {
for(j = 0 ; j < col ; j++) {
scanf("%d", &mat1[i][j]); }}
printf("\nEnter the values for Matrix 2 :\n");
for(i = 0 ; i < row ; i++) {
for(j = 0 ; j < col ; j++) {
scanf("%d", &mat2[i][j]); }}
printf("\nAddition of two matrices: \n\n") ;
for(i = 0 ; i < row ; i++){
for(j = 0 ; j < col ; j++){
mat3[i][j] = mat1[i][j] + mat2[i][j];
printf("%d", mat3[i][j]);
printf("\t");
} printf("\n");
} getch(); }
OUTPUT:
30. String Handling Functions
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main(){
char str1[10]="Hello";
char str2[10]="World";
char str3[10];
char str4[10]="Mello";
int len, x;
strcpy(str3, str1);
printf("String 3 is %s", str3);
strcat(str1,str2);
printf("\nConcatenated string is %s", str1);
len=strlen(str1);
printf("\nLength of string 1 is %d", len);
x=strcmp(str3, str4);
printf("\nDifference of ASCII values is %d", x);
getch();
OUTPUT:
31. Print prime numbers
#include <stdio.h>
#include <conio.h>
void main() {
clrscr();
int n, i = 3, count, c;
printf("Enter the number of prime numbers required: ");
scanf("%d",&n);
if (n >= 1) {
printf("First %d prime numbers are:\n",n);
printf("2\n");
} for (count = 2; count <= n; i++) {
for (c = 2; c <= i - 1; c++) {
if (i%c == 0)
break;
} if (c == i) {
printf("%d\n",i);
count++;
}
} getch();
}
OUTPUT: