0% found this document useful (0 votes)
26 views41 pages

C Lab Record > BCA HONORS> 2024 to 2027

C programming lab recrd

Uploaded by

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

C Lab Record > BCA HONORS> 2024 to 2027

C programming lab recrd

Uploaded by

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

NAME: .................................................

....................................................................................................................................
.................................
.
BATCH: ........................................................ REG. NO. :..............................................................................................

SUBJECT:..........................................................
..........................................................................................................................................................................
................................................................................................................

in the Computer Applications Department of this College

Lecturer -in -Charge Head of the Department

With Reg. No.................................................. Date :.............................

Examiners: 1.

2.
PAGE
SL.NO. DATE NAME OF EXPERIMENTS NO
Software Lab in C

1. Write a C program to print your address?

Program

#include<stdio.h>

#include<conio.h>

void main()

clrscr();

printf(“ADDRESS\n”);

printf(“Anil\n”);

printf(“Al-Azhar College of Arts and Science\n”);

printf(“Thodupuzha\n”);

getch();

Output

ADDRESS

Anil

Al-Azhar College of Arts and Science

Thodupuzha

First Semester BCA (Hons.) 4


Software Lab in C

2. Write a C program to find the sum and average of 3 numbers?

Program

#include<stdio.h>

#include<conio.h>

void main()

int a,b,c, sum;

float avg;

clrscr();

printf("Please enter 3 numbers");

scanf("%d%d%d",&a,&b,&c);

sum=a+b+c;

avg=(a+b+c)/3;

printf("\nSum is %d", sum);

printf("\nAverage is %f",avg);

getch();

Output

Please Enter 3 numbers

5 8 2

Sum is 15

Average is 5

First Semester BCA (Hons.) 5


Software Lab in C

3. Write a C program to calculate Simple Interest?

Program

#include<stdio.h>

#include<conio.h>

void main()

float principle, rate, time, simple_interest;

clrscr()

printf("Enter the principle :");

scanf("%f", &principle);

printf("Enter the rate :");

scanf("%f", &rate);

printf("Enter the time :");

scanf("%f", &time);

simple_interest = principle * rate * time / 100;

printf("Simple interest is %0.2f", simple_interest);

getch()

Output

Enter the principle: 5400


Enter the rate: 8
Enter the time: 3
Simple interest is 1296.00

First Semester BCA (Hons.) 6


Software Lab in C

4. Write a C program to calculate the square and square root of a


number?

Program

#include<stdio.h>

#include<math.h>

#include<conio.h>

void main()

int n,sqr=0,sqroot=0;

clrscr();

printf("\nEnter The Number\n");

scanf("%d",&n);

sqr=n*n;

sqroot=sqrt(n);

printf("\n SQUARE OF %d is %d .",n,sqr);

printf("\n SQUARE-ROOT OF %d is %d .",n,sqroot);

getch();

Output

Enter The Number

25

SQUARE OF 25 is 625.

SQUARE-ROOT OF 25 is 5.

First Semester BCA (Hons.) 7


Software Lab in C

5. Write a C program to find the smaller of 2 numbers?

Program

#include<stdio.h>

#include<conio.h>

void main()

int a,b;

clrscr();

printf("\n Enter First Number : ");

scanf("%d",&a);

printf("\n Enter Second Number : ");

scanf("%d",&b);

if(a<b) //logical test

{ printf("\n %d is Smaller",a);

else

{printf("\n %d is smaller",b);

getch();

Output

Enter First Number: 6

Enter Second Number: 3

3 is smaller

First Semester BCA (Hons.) 8


Software Lab in C

6. Write a C program to check whether the given number is even or


odd?

Program

#include <stdio.h>

#include <conio.h>

void main()

int number;

clrscr();

printf("Enter an integer: ");

scanf("%d", &number);

// True if the number is perfectly divisible by 2

if(number % 2 == 0)

printf("%d is even.", number);

else

printf("%d is odd.", number);

getch();

Output

Enter an integer

5 is odd.

First Semester BCA (Hons.) 9


Software Lab in C

7. Write a C program to find the largest among 3 numbers?

Program

#include<stdio.h>

#include<conio.h>

void main()

{ int a,b,c;

clrscr();

printf("Enter three numbers\n");

scanf("%d%d%d",&a,&b,&c);

if(a>b)

if(a>c)

printf(" %d is larger",a);

else

printf(" %d is larger",c);

else

if(b>c)

printf(" %d is larger",b);

else

printf(" %d is larger",c);

getch();}

Output

Enter three numbers

10 3 18

18 is larger.

First Semester BCA (Hons.) 10


Software Lab in C

8. Write a C program to check whether the given number is


positive or negative?

Program

#include <stdio.h>

#include <conio.h>

void main()

int num;

clrscr();

/* Input number from user */

printf("Enter any number: \n");

scanf("%d", &num);

if(num > 0)

printf("Number is POSITIVE");

if(num < 0)

printf("Number is NEGATIVE");

if(num == 0)

First Semester BCA (Hons.) 11


Software Lab in C

printf("Number is ZERO");

getch();

Output

Enter any number:

14

Number is POSITIVE

First Semester BCA (Hons.) 12


Software Lab in C

9. Write a C program to check whether the given number is


palindrome or not?

Program

#include <stdio.h>

#include <conio.h>

void main() {

int n, n1, rev = 0, rem;

clrscr();

printf("Enter any number:\n ");

scanf("%d", &n);

n1 = n;

while (n > 0)

rem = n % 10;

rev = rev * 10 + rem;

n = n / 10;

if (n1 == rev){

printf("Given number is a palindrome number");

First Semester BCA (Hons.) 13


Software Lab in C

else{

printf("Given number is not a palindrome number");

getch();

Output

Enter any number:

121

Given number is a palindrome number

First Semester BCA (Hons.) 14


Software Lab in C

10. Write a C program to check whether the given number is


Armstrong or not?

Program

#include<stdio.h>

int main(){

int num,r,sum=0,temp;

printf("Enter a number: ");

scanf("%d",&num);

temp=num;

while(num!=0)

r=num%10;

num=num/10;

sum=sum+(r*r*r);

if(sum==temp)

printf("%d is an Armstrong number",temp);

else

printf("%d is not an Armstrong number",temp);

return 0;

Output

Enter a number: 153

153 is an Armstrong number

First Semester BCA (Hons.) 15


Software Lab in C

11. Write a C program to check whether the given number is


prime or not?

Program

#include <stdio.h>

int main()

int n, i, flag = 0;

printf("Enter a positive integer: ");

scanf("%d",&n);

for(i=2; i<=n/2; ++i)

// condition for nonprime number

if(n%i==0)

flag=1;

break;

if (flag==0)

printf("%d is a prime number.",n);

else

First Semester BCA (Hons.) 16


Software Lab in C

printf("%d is not a prime number.",n);

return 0;

Output

Enter a positive integer: 13

13 is a prime number.

First Semester BCA (Hons.) 17


Software Lab in C

12. Write a C program to find the sum of digits of a number.

Program

#include <stdio.h>

int main()

int num, sum=0;

/* Input a number from user */

printf("Enter any number to find sum of its digit:\n");

scanf("%d", &num);

/* Repeat till num becomes 0 */

while(num!=0)

/* Find last digit of num and add to sum */

sum += num % 10;

/* Remove last digit from num */

num = num / 10;

printf("Sum of digits = %d", sum);

First Semester BCA (Hons.) 18


Software Lab in C

return 0;

Output

Enter any number to find sum of its digit:

123

Sum of digits = 6

First Semester BCA (Hons.) 19


Software Lab in C

13. Write a C program to find the reverse of a number?

Program

#include <stdio.h>

int main()

int Number, Reminder, Reverse = 0;

printf("\nPlease Enter any number to Reverse\n");

scanf("%d", & Number);

while (Number > 0)

Reminder = Number %10;

Reverse = Reverse *10+ Reminder;

Number = Number /10;

printf("Reverse of entered number is = %d\n", Reverse);

return 0;

Output

Please Enter any number to Reverse

4567

Reverse of entered number is 7654

First Semester BCA (Hons.) 20


Software Lab in C

14. Write a C program to generate Fibonacci series up to the


limit.

Program

#include <stdio.h>

#include <conio.h>

void main()

int a, b, c, i, n;

clrscr();

printf("Enter the limit: ");

scanf("%d", &n);

a = 0;

b = 1;

c = 0;

printf("Fibonacci series up to the given limit are:\n");

for(i=1; i<=n; i++)

printf("%d, ", c);

a = b;

b = c;

First Semester BCA (Hons.) 21


Software Lab in C

c = a + b;

getch();

Output

Enter the limit: 10

Fibonacci series up to the given limit are:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

First Semester BCA (Hons.) 22


Software Lab in C

15. Write a C program to find the factorial of a number using


function.

Program

#include <stdio.h>

int main()

int num;

printf("Enter an integer number :");

scanf("%d",&num);

printf("Factorial of %d is = %ld",num,factorial(num));

return 0;

long int factorial(int n)

int i;

long int fact=1;

if(n==1) return fact;

for(i=n;i>=1;i--)

First Semester BCA (Hons.) 23


Software Lab in C

fact= fact * i;

return fact;

Output

Enter an integer number: 7

Factorial of 7 is = 5040

First Semester BCA (Hons.) 24


Software Lab in C

16. Write a C program to add two numbers using function.

a)Function with arguments and return value.

Program

#include <stdio.h>

int add(int a,int b);

void main()

{ int num1,num2,sum=0;

printf("Enter two numbers\n");

scanf("%d%d",&num1,&num2);

sum=add(num1,num2);

printf("Sum =%d",sum);

int add(int a,int b)

int result;

result=a+b;

return result;

Output

Enter two numbers

15

21

Sum=36

First Semester BCA (Hons.) 25


Software Lab in C

b) Function with arguments and without return value.

Program

#include <stdio.h>

void add(int a,int b);

void main()

int num1,num2;

printf("Enter two numbers\n");

scanf("%d%d",&num1,&num2);

add(num1,num2);

void add(int a,int b)

int result;

result=a+b;

printf("Sum =%d",result);

Output

Enter two numbers

40

50

Sum=90

First Semester BCA (Hons.) 26


Software Lab in C

c) Function without arguments and with return value.

Program

#include <stdio.h>

int add();

void main()

int sum=0;

sum=add();

printf("Sum =%d",sum);

int add()

int result,num1,num2;

printf("Enter two numbers\n");

scanf("%d%d",&num1,&num2);

result=num1+num2;

return result;

Output

Enter two numbers

30

20

Sum = 50

First Semester BCA (Hons.) 27


Software Lab in C

d) Function without arguments and without return value.

Program

#include <stdio.h>

void add();

void main()

add();

void add()

int result,num1,num2;

printf("Enter two numbers\n");

scanf("%d%d",&num1,&num2);

result=num1+num2;

printf("Sum =%d",result);

Output

Enter two numbers

20

25

Sum = 45

First Semester BCA (Hons.) 28


Software Lab in C

17. Write a C program to search an element in an array.

Program

#include<stdio.h>

int main() {

int a[30], ele, num, i;

printf("\nEnter no of elements :");

scanf("%d", &num);

printf("\nEnter the values :");

for (i = 0; i < num; i++) {

scanf("%d", &a[i]);

//Read the element to be searched

printf("\nEnter the elements to be searched :");

scanf("%d", &ele);

//Search starts from the zeroth location

i = 0;

while (i < num && ele != a[i]) {

i++;

First Semester BCA (Hons.) 29


Software Lab in C

//If i < num then Match found

if (i < num) {

printf("Number found at the location = %d", i + 1);

} else {

printf("Number not found");

return (0);

Output

Enter no of elements : 5

11 22 33 44 55

Enter the elements to be searched : 44

Number found at the location = 4

First Semester BCA (Hons.) 30


Software Lab in C

18. Write a C program to perform sorting operation both


ascending and descending order.

Program

#include <stdio.h>

int main(void)

int a[10], i=0, j=0, n, t;

printf ("\n Enter the no. of elements: ");

scanf ("%d", &n);

printf ("\n");

for (i = 0; i <n; i++)

printf ("\n Enter the %dth element: ", (i+1));

scanf ("%d", &a[i]);

for (j=0 ; j<(n-1) ; j++)

for (i=0 ; i<(n-1) ; i++)

if (a[i+1] < a[i])

t = a[i];

a[i] = a[i + 1];

First Semester BCA (Hons.) 31


Software Lab in C

a[i + 1] = t;

printf ("\n Ascending order: ");

for (i=0 ; i<n ; i++)

printf (" %d", a[i]);

printf ("\n Descending order: ");

for (i=n ; i>0 ; i--)

printf (" %d", a[i-1]);

/* indicate successful completion */

return 0;

Output

Enter the no. of elements: 5

Enter the 1th element: 25

Enter the 2th element: 50

First Semester BCA (Hons.) 32


Software Lab in C

Enter the 3th element: 75

Enter the 4th element: 35

Enter the 5th element: 100

Ascending order: 25 35 50 75 100

Descending order: 100 75 50 35 25

First Semester BCA (Hons.) 33


Software Lab in C

19. Write a C program to perform matrix addition.

Program

#include<stdio.h>

int main() {

int i, j, mat1[10][10], mat2[10][10], mat3[10][10];

int row1, col1, row2, col2;

printf("\nEnter the number of Rows of Mat1 : ");

scanf("%d", &row1);

printf("\nEnter the number of Cols of Mat1 : ");

scanf("%d", &col1);

printf("\nEnter the number of Rows of Mat2 : ");

scanf("%d", &row2);

printf("\nEnter the number of Columns of Mat2 : ");

scanf("%d", &col2);

/* Before accepting the Elements Check if no of

rows and columns of both matrices is equal */

if (row1 != row2 || col1 != col2) {

printf("\nOrder of two matrices is not same ");

exit(0);

First Semester BCA (Hons.) 34


Software Lab in C

//Accept the Elements in Matrix 1

for (i = 0; i < row1; i++) {

for (j = 0; j < col1; j++) {

printf("Enter the Element a[%d][%d] : ", i, j);

scanf("%d", &mat1[i][j]);

//Accept the Elements in Matrix 2

for (i = 0; i < row2; i++)

for (j = 0; j < col2; j++) {

printf("Enter the Element b[%d][%d] : ", i, j);

scanf("%d", &mat2[i][j]);

//Addition of two matrices

for (i = 0; i < row1; i++)

for (j = 0; j < col1; j++) {

mat3[i][j] = mat1[i][j] + mat2[i][j];

//Print out the Resultant Matrix

First Semester BCA (Hons.) 35


Software Lab in C

printf("\nThe Addition of two Matrices is : \n");

for (i = 0; i < row1; i++) {

for (j = 0; j < col1; j++) {

printf("%d\t", mat3[i][j]);

printf("\n");

return (0);

Output

Enter the number of Rows of Mat1 : 3

Enter the number of Columns of Mat1 : 3

Enter the number of Rows of Mat2 : 3

Enter the number of Columns of Mat2 : 3

Enter the Element a[0][0] : 1

Enter the Element a[0][1] : 2

Enter the Element a[0][2] : 3

Enter the Element a[1][0] : 2

Enter the Element a[1][1] : 1

Enter the Element a[1][2] : 1

Enter the Element a[2][0] : 1

Enter the Element a[2][1] : 2

First Semester BCA (Hons.) 36


Software Lab in C

Enter the Element a[2][2] : 1

Enter the Element b[0][0] : 1

Enter the Element b[0][1] : 2

Enter the Element b[0][2] : 3

Enter the Element b[1][0] : 2

Enter the Element b[1][1] : 1

Enter the Element b[1][2] : 1

Enter the Element b[2][0] : 1

Enter the Element b[2][1] : 2

Enter the Element b[2][2] : 1

The Addition of two Matrices is :

246

422

242

First Semester BCA (Hons.) 37


Software Lab in C

20. Write a C program to perform string manipulations.

Program

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

int main(){

char string1[20], string2[20]; //string variables declaration with size 20

int choice;

while(1){

printf("\n1. Find Length \n2. Concatenate \n3. Compare \n4. Copy \n5.
Exit\n");

printf("Enter your choice: ");

scanf("%d",&choice);

switch(choice){

case 1:

printf("Enter the string: ");

scanf("%s",string1);

printf("The length of string is %d", strlen(string1));

break;

case 2:

printf("Enter two strings: ");

scanf("%s%s",string1,string2);

strcat(string1,string2);

printf("The concatenated string is %s", string1);

First Semester BCA (Hons.) 38


Software Lab in C

break;

case 3:

printf("Enter two strings: ");

scanf("%s%s",string1,string2);

if(strcmp(string1,string2)==0){

printf("They are equal");

}else{

printf("They are not equal");

break;

case 4:

printf("Enter a string: ");

scanf("%s",string1);

printf("String1 = %s\n",string1);

printf("After copying string1 to string 2\n");

strcpy(string2,string1);

printf("String2 = %s",string2);

break;

case 5:

exit(0);

return 0;

First Semester BCA (Hons.) 39


Software Lab in C

Output

1. Find Length

2. Concatenate

3. Compare

4. Copy

5. Exit

Enter your choice: 1

Enter the string: alazhar

The length of string is 7

1. Find Length

2. Concatenate

3. Compare

4. Copy

5. Exit

Enter your choice: 2

Enter two strings: alazhar

college

The concatenated string is alazharcollege

1. Find Length

2. Concatenate

First Semester BCA (Hons.) 40


Software Lab in C

3. Compare

4. Copy

5. Exit

Enter your choice: 3

Enter two strings: thodupuzha

kerala

They are not equal

1. Find Length

2. Concatenate

3. Compare

4. Copy

5. Exit

Enter your choice: 4

Enter a string: alazhar

String1 = alazhar

After copying string1 to string 2

String2 = alazhar

1. Find Length

2. Concatenate

3. Compare

4. Copy

5. Exit

Enter your choice: 5

First Semester BCA (Hons.) 41

You might also like