practical_file
practical_file
Practical File
Subject Title:C programming lab-I Subject Code:BCA0104(p)
No. of Pages:23
(For Teachers)
1. Practical 1 3
2. Practical 2 4
3. Practical 3 5
4. Practical 4 6
5. Practical 5 7
6. Practical 6 8-9
7. Practical 7 10-11
8. Practical 8 12-14
9. Practical 9 15
10. Practical 10 16-17
11. Practical 11 18
12. Practical 12 19
13. Practical 13 20-21
14. Practical 14 22-23
15. Practical 15 24
16. Practical 16 25
17. Practical 17 26
18. Practical 18 27
19. Practical 19 28-29
20. Practical 20 30
21. Practical 21 31
Page |4
1. Two numbers are input through the keyboard into two locations C and D. Write a
program to interchange the contents of C and D.
#include<stdio.h>
#include<conio.h>
main()
{
int c,d;
printf("enter the value of c and d:");
scanf("%d%d",&c,&d);
int t;
t=c;
c=d;
d=t;
printf("c=%d d=%d",c,d);
getch();
}
Output:
Page |5
#include<stdio.h>
#include<conio.h>
main()
{
int bs,hra,da;
printf("enter basic salary:");
scanf("%d",&bs);
if(bs<1500)
{
hra=10*bs/100;
da=90*bs/100;
}
else if(bs>=1500)
{
hra=500;
da=98*bs/100;
}
int gs=bs+hra+da;
printf("gross salary=%d",gs);
getch();
}
Output:
Page |6
3. If cost price and selling price of an item are input through the keyboard, write a program
to determine whether the seller has made profit or incurred loss. Also determine how
much profit he made or loss he incurred.
#include<stdio.h>
#include<conio.h>
int main()
{
int sp,cp;
printf("enter selling price and cost price:");
scanf("%d%d",&sp,&cp);
int d=sp-cp;
if(d>0)
{
printf("profit=%d",d);
}
else if(d<0)
{
d=d*-1;
printf("loss=%d",d);
}
else
{
printf("neither profit nor loss");
}
getch();
return 0;}
output:
Page |7
4. Any year is input through the keyboard. Write a program to determine whether the year
is a leap year or not. (Hint: Use the % (modulus) operator)
#include<stdio.h>
main()
{
int year;
printf("enter year:");
scanf("%d",&year);
if(year%4==0)
{
printf("%d year is a leap year\n",year);
}
else
{
printf("%d year is not a leap year\n",year);
}
}
Output:
Page |8
5. If the ages of Ram, Sham and Ajay are input through the keyboard, write a program to
determine the youngest of the three.
#include<stdio.h>
#include<conio.h>
main()
{
printf("enter age of ram shyam and ajay:");
int r,s,a;
scanf("%d%d%d",&r,&s,&a);
if(r<s && r<a)
{
printf("ram is the youngest");
6. The marks obtained by a student in 5 different subjects are input through the keyboard.
The student gets a division as per the following rules:
Percentage above or equal to 60 - First division
Percentage between 50 and 59 - Second division
Percentage between 40 and 49 - Third division
Percentage less than 40 – Fail
Write a program to calculate the division obtained by the student.
#include<stdio.h>
main()
{
int a[5],i,sum=0;
printf("enter marks in 5 subjects:");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<5;i++)
{
sum=sum+a[i];
}
int avg=sum/5;
if(avg<40)
{
printf("fail");
}
else if(avg>=40&&avg<50)
{
printf("third divison divison");
}
else if(avg>=50&&avg<60)
{
printf("second divison");
}
else
{
printf("first divison");
}
}
Output:
P a g e | 10
P a g e | 11
7. If the three sides of a triangle are entered through the keyboard, write a program to
check whether the triangle is valid or not. The triangle is valid if the sum of two sides is
greater than the largest of the three sides.
#include<stdio.h>
main()
{
printf("enter the sides of the triangle:");
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
int sum=0;;
int greatest;
if(a>c&&a>b)
{
greatest=a;
}
else
{
sum=sum+a;
}
if(a<c&&c>b)
{
greatest=c;
}
else
{
sum=sum+c;
}
if(b>c&&a<b)
{
greatest=b;
}
else
{
sum=sum+b;
}
if(sum>greatest)
{
printf("triangle is valid\n");
}
else
P a g e | 12
}
}
Output:
P a g e | 13
8. Write a program that receives month and date of birth as input and prints the
corresponding Zodiac sign based on the following table:
Sun Sign From - To
Capricorn December 22 - January 19
Aquarius January 20 - February 17
Pisces February 18 - March 19
Aries March 20 - April 19
Taurus April 20 - May 20
Gemini May 21 - June 20
Cancer June 21 - July 22
Leo July 23 - August 22
Virgo August 23 - September 22
Libra September 23 - October 22
Scorpio October 23 - November 21
Sagittarius November 22 - December 21
#include<stdio.h>
main()
{
int month, day;
if( (month == 12 && day >= 22) || (month == 1 && day <= 19) )
{
printf("Your Zodiac Sign based on your Birth date is Capricorn\n");
}
else if( (month == 1 && day >= 20) || (month == 2 && day <= 17) )
{
printf("Your Zodiac Sign based on your Birth date is Aquarius\n");
}
else if( (month == 2 && day >= 18) || (month == 3 && day <= 19) )
{
printf("Your Zodiac Sign based on your Birth date is Pisces\n");
P a g e | 14
}
else if( (month == 3 && day >= 20) || (month == 4 && day <= 19) )
{
printf("Your Zodiac Sign based on your Birth date is Aries\n");
}
else if( (month == 4 && day >= 20) || (month == 5 && day <= 20) )
{
printf("Your Zodiac Sign based on your Birth date is Taurus\n");
}
else if( (month == 5 && day >= 21) || (month == 6 && day <= 20) )
{
printf("Your Zodiac Sign based on your Birth date is Gemonthini\n");
}
else if( (month == 6 && day >= 21) || (month == 7 && day <= 22) )
{
printf("Your Zodiac Sign based on your Birth date is Cancer\n");
}
else if( (month == 7 && day >= 23) || (month == 8 && day <= 22) )
{
printf("Your Zodiac Sign based on your Birth date is Leo\n");
}
else if( (month == 8 && day >= 23) || (month == 9 && day <= 22) )
{
printf("Your Zodiac Sign based on your Birth date is Virgo\n");
}
else if( (month == 9 && day >= 23) || (month == 10 && day <= 22) )
{
printf("Your Zodiac Sign based on your Birth date is Libra\n");
}
else if((month==10&&day>=23)||(month==11&&day<=21))
{
printf("Your Zodiac Sign based on your Birth date is Scorpio\n");
}
else if((month==11 && day >= 22)||(month==12&&day <= 21) )
{
printf("Your Zodiac Sign based on your Birth date is Sagittarius\n");
P a g e | 15
}
else
{
printf("Invalid Birth date entered\n");
}
return 0;
}
Output:
P a g e | 16
10.Write a program to find the ascending order of elements in an array. Size of an array
is 5.
#include<stdio.h>
main()
{
int arr[5];
int t,i,j,k;
printf("enter elements of array:\n");
for(i=0;i<5;i++)
{
scanf("%d",&arr[i]);
}
for(j=0;j<5;j++)
{
for(k=j;k<5;k++)
{
if(arr[j]>arr[k])
{
t=arr[j];
arr[j]=arr[k];
arr[k]=t;
}
}
}
printf("array in ascending order:\n");
for(j=0;j<5;j++)
{
printf("%d\n",arr[j]);
}
}
Output:
P a g e | 18
P a g e | 19
}
Output:
P a g e | 20
12.Write a program to store the elements entered by user in a 2d array and display the
elements of a two dimensional array.(3 x 3)
#include<stdio.h>
main()
{
int arr[3][3],i,j;
printf("enter the elements of array:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&arr[i][j]);
}
}
printf("elements of array:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",arr[i][j]);
}
printf("\n");
}
}
Output:
P a g e | 21
scanf("%d", &arr2[i][j]);
}
}
printf("\n");
}
}
P a g e | 22
Output:
P a g e | 23
scanf("%d", &arr2[i][j]);
}
}
printf("\n");
}
}
P a g e | 24
Output:
P a g e | 25
#include<stdio.h>
void fun(int);
void fun(int arr[] ) { // Receiving array base address and size
int i=0;
printf("elements of array passed from fun function are:");
for (i = 0; i < 5; i++) {
printf("%d\n",arr[i]);
}
}
int main() {
int arr[5] = {
76,
89,
67,
23,
24
};
fun(arr);
}
Output:
P a g e | 26
16.Write a program that converts a given decimal number to its binary equivalent.
#include<stdio.h>
void main()
{
printf("enter no:");
int n;
scanf("%d",&n);
int a=0;
while(n>1)
{
a=a*10+(n%2);
n=n/2;
}
printf("no after binary conversion=%d",a);
}
P a g e | 27
17.Write a program in C to find the square of any number using the function.(using
call by value)
#include<stdio.h>
int square(int);
int square(int m)
{
int s=m*m;
return s;
}
main()
{
printf("enter any number:");
int n;
scanf("%d",&n);
int res=square(n);
printf("square=%d",res);
}
Output:
P a g e | 28
temp = *b;
*b = *a;
*a = temp;
}
Output:
P a g e | 29
19.Write a program in C to get the largest element of three numbers using the
function.
#include<stdio.h>
int greatest(int,int,int);
int greatest(int a,int b,int c)
{
if(a>b&&a>c)
{
return a;
}
else if(b>a&&b>c)
{
return b;
}
else
{
return c;
}
}
main()
{
int a,b,c;
printf("enter numbers:");
scanf("%d%d%d",&a,&b,&c);
int k=greatest(a,b,c);
printf("greatest no=%d",k);
}
Output:
P a g e | 30
P a g e | 31
int main()
{
time_t t;
time(&t);