Bachelor of Computer Adminstration (Bca) : Practical File On
Bachelor of Computer Adminstration (Bca) : Practical File On
Practical File on
PROGRAMMING WITH C
Page 1
PROGRAMMING WITH C-171
INDEX
S No Lab Objective Problem Statement Page Date of Signature
No. Completion
1. To teach them how to use 1. Program to find sum of two 6 20-12-2021
C Basics (printf(), scanf(), numbers.
arithmetic operators) 2. Program to find area and 7 20-12-2021
circumference of circle. 20-12-2021
3. Program to find the simple
8
interest 20-12-2021
4. Program to convert 9
temperature from degree
centigrade to Fahrenheit.
Page 2
PROGRAMMING WITH C-171
Page 3
PROGRAMMING WITH C-171
Page 4
PROGRAMMING WITH C-171
Page 5
PROGRAMMING WITH C-171
EXPERIMENT NO. 1
1. Write a Program to find sum of two numbers.
Solution:
#include<stdio.h>
void main ()
{
printf("DEVYANSHU KATARIA , 01221102021");
int num1, num2, sum;
printf("\n Enter First Number:");
scanf("%d",&num1);
printf("\n Enter Second Number:");
scanf("%d",&num2);
sum = num1+num2;
OUTPUT:
Page 6
PROGRAMMING WITH C-171
Solution:
#include<stdio.h>
void main()
{
printf("DEVYANSHU KATARIA , 01221102021");
float rad,area,circum;
printf("\n Enter The Radius of Circle:-");
scanf("%f",&rad);
area=3.14*rad*rad;
printf("\n Area of Circle = %f",area);
circum= 2*3.14*rad;
printf("\n Circumference of Circle = %f",circum);
}
OUTPUT:
Page 7
PROGRAMMING WITH C-171
Solution:
#include <stdio.h>
int main()
{
printf("DEVYANSHU KATARIA \n");
float principle, time, rate, SI;
Output:
Page 8
PROGRAMMING WITH C-171
Solution:
#include<stdio.h>
void main()
{
printf("DEVYANSHU KATARIA");
float cel,fah;
printf("\n Temperature in Celsius:");
scanf("%f",&cel);
fah=(cel*9/5)+32;
printf("\n Temperature in Fahrenheit:%f",fah);
getch ();
}
Output:
Page 9
PROGRAMMING WITH C-171
EXPERIMENT NO. 2
Solution:
#include<stdio.h>
#include<conio.h>
void main()
{
printf("DEVYANSHU KATARIA");
float phy,chem,math,eng,cs,total,per;
printf("\n Enter Marks for Physics:");
scanf("%f",&phy);
printf("\n Enter Marks for English:");
scanf("%f",&eng);
printf("\n Enter Marks for Maths:");
scanf("%f",&math);
printf("\n Enter Marks for Chemistry:");
scanf("%f",&chem);
printf("\n Enter Marks for Computer Science:");
scanf("%f",&cs);
total=phy+chem+math+eng+cs;
printf("\n Total Marks Obtained:%f",total);
per=(total/500)*100;
printf("\n Percentage Obtained is:%f",per);
getch ();
}
Page 10
PROGRAMMING WITH C-171
Output:
Page 11
PROGRAMMING WITH C-171
2. Program to show swap of two no’s with and without using third variable
Solution:
#include<stdio.h>
void main()
{
printf("DEVYANSHU KATARIA \n");
int a=10, b=20;
printf("Before swap a=%d b=%d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("\nAfter swap a=%d b=%d",a,b);
getch ();
}
Output:
Page 12
PROGRAMMING WITH C-171
Solution:
#include<stdio.h>
int main()
{
printf("DEVYANSHU KATARIA \n");
int gross_salary, basic, da, ta;
Output:
Page 13
PROGRAMMING WITH C-171
EXPERIMENT NO. 3
Solution:
#include <stdio.h>
int main()
{
printf("DEVYANSHU KATARIA \n");
int n, i;
printf("Enter an integer: ");
scanf("%d", &n);
for (i = 1; i<= 10; ++i)
{
printf("%d * %d = %d \n", n, i, n * i);
}
}
Output:
Page 14
PROGRAMMING WITH C-171
Solution:
#include <stdio.h>
int main()
{
printf("DEVYANSHU KATARIA \n");
int num1, num2, num3;
printf(" Enter the number1 = ");
scanf("%d", &num1);
printf("\n Enter the number2 = ");
scanf("%d", &num2);
printf("\n Enter the number3 = ");
scanf("%d", &num3);
if (num1 > num2)
{
if (num1 > num3)
{
printf("\n Largest number = %d \n",num1);
}
else
{
printf("\n Largest number = %d \n",num3);
}
}
else if (num2 > num3)
{
printf("\n Largest number = %d \n",num2);
}
else
{
printf("\n Largest number = %d \n",num3);
}
return 0;
}
Page 15
PROGRAMMING WITH C-171
Output:
Page 16
PROGRAMMING WITH C-171
Solution:
#include<stdio.h>
int main()
{
printf("DEVYANSHU KATARIA \n");
int num;
printf("Enter a number: ");
scanf("%d", &num);
(num>=0)?printf(" Number is Positive."):printf(" Number is Negative");
return 0;
}
Output:
Page 17
PROGRAMMING WITH C-171
Page 18
PROGRAMMING WITH C-171
Output:
Page 19
PROGRAMMING WITH C-171
EXPERIMENT NO. 4
Solution:
#include <stdio.h>
void main()
{
printf("DEVYANSHU KATARIA , 01221102021 \n");
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if(num % 2 == 0)
printf("%d is even.", num);
else
printf("%d is odd.", num);
}
Output:
Page 20
PROGRAMMING WITH C-171
Solution:
#include<stdio.h>
int main()
{
int a, b;
printf("DEVYANSHU KATARIA , 01221102021 \n");
printf("Read the integer from keyboard-");
scanf("%d",&a);
printf("\nInteger value = %d ",a);
a<<=2;
b=a;
printf("\nThe left shifted data is = %d ",b);
}
Output:
Page 21
PROGRAMMING WITH C-171
switch(day){
case 1 :printf("Monday\n");
break;
case 2 :printf("Tuesday\n");
break;
case 3 :printf("Wednesday\n");
break;
case 4 :printf("Thursday\n");
break;
case 5 :printf("Friday\n");
break;
case 6 :printf("Saturday\n");
break;
case 7 :printf("Sunday\n");
break;
default: printf("Invalid Input !!!!\n");
}
}
Output:
Page 22
PROGRAMMING WITH C-171
break;
case 4:
printf("\n Please Enter the Value of Num1 and Num2:");
scanf("%d%d",&num1,&num2);
result=num1/num2;
printf("\n Division of %d with %d = %d",num1,num2, result);
break;
default:
printf("\n Enter the Wrong Choice:");
}
Page 23
PROGRAMMING WITH C-171
Output:
Page 24
PROGRAMMING WITH C-171
EXPERIMENT NO. 5
Output:
Page 25
PROGRAMMING WITH C-171
2. Program to find sum of the digits of 3 digit number entered by the user.
Solution:
#include<stdio.h>
void main()
{
int n,sum=0,m;
printf("DEVYANSHU KATARIA , 01221102021 \n");
printf("Enter a number:");
scanf("%d",&n);
while(n>0)
{
m=n%10;
sum=sum+m;
n=n/10;
}
printf("Sum is=%d",sum);
}
Output:
Page 26
PROGRAMMING WITH C-171
while (originalNum != 0)
{
remainder = originalNum % 10;
result += remainder * remainder * remainder;
originalNum /= 10;
}
if (result == num)
printf("%d is an Armstrong number.", num);
else
printf("%d is not an Armstrong number.", num);
}
Output:
Page 27
PROGRAMMING WITH C-171
Output:
Page 28
PROGRAMMING WITH C-171
Output:
Page 29
PROGRAMMING WITH C-171
Output:
Page 30
PROGRAMMING WITH C-171
Output:
Page 31
PROGRAMMING WITH C-171
EXPERIMENT NO. 6
Output:
Page 32
PROGRAMMING WITH C-171
if (low % i == 0) {
flag = 1;
break;
}
}
if (flag == 0)
printf("%d ", low);
++low;
}
}
Output:
Page 33
PROGRAMMING WITH C-171
Output:
Page 34
PROGRAMMING WITH C-171
EXPERIMENT NO. 7
Solution:
#include<stdio.h>
void main()
{
int i,n,sum=1;
printf("Enter any number n:");
scanf("%d",&n);
printf("\n1 ");
for(i=3;i<=n;i++){
printf("+ %d ",i);
sum=sum+i;
i++;
}
printf("\nSum is: %d", sum);
}
Output:
Page 35
PROGRAMMING WITH C-171
Output:
Page 36
PROGRAMMING WITH C-171
EXPERIMENT NO. 8
Solution:
#include<stdio.h>
void func(int *);
int main()
{
int n,*p;
printf("enter the number ");
scanf("%d",&n);
func(&n);
printf("\n the reverse is %d ",n);
return 0;
}
void func(int *n)
{
int i,s=0;
int a[3];
for(i=0;*n!=0;i++)
{
a[i]=*n%10;
*n=*n/10;
}
for(i=0;i<3;i++)
{
*n=*n+a[i]*pow(10,2-i);
}
}
Output:
Page 37
PROGRAMMING WITH C-171
sum = *p + *q;
printf("Sum of the numbers = %d\n", sum);
return 0;
}
Output:
Page 38
PROGRAMMING WITH C-171
Solution:
int main()
{
int no, square;
square = func(no);
Output:
Page 39
PROGRAMMING WITH C-171
Output:
Page 40
PROGRAMMING WITH C-171
int fact(int n)
{
int i,f=1;
for(i=1;i<=n;i++)
{
f=f*i;
}
return f;
}
Outcome:
Page 41
PROGRAMMING WITH C-171
EXPERIMENT NO. 9
Output:
Page 42
PROGRAMMING WITH C-171
Page 43
PROGRAMMING WITH C-171
Page 44
PROGRAMMING WITH C-171
EXPERIMENT NO. 10
return 0;
}
Output:
Page 45
PROGRAMMING WITH C-171
Output:
Page 46
PROGRAMMING WITH C-171
EXPERIMENT NO. 11
Output:
Page 47
PROGRAMMING WITH C-171
Output:
Page 48
PROGRAMMING WITH C-171
Page 49
PROGRAMMING WITH C-171
Output:
Page 50
PROGRAMMING WITH C-171
Output:
Page 51
PROGRAMMING WITH C-171
Output:
Page 52
PROGRAMMING WITH C-171
Output:
Page 53
PROGRAMMING WITH C-171
EXPERIMENT NO. 12
1. Program to print array elements in reverse order.
Solution:
#include <stdio.h>
int main()
{
printf("DEVYANSHU KATARIA,
01221102021 \n");int arr[] = {1, 2, 3,
4, 5};
int length = sizeof(arr)/sizeof(arr[0]);
printf("Original array: \n");
for (int i = 0; i< length; i++) {
printf("%d ", arr[i]);
}
printf("\n");
printf("Array in reverse order: \n");
for (int i = length-1; i>= 0; i--) {
printf("%d ", arr[i]);
}
return 0;
}
Page 54
PROGRAMMING WITH C-171
Output:
Page 55
PROGRAMMING WITH C-171
Page 56
PROGRAMMING WITH C-171
Solution:
#include<stdio.h>
void main()
{
printf("DEVYANSHU KATARIA , 01221102021 \n");
int a[10],i,sum=0;
float av;
printf("enter elements of an array: \n");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<10;i++)
{
sum=sum+a[i];
}
printf("sum=%d\n",sum);
av=sum/10;
printf("average=%f\n",av);
Page 57
PROGRAMMING WITH C-171
Output:
Page 58
PROGRAMMING WITH C-171
Page 59
PROGRAMMING WITH C-171
Solution:
#include <stdio.h>
int main()
scanf("%d", &n);
scanf("%d", &array[c]);
scanf("%d", &search);
if (array[c] == search)
break;
}
Page 60
PROGRAMMING WITH C-171
if (c == n)
return 0;
Output:
Page 61
PROGRAMMING WITH C-171
}
}
void main ()
{
int i, j,temp;
int a[5] = { 10, 35, 32, 13, 26};
int n = sizeof(a)/sizeof(a[0]);
printf("Before sorting array elements are - \n");
print(a, n);
bubble(a, n);
printf("\nAfter sorting array elements are - \n");
print(a, n);
}
Output:
Page 63
PROGRAMMING WITH C-171
Page 64
PROGRAMMING WITH C-171
Output:
Page 65
PROGRAMMING WITH C-171
EXPERIMENT NO. 13
1. Program to find subtraction of two matrices
Solution:
#include <stdio.h>
int main()
{
printf("DEVYANSHU KATARIA, 01221102021 \n");
int m, n, c, d, first[10][10], second[10][10], difference[10][10];
printf("Enter the number of rows and columns of matrix\n");
scanf("%d%d", & m, & n);
printf("Enter the elements of first matrix\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++) scanf("%d", & first[c][d]);
printf("Enter the elements of second matrix\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++) scanf("%d", & second[c][d]);
printf("Difference of entered matrices:-\n");
for (c = 0; c < m; c++)
{
for (d = 0; d < n; d++)
{
difference[c][d] = first[c][d] - second[c][d];
printf("%d\t", difference[c][d]);
}
printf("\n");
Page 66
PROGRAMMING WITH C-171
}
return 0;
}
Output:
Page 67
PROGRAMMING WITH C-171
Page 68
PROGRAMMING WITH C-171
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");
}
return 0;
}
Output:
Page 69
PROGRAMMING WITH C-171
Page 70
PROGRAMMING WITH C-171
Output:
Page 71
PROGRAMMING WITH C-171
Page 72
PROGRAMMING WITH C-171
Output:
Page 73
PROGRAMMING WITH C-171
Page 74
PROGRAMMING WITH C-171
Output:
Page 75
PROGRAMMING WITH C-171
Page 76
PROGRAMMING WITH C-171
Output:
Page 77
PROGRAMMING WITH C-171
EXPERIMENT NO. 14
1. Program to enter book records
Solution:
#include<stdio.h>
struct book
{
char book_name[20];
int bookid;
float book_price;
char author[15];
};
int main()
{
struct book b[3];
int i;
for(i=0; i<3; i++)
{
printf("Enter details of book #%d\n", i+1);
printf("Enter book id: ");
scanf("%d", &b[i].bookid);
printf("Enter book name: ");
scanf("%s", b[i].book_name);
printf("Enter book author: ");
Page 78
PROGRAMMING WITH C-171
scanf("%s", b[i].author);
printf("Enter book price: ");
scanf("%f", &b[i].book_price);
}
for(i=0; i<3; i++)
{
printf("\nBook %d. ........... \n\n", i+1);
printf("Book Id: %d\n", b[i].bookid);
printf("Book Name: %s\n", b[i].book_name);
printf("Book Author: %s\n", b[i].author);
printf("Book price: %f", b[i].book_price);
}
return 0;
}
Output:
Page 79
PROGRAMMING WITH C-171
Solution:
#include <stdio.h>
struct student {
char name[50];
int roll;
float marks;
} s;
int main() {
printf("DEVYANSHU KATARIA \n");
printf("Enter information:\n");
printf("Enter name: ");
fgets(s.name, sizeof(s.name), stdin);
printf("Enter roll number: ");
scanf("%d", &s.roll);
printf("Enter marks: ");
scanf("%f", &s.marks);
printf("Displaying Information:\n");
printf("Name: ");
printf("%s", s.name);
printf("Roll number: %d\n", s.roll);
printf("Marks: %.1f\n", s.marks);
return 0;
Page 80
PROGRAMMING WITH C-171
Output:
Page 81
PROGRAMMING WITH C-171
Page 82
PROGRAMMING WITH C-171
Output:
Page 83
PROGRAMMING WITH C-171
EXPERIMENT NO. 15
1. Program to reverse the string.
Solution:
#include <stdio.h>
#include <string.h>
int main()
printf("DEVYANSHU KATARIA ,
01221102021\n\n");
char str[40];
getch();
Output:
Page 84
PROGRAMMING WITH C-171
Solution:
#include <stdio.h>
int main() {
char s1[100] = "programming ", s2[] = "is awesome";
int length, j;
length = 0;
while (s1[length] != '\0') {
++length;
}
for (j = 0; s2[j] != '\0'; ++j, ++length) {
s1[length] = s2[j];
}
s1[length] = '\0';
printf("DEVYANSHU KATARIA ,
01221102021\n\n");
printf("After concatenation: "); puts(s1);
getch();
}
Output:
Page 85
PROGRAMMING WITH C-171
Solution:
#include<stdio.h>
#include<string.h>
void main()
{
printf("DEVYANSHU KATARIA , 01221102021 \n");
char str1[100],str2[50];
printf("Enter string str1\n");
gets(str1);
strcpy(str2,str1);
printf("Copied String(str2) is %s",str2);
getch();
}
Output:
Page 86
PROGRAMMING WITH C-171
Solution:
#include <stdio.h>
#define MAX_SIZE 100
int main()
{
char str[MAX_SIZE];
int i;
printf("DEVYANSHU KATARIA , 01221102021 \n");
printf("Enter your text : ");
gets(str);
OUTPUT:
Page 87
PROGRAMMING WITH C-171
Solution:
#include <stdio.h>
#define MAX_SIZE 100
int main()
{
char str[MAX_SIZE];
int i;
printf("DEVYANSHU KATARIA , 01221102021\n");
printf("Enter any string: ");
gets(str);
for(i=0; str[i]!='\0'; i++)
{
if(str[i]>='A' && str[i]<='Z')
{
str[i] = str[i] + 32;
}
}
OUTPUT:
Page 88
PROGRAMMING WITH C-171
EXPERIMENT NO. 15
1. WAP to create a file.
Solution:
#include<stdio.h>
int main()
{
FILE *fp;
char fName[20];
printf("DEVYANSHU KATARIA ,
01221102021\n");
printf("Enter file name to create :");
scanf("%s",fName);
fp=fopen(fName,"w");
if(fp==NULL)
{
printf("File does not created!!!");
exit(0);
}
printf("File created successfully.");
getch();
}
OUTPUT:
Page 89
PROGRAMMING WITH C-171
Solution:
#include <stdio.h>
#include <stdlib.h>
int main() {
char sentence[1000];
FILE *fptr;
printf("DEVYANSHU KATARIA , 01221102021\n");
fptr = fopen("fp1.txt", "w");
if (fptr == NULL) {
printf("Error!");
exit(1);
}
printf("Enter a sentence:\n");
fgets(sentence, sizeof(sentence), stdin);
fprintf(fptr, "%s", sentence);
fclose(fptr);
getch();
}
OUTPUT:
Page 90
PROGRAMMING WITH C-171
Solution:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fptr1, *fptr2;
char filename[100], c;
printf("DEVYANSHU KATARIA, 01221102021 \n");
printf("Enter the filename to open for reading \n");
scanf("%s", filename);
fptr1 = fopen(filename, "r");
if (fptr1 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}
fclose(fptr1);
Page 91
PROGRAMMING WITH C-171
fclose(fptr2);
getch();
}
OUTPUT:
Page 92
PROGRAMMING WITH C-171
Solution:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE * fPtr;
char ch;
printf("DEVYANSHU KATARIA, 01221102021 \n");
fPtr = fopen("fp1.txt", "r");
if(fPtr == NULL)
{
printf("Unable to open file.\n");
printf("Please check whether file exists and you have read privilege.\n");
}
printf("File opened successfully. Reading file contents character by character. \n\n");
do
{
ch = fgetc(fPtr);
putchar(ch);
} while(ch != EOF);
fclose(fPtr);
getch();
}
OUTPUT:
Page 93