0% found this document useful (0 votes)
98 views22 pages

SEP BCA - C LAB MANUAL

The document is a C Programming Lab manual that includes various programming exercises and their solutions. It covers topics such as generating Fibonacci numbers, finding GCD and LCM, checking for prime numbers, and performing matrix operations. Each exercise is accompanied by a code snippet and is prepared by a lecturer from the Department of Computer Applications.

Uploaded by

Sahana M.k
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)
98 views22 pages

SEP BCA - C LAB MANUAL

The document is a C Programming Lab manual that includes various programming exercises and their solutions. It covers topics such as generating Fibonacci numbers, finding GCD and LCM, checking for prime numbers, and performing matrix operations. Each exercise is accompanied by a code snippet and is prepared by a lecturer from the Department of Computer Applications.

Uploaded by

Sahana M.k
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/ 22

SEP- BCA : C PROGRAMMING LAB

C – PROGRAMMING LAB MANUAL

PART - A
1. Write a C program to generate and print first N FIBONACCI numbers.
#include <stdio.h>
#include<conio.h>
void main( )
{
int fib1 = 0, fib2 = 1, fib3, num, count = 0;
clrscr( );
printf("Enter the value of num \n");
scanf("%d", &num);
printf("First %d FIBONACCI numbers are ...\n", num);
printf("%d\n", fib1);
printf("%d\n", fib2);
count = 2;
while (count < num)
{
fib3 = fib1 + fib2;
count++;
printf("%d\n", fib3);
fib1 = fib2;
fib2 = fib3;
}
getch( );
}

Prepared by: SOWMYA RANI G S, LECTURER, DEPT. OF COMPUTER APPLICATIONS CTA. Page
1
SEP- BCA : C PROGRAMMING LAB

2. Write a C program to find the GCD and LCM of two integer numbers.
#include <stdio.h>
#include<conio.h>
void main( )
{
int num1, num2, gcd, lcm, remainder, numerator, denominator;
clrscr( );
printf("Enter two numbers:\n");
scanf("%d %d", &num1, &num2);
numerator = (num1>num2)?num1:num2;
denominator = (num1<num2)?num1:num2;
remainder = numerator % denominator;

while (remainder != 0)
{
numerator = denominator;
denominator = remainder;
remainder = numerator % denominator;
}
gcd = denominator;
lcm = num1 * num2 / gcd;
printf("GCD of %d and %d = %d\n", num1, num2, gcd);
printf("LCM of %d and %d = %d\n", num1, num2, lcm);
getch( );
}

Prepared by: SOWMYA RANI G S, LECTURER, DEPT. OF COMPUTER APPLICATIONS CTA. Page
2
SEP- BCA : C PROGRAMMING LAB

3. Write a C program to find biggest of three numbers.


#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 && a > c)


printf("Biggest number is %d", a);
if (b > a && b > c)
printf("Biggest number is %d", b);
if (c > a && c > b)
printf("Biggest number is %d", c);
getch( );
}

Prepared by: SOWMYA RANI G S, LECTURER, DEPT. OF COMPUTER APPLICATIONS CTA. Page
3
SEP- BCA : C PROGRAMMING LAB

4. Write a C program that reverse a given integer number and check whether the
number is palindrome or not.
#include <stdio.h>
#include<conio.h>
void main( )
{
int num, temp, remainder, reverse = 0;
clrscr( );
printf("Enter an integer \n");
scanf("%d", &num);
temp = num;
while (num > 0)
{
remainder = num % 10;
reverse = reverse * 10 + remainder;
num /= 10;
}
printf("Given number is = %d\n", temp);
printf("Its reverse is = %d\n", reverse);
if (temp == reverse)
printf("Number is a palindrome \n");
else
printf("Number is not a palindrome \n");
getch( );
}

Prepared by: SOWMYA RANI G S, LECTURER, DEPT. OF COMPUTER APPLICATIONS CTA. Page
4
SEP- BCA : C PROGRAMMING LAB

5. Write a C program to construct simple calculator using if statement.


#include <stdio.h>
#include< conio.h>
void main( )
{
int number1, number2;
float answer;
char op;
clrscr( );
printf (" Enter the operation to perform(+, -, *, /) \n ");
scanf ("%c", &op);
printf (" Enter the first number: ");
scanf(" %d", &number1);
printf (" Enter the second number: ");
scanf (" %d", &number2);
if (op == '+')
{
answer = number1 + number2;
printf (" %d + %d = %f", number1, number2, answer);
}
else if (op == '-')
{
answer = number1 - number2;
printf (" %d - %d = %f", number1, number2, answer);
}
else if (op == '*')
{
answer = number1 * number2;
printf (" %d * %d = %f", number1, number2, answer);
}
else if (op == '/')
{
if (number2 == 0)
{
printf (" \n Divisor cannot be zero. Please enter another value ");
scanf ("%d", &number2);
}
answer = number1 / number2;
printf (" %d / %d = %.2f", number1, number2, answer);
}
else
{
printf(" \n Enter valid operator ");
}
getch( );
}

Prepared by: SOWMYA RANI G S, LECTURER, DEPT. OF COMPUTER APPLICATIONS CTA. Page
5
SEP- BCA : C PROGRAMMING LAB

6. Write a C program to find whether a given number is prime number or not.


#include <stdio.h>
#include<conio.h>
void main( )
{
int n, i, flag = 0;
clrscr( );
printf("Enter a positive integer: ");
scanf("%d", &n);

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


{
if(n%i == 0)
{
flag = 1;
break;
}
}

if (n == 1)
{
printf("1 is neither prime nor composite.");
}
else
{
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
}

getch( );
}

Prepared by: SOWMYA RANI G S, LECTURER, DEPT. OF COMPUTER APPLICATIONS CTA. Page
6
SEP- BCA : C PROGRAMMING LAB

7. Write a C program to input numbers and to find mean, variance and standard
deviation.
#include<stdio.h>
#include<math.h>
#include<conio.h>
void main( )
{
int i,n;
float std_dev,sum=0,sumsqr=0,mean,value,variance=0.0,a[100];
clrscr( );
printf("Enter value of n : ");
scanf("%d",&n);
printf("\nEnter numbers : ");
for(i=0;i<n;i++)
{
printf("\nNumber %d : ",i+1);
scanf("%f",&a[i]);
sum=sum+a[i];
}
mean=sum/n;
sumsqr=0;
for(i=0;i<n;i++)
{
value=a[i]-mean;
sumsqr=sumsqr+value*value;
}
variance=sumsqr/n;
std_dev=sqrt(variance);
printf("\nMean of %d numbers = %f\n",n,mean);
printf("\nVariance of %d numbers = %f\n",n,variance);
printf("\nStandard deviation of %d numbers = %f\n",n,std_dev);
getch( );
}

Prepared by: SOWMYA RANI G S, LECTURER, DEPT. OF COMPUTER APPLICATIONS CTA. Page
7
SEP- BCA : C PROGRAMMING LAB

8. Write a C program to print largest number with its position in an array.


#include<stdio.h>
#include<conio.h>
void main( )
{
int i, max_index, arr[5], max;
clrscr( );
printf("Please enter five numbers:\n ");
for (i = 0; i < 5; ++i)
{
scanf("%d", &arr[i]);
}
max = arr[0];
for (i = 0; i < 5; i++)
{
if (arr[i] > max)
{
max = arr[i];
max_index = i;
}
}
printf("Largest element = %d at index %d", max, max_index);

getch( );
}

Prepared by: SOWMYA RANI G S, LECTURER, DEPT. OF COMPUTER APPLICATIONS CTA. Page
8
SEP- BCA : C PROGRAMMING LAB

9. Write a C program to search an element using linear search method.


#include <stdio.h>
#include<conio.h>
void main( )
{
int a[20],key,n,i,flag=0;
printf("Enter the size of an array : ");
scanf("%d",&n);
printf("Enter the array data ");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
printf("\nArray elements are below\n");
for(i=0; i<n; i++)
printf("%d, ",a[i]);
printf("\nEnter the searching element : ");
scanf("%d",&key);
for(i=0; i<n; i++)
{
if(a[i]==key)
{
flag = 1;
break;
}
}
if(flag==1)
printf("\n%d is found at %d position",key,i+1);
else
printf("\n%d is not found !!!");
getch( );
}

Prepared by: SOWMYA RANI G S, LECTURER, DEPT. OF COMPUTER APPLICATIONS CTA. Page
9
SEP- BCA : C PROGRAMMING LAB

10. Write a C program to print string in reverse orders using pointers.


#include <stdio.h>
#include <string.h>
#include<conio.h>
int str_len( char *st);
void revstr( char *st);
void main()
{
char st[50];
clrscr( );
printf (" Enter a string to be reversed: ");
scanf( "%s", st);
revstr(st);
printf (" The reverse string is: %s", st);
getch( );
}

void revstr (char *st)


{
int len, i;
char *start, *end, temp;
len = str_len (st);
start = st;
end = st;
for (i = 0; i < len - 1; i++)
end++;
for (i = 0; i < len/2; i++)
{
temp = *end;
*end = *start;
*start = temp;
start++;
end--;
}
}
int str_len (char *ptr)

Prepared by: SOWMYA RANI G S, LECTURER, DEPT. OF COMPUTER APPLICATIONS CTA. Page
10
SEP- BCA : C PROGRAMMING LAB

{
int i = 0;
while ( *(ptr + i) != '\0')
i++;
return i;
}
PART – B

1. Write a C program to read two matrices and perform addition and subtraction
of two matrices.
#include<stdio.h>
#include<conio.h>
void main( )
{
int n, m, c, d, first[10][10], second[10][10], sum[10][10], diff[10][10];
clrscr( );
printf("\nEnter the number of rows and columns of the first matrix \n\n");
scanf("%d%d", &m, &n);

printf("\nEnter the %d elements of the first matrix \n\n", m*n);


for(c = 0; c < m; c++)
for(d = 0; d < n; d++)
scanf("%d", &first[c][d]);

printf("\nEnter the %d elements of the second matrix \n\n", m*n);


for(c = 0; c < m; c++)
for(d = 0; d < n; d++)
scanf("%d", &second[c][d]);

printf("\n\nThe first matrix is: \n\n");


for(c = 0; c < m; c++)
{
for(d = 0; d < n; d++)
{
printf("%d\t", first[c][d]);
}
printf("\n");
}

printf("\n\nThe second matrix is: \n\n");


for(c = 0; c < m; c++)
{
for(d = 0; d < n; d++)

Prepared by: SOWMYA RANI G S, LECTURER, DEPT. OF COMPUTER APPLICATIONS CTA. Page
11
SEP- BCA : C PROGRAMMING LAB

{
printf("%d\t", second[c][d]);
}
printf("\n");
}

for(c = 0; c < m; c++)


for(d = 0; d < n; d++)
sum[c][d] = first[c][d] + second[c][d];

printf("\n\nThe sum of the two entered matrices is: \n\n");


for(c = 0; c < m; c++)
{
for(d = 0; d < n; d++)
{
printf("%d\t", sum[c][d]);
}
printf("\n");
}

for(c = 0; c < m; c++)


for(d = 0; d < n; d++)
diff[c][d] = first[c][d] - second[c][d];

printf("\n\nThe difference(subtraction) of the two entered matrices is: \n\n");


for(c = 0; c < m; c++)
{
for(d = 0; d < n; d++)
{
printf("%d\t", diff[c][d]);
}
printf("\n");
}

printf("\n\n\t\t\tCoding is Fun !\n\n\n");


getch( );
}

Prepared by: SOWMYA RANI G S, LECTURER, DEPT. OF COMPUTER APPLICATIONS CTA. Page
12
SEP- BCA : C PROGRAMMING LAB

2. Write a C program to read two matrices and perform multiplication of two


matrices.

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

void main()
{
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
clrscr();
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]);
}
}
printf("multiply of the matrix=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
Prepared by: SOWMYA RANI G S, LECTURER, DEPT. OF COMPUTER APPLICATIONS CTA. Page
13
SEP- BCA : C PROGRAMMING LAB

mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}

for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
getch();
}

3. Write a C program to read a string and check whether it is palindrome or not.


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

void main()
{
char string1[20];
int i, length;
int flag = 0;
clrscr();
printf("Enter a string: ");
scanf("%s", string1);
length = strlen(string1);
for (i = 0; i < length / 2; i++)
{
if (string1[i] != string1[length - i - 1])
{
flag = 1;
break;
}
}
if (flag)

Prepared by: SOWMYA RANI G S, LECTURER, DEPT. OF COMPUTER APPLICATIONS CTA. Page
14
SEP- BCA : C PROGRAMMING LAB

{
printf("%s is not a palindrome\n", string1);
}
else
{
printf("%s is a palindrome\n", string1);
}
getch( );
}
4. Write a C program to find the factorial of a number using function.
#include<stdio.h>
#include<math.h>
void main()
{
clrscr();
printf("Enter a Number to Find Factorial: ");
printf("\nFactorial of a given number is: %d ",fact());
getch();
}
int fact()
{
int i,fact=1,n;
scanf("%d",&n);
for(i=1; i<=n; i++)
{
fact=fact*i;
}
return fact;
}

Prepared by: SOWMYA RANI G S, LECTURER, DEPT. OF COMPUTER APPLICATIONS CTA. Page
15
SEP- BCA : C PROGRAMMING LAB

5. Write a C program to check whether a character is vowel or consonant using


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

void main()
{
char ch;
clrscr();
printf("Enter any alphabet: ");
scanf("%c", &ch);
switch(ch)
{
case 'a': printf("Vowel");
break;
case 'e': printf("Vowel");
break;
case 'i': printf("Vowel");
break;
case 'o': printf("Vowel");
break;
case 'u': printf("Vowel");
break;
case 'A': printf("Vowel");
break;
case 'E': printf("Vowel");
break;
case 'I': printf("Vowel");
break;
case 'O': printf("Vowel");
break;
case 'U': printf("Vowel");
break;

Prepared by: SOWMYA RANI G S, LECTURER, DEPT. OF COMPUTER APPLICATIONS CTA. Page
16
SEP- BCA : C PROGRAMMING LAB

default:
printf("Consonant");
}
getch();
}

6. Write a C program to find if a character is alphabetic, numeric or special


character.

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

void main()
{
char ch;
clrscr();
printf("Enter any character: ");
scanf("%c", &ch);

if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
printf("'%c' is alphabet.", ch);
}
else if(ch >= '0' && ch <= '9')
{
printf("'%c' is digit.", ch);
}
else
{
printf("'%c' is special character.", ch);
}
getch();
}

Prepared by: SOWMYA RANI G S, LECTURER, DEPT. OF COMPUTER APPLICATIONS CTA. Page
17
SEP- BCA : C PROGRAMMING LAB

7. Write a c program to compute the sum of even numbers and the sum of odd
numbers using a function.
#include <stdio.h>
#include <conio.h>

int sumOfEvens(int n)
{
int i, evensum = 0;
for ( i = 1; i <= n; i++)
{
if (i % 2 == 0)
{
evensum += i;
}
}
return(evensum);
}
int sumOfOdds(int n)
{
int i,oddsum=0;
for ( i = 1; i <= n; i++)
{
if (i % 2!= 0)
{
oddsum +=i;
}
}
return(oddsum);
}

void main( )

Prepared by: SOWMYA RANI G S, LECTURER, DEPT. OF COMPUTER APPLICATIONS CTA. Page
18
SEP- BCA : C PROGRAMMING LAB

{
int n,evenresult, oddresult;
clrscr();
printf("Enter a positive integer: ");
scanf("%d", &n);
evenresult=sumOfEvens(n);
printf("The sum of all even numbers between 1 and %d is %d\n", n, evenresult);
oddresult=sumOfOdds(n);
printf("The sum of all odd numbers between 1 and %d is %d\n", n, oddresult);
getch();
}

8. Write a C program to find trace and norm of a square matrix using functions.
#include <math.h>
#include <stdio.h>
#include <conio.h>
int findNormal(int mat[10][10],int n)
{
int sum = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
sum += mat[i][j] * mat[i][j];
return sqrt(sum);
}
int findTrace(int mat[10][10], int n)
{
int sum = 0;
for (int i = 0; i < n; i++)
sum += mat[i][i];
return sum;
}
void main()
{
int mat[10][10], i, j, n;
clrscr( );
printf("Enter the order of the matrix \n");
scanf(" %d",&n);
printf("Enter the matrix elements \n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d", &mul[i][j]);

Prepared by: SOWMYA RANI G S, LECTURER, DEPT. OF COMPUTER APPLICATIONS CTA. Page
19
SEP- BCA : C PROGRAMMING LAB

}
}
printf("The matrix is :");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
printf("Normal of Matrix = %d", findNormal(mat, n));
printf("\nTrace of Matrix = %d", findTrace(mat, n));
getch();
}
9. Write a C program to accept different goods with the number, price and date of
purchase and display those using structures.
#include<stdio.h>
#include<conio.h>
void main()
{
struct date
{
int day;
int month;
int year;
};
struct details
{
char name[20];
int price;
int code;
int qty;
struct date purchase;
};
struct details item[50];

Prepared by: SOWMYA RANI G S, LECTURER, DEPT. OF COMPUTER APPLICATIONS CTA. Page
20
SEP- BCA : C PROGRAMMING LAB

10. Write a C program to find the length of the string without using the built in
function.

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

void main()
{
char string[50];
int i, length = 0;
clrscr();
printf("Enter the string: \n");
gets(string);
for (i = 0; string[i] != '\0'; i++)
{
length++;
}
printf("The length of a string is the number of characters in it \n");
printf("So, the length of %s = %d\n", string, length);
Prepared by: SOWMYA RANI G S, LECTURER, DEPT. OF COMPUTER APPLICATIONS CTA. Page
21
SEP- BCA : C PROGRAMMING LAB

getch();
}

Prepared by: SOWMYA RANI G S, LECTURER, DEPT. OF COMPUTER APPLICATIONS CTA. Page
22

You might also like