SEP BCA - C LAB MANUAL
SEP BCA - C 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
void main()
{
int a, b, c;
clrscr( );
printf("Enter three numbers: \n ");
scanf("%d %d %d", &a, &b, &c);
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
Prepared by: SOWMYA RANI G S, LECTURER, DEPT. OF COMPUTER APPLICATIONS CTA. Page
5
SEP- BCA : C PROGRAMMING LAB
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
getch( );
}
Prepared by: SOWMYA RANI G S, LECTURER, DEPT. OF COMPUTER APPLICATIONS CTA. Page
8
SEP- BCA : C PROGRAMMING LAB
Prepared by: SOWMYA RANI G S, LECTURER, DEPT. OF COMPUTER APPLICATIONS CTA. Page
9
SEP- BCA : C PROGRAMMING LAB
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);
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");
}
Prepared by: SOWMYA RANI G S, LECTURER, DEPT. OF COMPUTER APPLICATIONS CTA. Page
12
SEP- BCA : C PROGRAMMING LAB
#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();
}
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
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();
}
#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