0% found this document useful (0 votes)
53 views33 pages

LabProgramsBCA 1

The document contains 20 code snippets of C programs that demonstrate various programming concepts like finding area of shapes, determining largest of three numbers, reversing digits of an integer, computing GCD and Fibonacci numbers, identifying even/odd numbers, exchanging variable values, finding sum and count of numbers divisible by 7, computing factorial and summation, determining maximum/minimum elements in arrays, counting vowels/consonants in a string, adding and subtracting matrices.

Uploaded by

nandishimurgod
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)
53 views33 pages

LabProgramsBCA 1

The document contains 20 code snippets of C programs that demonstrate various programming concepts like finding area of shapes, determining largest of three numbers, reversing digits of an integer, computing GCD and Fibonacci numbers, identifying even/odd numbers, exchanging variable values, finding sum and count of numbers divisible by 7, computing factorial and summation, determining maximum/minimum elements in arrays, counting vowels/consonants in a string, adding and subtracting matrices.

Uploaded by

nandishimurgod
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/ 33

1.

Find the Area of Circle and Area of a Triangle


given three sides.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float r,a,b,c,s, Tarea, Carea;
clrscr();
printf("Enter the sides of triangle \n");
scanf("%f%f%f",&a,&b,&c);
s=(a+b+c)/2;
Tarea=sqrt(s*(s-a)*(s-b)*(s-c));
printf("Area of Triangle is %f\n ", Tarea);
printf("Enter the Radius");
scanf("%f",&r);
Carea=3.142*r*r;
printf("Area of Circle is %f", Carea);
getch();
}
OUTPUT:
2. Write c Program to find Largest of Three
numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,n3;
clrscr();
printf("Enter Three Numbers");
scanf("%d%d%d",&n1,&n2,&n3);
if(n1>=n2)
{
if(n1>=n3)
printf("The largest number is %d",n1);
else
printf("The largest number is %d",n2);
}
else
{
if(n2>=n3)
printf("The largest number is %d ",n2);
else
printf("The largest number is %d",n3);
}
getch();
}
OUTPUT:
3. Write C program to Reversing the digits of an
Integer.
#include<stdio.h>
#include<conio.h>
void main()
{
int n, sum=0,r;
clrscr();
printf("Enter the value of n");
scanf("%d",&n);
while(n>0)
{
r=n%10;
sum=sum*10+r;
n=n/10;
}
printf("Reverse of the given number is %d",sum);
getch();
}
OUTPUT:
4. Write a C Program to find GCD of Two Integers
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2, GCD;
clrscr();
printf("Enter the value od n1 & n2");
scanf("%d%d",&n1,&n2);
while(n1!=n2)
{
if(n1>n2)
{
n1=n1-n2;
}
else
{
n2=n2-n1;
}
}
printf("GCD of given Two numbers is = %d",n1);
getch();
}
OUTPUT:

5. Write c Program to Generating PRIME


Numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;
clrscr();
printf("Enter the Number");
scanf("%d",&n);
printf("Prime Numbers are");
for(i=2;i<n;i++)
{
int count=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
{
count++;
}
}
if(count==2)
{
printf("%d\n",i);
}
}
getch();
}
OUTPUT:
6. Write C Program to Computing Nth Fibonacci
Numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int n, t1 = 0, t2 = 1, nextTerm = 0, i;
clrscr();
printf("Enter the n value: ");
scanf("%d", &n);
if(n == 0 || n == 1)
printf("%d", n);
else
nextTerm = t1 + t2;
for (i = 3; i <= n; ++i)
{
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
printf("%d", t2);
getch();
}
OUTPUT:
7. Write C Program to Finding Even and Odd
Numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("enter the value of n ");
scanf("%d",&n);
if(n%2==0)
{
printf("%d is Even number",n);
}
else
{
printf("%d is Odd number ",n);
}
getch();
}
OUTPUT:

8. Write C Program to Exchanging the values of


two variables.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,temp;
clrscr();
printf("Enter the value of a & b\n");
scanf("%d%d",&a,&b);
printf("Before Exchanging \n a=%d \n b=%d \n
",a,b);
temp=a;
a=b;
b=temp;
printf("After Exchanging \n a=%d \n b=%d \n ",a,b);
getch();
}
OUTPUT:

9. Write a C program for Counting: print number


from 100 to 200 which are
divisible by 7 and display their sum and count using
for loop
#include<stdio.h>
#include<conio.h>
void main()
{
int n, sum=0, count=0;
clrscr();
for(n=100;n<=200;n++)
{
if(n%7==0)
{
printf("%d\n",n);
sum=sum+n;
n++;
count++;
}
}
printf("Sum= %d\n",sum);
printf("Count=%d\n",count);
getch();
}
OUTPUT:
10. Write C program for Summation of set of
numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i, sum=0;
clrscr();
printf("Enter the value");
scanf("%d", &n);
for(i=1;i<=n;i++)
{
sum=sum+i;
}
printf("Summation of n numbers is %d", sum);
getch();
}
OUTPUT:

11.Write C program for Computation of Factorial


#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,f=1;
clrscr();
printf("Enter n value");
scanf("%d", &n);
for(i=1;i<=n;i++)
{
f=f*i;
}
printf("Factorial of given number id %d", f);
getch();
}

OUTPUT:

12. Writ a C Program for Generation of Fibonacci


Sequence.
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, c, i, n;
clrscr();
a=0;b=1;
printf("Enter the limit of fibonacci Series ");
scanf("%d", &n);
printf(" * * * Fibonacci Series * * * \n");
for(i=0;i<n;i++)
{
printf("%d\n",a);
c=a+b;
a=b;
b=c;
}
getch();
}
OUTPUT:

+
13. Write a c program to Array Order Reversal.
#include<stdio.h>
#include<conio.h>
void main()
{
int i, a[5];
clrscr();
printf("enter 5 Elements of Array \n");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
printf("Reversed Array Elements are \n");
for(i=4;i>=0;i--)
printf("%d\n",a[i]);
getch();
}
OUTPUT:
14. Finding the maximum number in the set.
#include<stdio.h>
#include<conio.h>
void main()
{
int i, num, n, max=0;
clrscr();
printf("How Many numbers in a set \n");
scanf("%d",&n);
printf("Enter numbers");
for(i=0;i<n;i++)
{
scanf("%d",&num);
if(num>max)
max=num;
}
printf("The maximum number is %d ", max);
getch();
}
OUTPUT:

15. Write a program to Removal of Duplicates


from an Ordered Array .
#include<stdio.h>
#include<conio.h>
int removeDuplicates(int arr[], int n)
{ int temp[10];
int j = 0, i;
if (n == 0 || n == 1)
return n;
for (i = 0; i < n - 1; i++)
if (arr[i] != arr[i + 1])
temp[j++] = arr[i];
temp[j++] = arr[n - 1];
for (i = 0; i < j; i++)
arr[i] = temp[i];
return j;
}
void main()
{
int arr[] = {1, 2, 2, 3, 4, 4, 4, 5, 5};
int n = sizeof(arr) / sizeof(arr[0]);
int i;
clrscr();
n = removeDuplicates(arr, n);
printf("After removing Duplicate values in array is
\n");
for ( i = 0; i < n; i++)
printf("%d\n",arr[i]);
getch();
}
OUTPUT:

16.Write a C program for Partition of an Array


#include<stdio.h>
#include<conio.h>
#define N 10
void main()
{
int a[N], arr1[N], arr2[N], i, pos, k1 = 0, k2 = 0;
clrscr();
printf("Enter %d integer numbers\n", N);
for(i = 0; i < N; i++)
scanf("%d", &a[i]);
printf("Enter position to split the array in to Two\n");
scanf("%d", &pos);
for(i = 0; i < N; i++)
{
if(i < pos)
arr1[k1++] = a[i];
else
arr2[k2++] = a[i];
}
printf("\nElements of First Array -> arr1[%d]\n",
k1);
for(i = 0; i < k1; i++)
printf("%d\n", arr1[i]);
printf("\nElements of Second Array -> arr2[%d]\n",
k2);
for(i = 0; i < k2; i++)
printf("%d\n", arr2[i]);
printf("\n");
getch();
}
OUTPUT:
17. Write a C Program for Finding the Smallest
Elements
#include<stdio.h>
#include<conio.h>
void main()
{
int a[50], n, i,small;
clrscr();
printf("Enter size of Array");
scanf("%d",&n);
printf("Enter %d Elements \n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
small=a[0];
for(i=0;i<n;i++)
{
if(small>a[i])
{
small=a[i];
}
}
printf("Smallest element is %d",small);
getch();
}
OUTPUT:
18. Read N(minimum) Students Marks and find
number of Students Passed and
Failed depending on the Marks
#include<stdio.h>
#include<conio.h>
void main()
{
int i=0, marks , n, countP=0,countF=0;
clrscr();
printf("Enter the total number of Students \n");
scanf("%d",&n);
while(i<n)
{
i=i+1;
printf("Enter %d Students marks ",i);
scanf("%d",&marks);
if(marks>35)
countP=countP+1;
else
countF=countF+1;
}
printf("Number of Passed Students are
=%d",countP);
printf("Number of Failed Students are =
%d",countF);
getch();
}
OUTPUT:

19. Write C Program Count the number of Vowels,


Consonants & Special Characters in a given
Sentence.
#include<stdio.h>
#include<conio.h>
void main()
{
char str[50];
int Vcount=0,Ccount=0,
SPLcount=0,NUMcount=0,Scount=0,i;
clrscr();
printf("Enter a Sentence\n");
gets(str);
for(i=0;str[i]!='\0';i++)
{
if(str[i]=='a'||
str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u'||

str[i]=='A'||str[i]=='E'||str[i]=='I'||str[i]=='O'||str[i]=='U')
{
Vcount++;
}
else
if(str[i]=='@'||str[i]=='%'||str[i]=='#'||str[i]=='&'||
str[i]=='*'||str[i]=='$'||str[i]=='^'||str[i]=='|')
{
SPLcount++;
}
else if(str[i]>='O'&&str[i]<='a')
{
NUMcount++;
}
else if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&
str[i]<='z'))
{
Ccount++;
}
else if(str[i]==' ')
{
Scount++;
}
}
printf("Vowels are =%d \n",Vcount);
printf("Consonents are =%d \n",Ccount);
printf("Special Characters are = %d \n ",
SPLcount);
printf(" space Character are =%d ",Scount);
getch();
}
OUTPUT:

20. Write C Program to Find the Addition and


Subtraction of two Matrices using Function.
#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++)
{
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");
}
getch();
}
OUTPUT:

You might also like