0% found this document useful (0 votes)
82 views34 pages

SEM1 - CPCL Lab Practicals

The document contains 20 code snippets demonstrating various programming concepts in C language. The codes cover basic calculations like addition, average, interest calculation etc. More complex concepts covered are prime number check, pattern printing using loops, factorial calculation using different loops, Fibonacci series and reversing digits of a number. Each code snippet is followed by its output. The codes progress from simple to more advanced programming exercises.

Uploaded by

bdn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views34 pages

SEM1 - CPCL Lab Practicals

The document contains 20 code snippets demonstrating various programming concepts in C language. The codes cover basic calculations like addition, average, interest calculation etc. More complex concepts covered are prime number check, pattern printing using loops, factorial calculation using different loops, Fibonacci series and reversing digits of a number. Each code snippet is followed by its output. The codes progress from simple to more advanced programming exercises.

Uploaded by

bdn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 34

P1 – Addition of two values

CODE:

#include <stdio.h>
#include <conio.h>

void main() {
int a, b;
int sum;
clrscr();

printf("Enter the value of A:");


scanf("%d", &a);
printf("Enter the value of B:");
scanf("%d", &b);

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();

printf("Enter the value of A:");


scanf("%d", &a);
printf("Enter the value of B:");
scanf("%d", &b);
printf("Enter the value of C:");
scanf("%d", &c);

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();

printf("Enter the value of Principle: ");


scanf("%d", &p);

printf("\nEnter the Rate Of Interest: ");


scanf("%f", &r);

printf("\nEnter the Time: ");


scanf("%d", &t);
si = (p * r * t)/100;

printf("\nSimple interest is: %f", si);


getch();
}

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");

printf("\nSide of square is: ");


scanf("%d", &side);
areaS = side*side;
printf("\nArea of square is: %f", areaS);
getch();
}

OUTPUT:
P5 – Program to find if number is positive or negative

#include <stdio.h>
#include <conio.h>

void main() {
int x;

printf("Enter value =");


scanf("%d", &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();

printf("Enter the value of A: ");


scanf("%d", &a);

printf("Enter the value of B: ");


scanf("%d", &b);

printf("Enter the value of C: ");


scanf("%d", &c);

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();

printf("Enter the value of A: ");


scanf("%d", &a);

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();

printf("Enter the Coefficient of x^2 =");


scanf("%d", &a);
printf("Enter the Coefficient of x =");
scanf("%d", &b);
printf("Enter the value of Constant =");
scanf("%d", &c);

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();

printf(“Enter the value of A: ");


scanf("%d", &a);
printf("\nEnter the value of B: ");
scanf("%d", &b);

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;

printf("\nOdd numbers to 10 are=");

for(a=1;a<=9;a+=2) {
printf("%d\n",a); }

printf("\nEven numbers to 10 are=");

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);

for(int i = num; i >= 1; i--) {


fact=fact*i;
}
printf("The factorial of given number %d is %d\n", num, fact);
getch();
}

OUTPUT:
P13 – Fibonacci series

CODE:

#include <stdio.h>
#include <conio.h>

int fibonacci(int n);

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;
}

printf("Reverse of entered number is: %d", reverse);


getch();
}

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:

#include <stdio.h> #include <stdio.h>


#include <conio.h> #include <conio.h>

void main() { void main() {


int n, i = 0; int n, i = 0;
clrscr(); printf("Enter number: ");
printf("Enter number: "); scanf("%d", &n);
scanf("%d", &n); while(i <= n) {
while(i <= n) { i++;
if(i == 5) { if (i == n/2) {
break; continue;
} }
printf("%d", n); printf("%d\n", i);
i++; }
} getch();
getch(); }
}

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);

printf("The factorial of given number %d is %d\n", num, fact);


getch();
}

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;

for(int i = 1; i <=rows ; i++) {


for(int j = 1; j <= b; j++) {
printf(" ");
}
for(int j = 1; j <= i; j++) {
printf("* ");
}
a+=1;
b--;
printf("\n");
}
getch();
}

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);

for(int i = 2; i <=sqrt(n)/2 ; i++) {


if(n%i==0) { prime = 0; break; }
}
if(prime==1) {printf("%d is a prime number", n);}
else {printf("%d is not a prime number", 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);

for(int i = 0; i < 10; i++) {


table[i] = (i+1) * n;
}

printf("Multiplication table of %d is: \n", n);


for (int j = 0; j < 10; j++) {
printf("%d X %d = %d\n", table[0], j+1, table[j]);
}
getch();
}

OUTPUT:
P21 – Product of two numbers using functions

CODE:

#include <stdio.h>
#include <conio.h>
#include <math.h>

void main() {
int y;

int mul(int x, int y) {


int p;
p=x*y;
return(p);
}

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>

int factorial(int n);

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();
}

float ratio(int x,int y,int z)


{
if(diff(y,z)) {
return(x/(y-z)); }
else return(0.0); }
int diff(int p,int q) {
if(p!=q)
return(1);
else
return(0);
}

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;
}

void change(int *p, int *q) {


int d; d=*p; *p=*q; *q=d;
}
OUTPUT:
25. Print an Array based on user input

#include<conio.h>
#include<stdio.h>

void main() {
int ar[100],i,n;

printf("\nENTER THE SIZE OF THE ARRAY:");


scanf("%d",&n);
printf("\nENTER THE %d ELEMENTS OF THE ARRAY:",n);
for(i=0;i<n;i++) {
scanf("%d",&ar[i]);
}
printf("\nTHE %d ELEMENTS OF THE ARRAY ARE:",n);
for(i=0;i<n;i++) {
printf("%d ",ar[i]);
}
getch();

OUTPUT:
26. Linear Search in an Array

#include<stdio.h>
#include<conio.h>

void main() {

int ar[100],i,n,x,f=0;

printf("\nENTER THE SIZE OF THE ARRAY:");


scanf("%d",&n);
printf("\nENTER THE %d ELEMENTS OF THE ARRAY:",n);
for(i=0;i<n;i++) {
scanf("%d",&ar[i]);
}
printf("\nTHE %d ELEMENTS OF THE ARRAY ARE=",n);
for(i=0;i<n;i++) {
printf("%d ",ar[i]); }
printf("\n\nENTER THE ELEMENT TO BE SEARCHED.");
scanf("%d",&x);
for(i=0;i<n;i++) {
if(ar[i]==x) {f=1; break; } }
if(f==1)
printf("\n%d IS PRESENT IN THE ARRAY",x);
else printf("\n%d IS NOT PRESENT IN THE ARRAY.",x),
getch();
}

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]); }}

printf("\nThe elements in the matrix are: \n\n") ;


for(i = 0 ; i < row ; i++){
for(j = 0 ; j < col ; j++){
printf("%d", mat[i][j]) ;
printf("\t");
}
printf("\n");
}
getch();
}

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:

You might also like