0% found this document useful (0 votes)
2 views

c Manual

The document outlines a C Programming Laboratory course with a focus on practical programming exercises. It includes a variety of tasks such as calculating areas, finding roots of equations, and working with data structures and file handling. Students are required to complete one question from Part A and one from Part B, covering fundamental programming concepts and techniques.

Uploaded by

sahanakm8867
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

c Manual

The document outlines a C Programming Laboratory course with a focus on practical programming exercises. It includes a variety of tasks such as calculating areas, finding roots of equations, and working with data structures and file handling. Students are required to complete one question from Part A and one from Part B, covering fundamental programming concepts and techniques.

Uploaded by

sahanakm8867
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 31

C PROGRAMMING LABORATORY

[10MCA11]

C Programming Laboratory
Subject Code: 10MCA16 I.A. Marks: 50
Hours/Week: 3 Exam Marks: 50
Total Hours: 42 Exam Hours: 3

Part A
1. a. Write a program to find the area of a triangle (Given the three sides).
b. Write a program to find the area of a circle (Given the radius).

2. Write a program to find the Simple interest, given the principle, time and rate
of interest with appropriate validations.

3. Write a program to find out whether a given year is a leap year or not.

4. Write a program to find the roots of a quadratic equation with appropriate


error messages.

5. Write a program to display the following files of current directory.


i) .EXE files ii) .BAT files iii) .OBJ files iv) .BAK files.
By using system DOS command.

6. Write a program to find GCD and LCM of given two numbers.

7. Write a program to find the value of Sin (x) using the series.
Sin (x) = x – x3/3! + x5/5! – x7/7! + …………….

8. Write a program to print all prime numbers between m and n.


.
9. Write a program to reverse a number and check whether it is palindrome or
Not.

10. Write a program to generate and print first n Fibonacci numbers using
function.

11. Write a program to find a factorial of a given number using recursive function.

12. Write a program to convert UPPERCASE alphabets to LOWERCASE alphabets in


a given string
and vice-versa.

13. Write a program to read two strings and concatenate them (without using
library functions).

14. Write a program to read a sentence and count the number of vowels and
constants.

PART - B

1. Write a program to read N integers (zero, + ve and –ve) into an array and
find sum of positive numbers, sum of negative numbers and average of all
input numbers.

Dept. of MCA Sir MVIT 1


C PROGRAMMING LABORATORY
[10MCA11]

2. Write a program to input N real numbers and to find the mean, variance and
standard deviation, where,

Mean = xi / N
(xi – mean)2
Variance = ----------------
N
Deviation = variance and 0  i < n
3. Write a program to input N numbers (integers or real) and store them in an
array. Conduct a Linear search for a given key number and report success or
failure in the form of a suitable message.

4. Write a program to sort N numbers in ascending or descending order using


bubble sort.

5. Write a program to accept N numbers sorted in ascending order and search


for a given number using binary search. Report success or failure in the form
of suitable messages.

6. Write a program to read two matrices A and B of size M x N and perform


product of two given matrices.

7. Write a program to list the names of students who have scored more than
60% of total marks in three subjects using structure variables.

8. Write a program to compute the sum of two complex numbers – passing a


structure to a function.

9. Define a book structure having title of the book, ISBN, author, price and
month and year of publication as its members. Use a substructure to store
the month and year of Publication information. Develop a program to accept
a date (in the form of month and year) and list out all the book titles
(along with price and ISBN) published during that date.

10. Define a student structure having the name, USN (university seat number),
marks in five subjects, total and percentage of marks as its members. Marks
of all the subjects are to be stored in an array. Develop a program to list the
names of all the students who have failed.

11. Write a program to read N integers and store them in an array, find the sum
of all these elements using pointer. Output the given array and the computed
sum with suitable heading.

12. Write a program to read and write to a file.

13. Write a program to Create and count number of characters in a file.

14. Write a program to handle files with mixed data type.

Note: Students are required to execute one question from Part A and one
from Part B

Dept. of MCA Sir MVIT 2


C PROGRAMMING LABORATORY
[10MCA11]

1. a)WAP TO FIND THE AREA OF A TRIANGLE (GIVEN THE


THREE SIDES)
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float s,a,b,c,area;clrscr();
printf("\n\n\t ******* AREA OF TRIANGLE ******\n\n");
printf("ENTER VALUES TO 3 SIDES A,B AND C :- ");
scanf("%f%f%f",&a,&b,&c);
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("\nAREA OF TRIANGLE -> %5.2f\n",area);
getch();
}

OUPUT

******* AREA OF TRIANGLE ******

ENTER VALUES TO 3 SIDES A, B AND C: -


234

AREA OF TRIANGLE
 2.90
2. b)WRITE A PROGRAM TO FIND THE AREA OF A
CIRCLE(GIVEN THE RADIUS).
#include<stdio.h>
#include<conio.h>
#define PI 3.142
void main()
{
float radius,area;clrscr();
printf("\n\t\t******* AREA OF A CIRCLE ********\n");
printf("\nEnter the radius of a circle :- ");
scanf("%f",&radius);
area=PI * radius * radius;
printf("\nArea of a Circle -> %5.2f",area);
getch();
}

OUPUT

******* AREA OF A CIRCLE ********


Enter the radius of a circle: 3
Area of a Circle  28.28

Dept. of MCA Sir MVIT 3


C PROGRAMMING LABORATORY
[10MCA11]

3. WRITE A PROGRAM TO FIND OUT WHETHER A GIVEN


YEAR IS A LEAP YEAR OR NOT.
#include<stdio.h>
#include<conio.h>
void main()
{
int year;
clrscr();
printf("Enter a year :- ");
scanf("%d",&year);
if(year%4==0)
printf("%d is a leap year",year);
else
printf("%d is not a leap year",year);
getch();
}

OUPUT

Enter a year :- 2004


2004 is a leap year

Enter a year :- 2010


2010 is not a leap year

4. WRITE A PROGRAM TO FIND THE ROOTS OF A


QUADRATIC EQUATION WITH APPROPRIATE ERROR
MESSAGES.
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<process.h>
void main()
{
float a,b,c,root1,root2;
float realp,imagp,disc;
clrscr();
printf("\n\t\t******** ROOTS OF QUADRATIC EQUATION *******\n");
printf("Enter the values to a,b and c :- ");
scanf("%f%f%f",&a,&b,&c);
if(a==0||b==0||c==0)
{
printf("Error:Roots cannot be determined\n");
exit(1);
}
else
{

Dept. of MCA Sir MVIT 4


C PROGRAMMING LABORATORY
[10MCA11]

disc=b*b-4.0*a*c;
if(disc<0)
{
printf("Imaginary Roots\n");
realp=-b/(2.0*a);
imagp=sqrt(abs(disc))/(2.0*a);
printf("Root1 = %f +i %f\n",realp,imagp);
printf("Root2 = %f -i %f\n",realp,imagp);
}
else if(disc == 0)
{
printf("Roots are real and equal\n");
root1=-b/(2.0*a);
root2=root1;
printf("Root1 = %f\n",root1);
printf("Root2 = %f\n",root2);
}
else if(disc >0)
{
printf("Roots are real and distinct\n");
root1=(-b+sqrt(disc))/(2.0*a);
root2=(-b-sqrt(disc))/(2.0*a);
printf("Root1 = %f\n",root1);
printf("Root2 = %f\n",root2);
}
}
getch();
}

OUPUT

******** ROOTS OF QUADRATIC EQUATION *******


Enter the values to a,b and c :- 0 0 0
Error:Roots cannot be determined

******** ROOTS OF QUADRATIC EQUATION *******


Enter the values to a,b and c :- 2 3 4
Imaginary Roots
Root1 = -0.750000 +i 1.198958
Root2 = -0.750000 -i 1.198958

******** ROOTS OF QUADRATIC EQUATION *******


Enter the values to a,b and c :- 2 1 1
Imaginary Roots
Root1 = -0.250000 +i 0.661438
Root2 = -0.250000 -i 0.661438

******** ROOTS OF QUADRATIC EQUATION *******


Enter the values to a,b and c :- 3 1 1
Imaginary Roots
Root1 = -0.166667 +i 0.552771
Root2 = -0.166667 -i 0.552771

Dept. of MCA Sir MVIT 5


C PROGRAMMING LABORATORY
[10MCA11]

5. WRITE A PROGRAM TO DISPLAY THE FOLLOWING FILES


OF CURRENT DIRECTORY. I) .EXE FILES II) .BAT FILES
III) .OBJ FILES IV) .BAK FILES. BY USING SYSTEM
DOS COMMAND.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
clrscr();
printf("\n ****** DISPLAYING .EXE .OBJ .BAT & .BAK FILES OF CURRENT
DIRECTORY *****\n");
printf("\nFOLLOWING ARE THE .EXE FILES IN THE CURRENT DIRECTORY
ARE .....\n");
system("dir *.exe");
getch();
printf("\nFOLLOWING ARE THE .OBJ FILES IN THE CURRENT DIRECTORY
ARE .....\n");
system("dir *.obj/p");
getch();
printf("\nFOLLOWING ARE THE .BAT FILES IN THE CURRENT DIRECTORY
ARE .....\n");
system("dir *.bat");
getch();
printf("\nFOLLOWING ARE THE .BAK FILES IN THE CURRENT DIRECTORY
ARE .....\n");
system("dir *.bak");
getch();
}

OUPUT
** DISPLAYING .EXE .OBJ .BAT & .BAK FILES OF CURRENT DIRECTORY **

FOLLOWING ARE THE .EXE FILES IN THE CURRENT DIRECTORY ARE .....

Volume in drive E is New Volume


Volume Serial Number is E07A-2D33
Directory of E:\VAS\LM

LEAP EXE 14745 09/24/10 11:44a


QUAD EXE 32139 09/24/10 11:49a
SYSTEM EXE 13173 09/24/10 11:51a
3 file(s) 60057 bytes
383381504 bytes free

FOLLOWING ARE THE .OBJ FILES IN THE CURRENT DIRECTORY ARE .....

Volume in drive E is New Volume


Volume Serial Number is E07A-2D33

Dept. of MCA Sir MVIT 6


C PROGRAMMING LABORATORY
[10MCA11]

Directory of E:\VAS\LM

LEAP OBJ 804 09/24/10 11:44a


QUAD OBJ 2572 09/24/10 11:49a
SYSTEM OBJ 1264 09/24/10 11:51a
3 file(s) 4640 bytes
383381504 bytes free
QUAD EXE 32139 09/24/10 11:49a
SYSTEM EXE 13173 09/24/10 11:51a
3 file(s) 60057 bytes
383381504 bytes free

FOLLOWING ARE THE .BAT FILES IN THE CURRENT DIRECTORY ARE .....

Volume in drive E is New Volume


Volume Serial Number is E07A-2D33
Directory of E:\VAS\LM

File not found

FOLLOWING ARE THE .BAT FILES IN THE CURRENT DIRECTORY ARE .....

Volume in drive E is New Volume


Volume Serial Number is E07A-2D33
Directory of E:\VAS\LM

File not found

FOLLOWING ARE THE .BAK FILES IN THE CURRENT DIRECTORY ARE .....

Volume in drive E is New Volume


Volume Serial Number is E07A-2D33
Directory of E:\VAS\LM

File not found

6. WRITE A PROGRAM TO FIND GCD AND LCM OF GIVEN


TWO NUMBERS.
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,gcd,lcm,r,n,d;
clrscr();
printf("\n\t\t ****** GCD AND LCM OF TWO NUMBERS ******\n");
printf("\nEnter two numbers :- ");
scanf("%d%d",&n1,&n2);
if(n1>n2)
{
n=n1;
d=n2;
}

Dept. of MCA Sir MVIT 7


C PROGRAMMING LABORATORY
[10MCA11]

else
{
n=n2;
d=n1;
}
r=n1%n2;
while(r!=0)
{
n=d;
d=r;
r=n%d;
}
gcd=d;
lcm=n1*n2/gcd;
printf("\nGCD OF %d AND %d = %d\n",n1,n2,gcd);
printf("LCM OF %d AND %d = %d\n",n1,n2,lcm);
getch();
}

OUPUT

****** GCD AND LCM OF TWO NUMBERS ******


Enter two numbers :- 5 15
GCD OF 5 AND 15 = 5
LCM OF 5 AND 15 = 15

7. WRITE A PROGRAM TO FIND THE VALUE OF SIN (X)


USING THE SERIES. SIN (X) = X – X3/3! +
X5/5! – X7/7! + …………….

#include<stdio.h>
#include<conio.h>
#define pi 3.142
void main()
{
float sum=0,xrad,num,xsqr,den=1,sign=1,term,x;
int n,i,k=2;
clrscr();
printf("Enter degree ");
scanf("%f",&x);
printf("Enter term ");
scanf("%d",&n);
xrad=x*(pi/180);
xsqr=xrad*xrad;
num=xrad;
for(i=0;i<n;i++)
{
term=(num/den)*sign;

Dept. of MCA Sir MVIT 8


C PROGRAMMING LABORATORY
[10MCA11]

sum+=term;
num*=xsqr;
den*=k*(k+1);
sign*=-1;
k+=2;
}
printf("Sine series = %f",sum);
getch();
}
OUPUT

Enter degree 90
Enter term 7
Sine series = 1.000000

Enter degree 45
Enter term 8
Sine series = 0.707179

8. WRITE A PROGRAM TO PRINT ALL PRIME NUMBERS


BETWEEN M AND N.

#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
int M,N, i,j,flag,temp;
clrscr();
printf("\n\t\t ******* PRIME NUMBERS BETWEEN THE RANGE ******\n");
printf("Enter the value of M and N :- ");
scanf("%d%d",&M,&N);
if(N<2)
{
printf("\nThere are no primes upto %d\n",N);
exit(0);
}
printf("\nPRIME NUMBERS ARE : ");
if(M%2==0)
{
M++;
}
for(i=M;i<=N;i=i+2)
{

Dept. of MCA Sir MVIT 9


C PROGRAMMING LABORATORY
[10MCA11]

flag=0;
for(j=2;j<=i/2;j++)
{
if((i%j)==0)
{
flag=1;
break;
}
}
if(flag==0)
{
printf("%d\t",i);
}
}
getch();
}

OUPUT

******* PRIME NUMBERS BETWEEN THE RANGE ******


Enter the value of M and N: - 1 20
PRIME NUMBERS ARE: 1 3 5 7 11 13 17 19

9. WRITE A PROGRAM TO REVERSE A NUMBER AND


CHECK WHETHER IT IS PALINDROME OR NOT.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,rev=0,t,r;
clrscr();
printf(" **** PALINDROME *****\n");
printf("Enter an integer value ");
scanf("%d",&n);
t=n;
while(n>0)
{
r=n%10;
rev=rev*10+r;
n/=10;
}
printf("Given number is %d ",t);
printf("\nIts reverse is %d ",rev);

Dept. of MCA Sir MVIT 10


C PROGRAMMING LABORATORY
[10MCA11]

if(t==rev)
printf("\nNumber is a palindrome");
else
printf("\nNumber is not a palindrome");
getch();
}

OUPUT
**** PALINDROME ***** **** PALINDROME *****
Enter an integer value 121 Enter an integer value 123
Given number is 121 Given number is 123
Its reverse is 121 Its reverse is 321
Number is a palindrome Number is not a palindrome

10. WRITE A PROGRAM TO GENERATE AND PRINT


FIRST N FIBONACCI NUMBERS USING FUNCTION.

#include<stdio.h>
#include<conio.h>
void main()
{
int f1=0,f2=1,f3,n,count=0;
clrscr();
printf(" **** FIBONACCI SERIES *****\n");
printf("Enter the value of n ");
scanf("%d",&n);
printf("\nFirst %d FIBONACCI numbers are \n",n);
printf("%d ",f1);
printf("%d ",f2);
count=2;
while(count<n)
{
f3=f1+f2;
count++;
printf("%d ",f3);
f1=f2;
f2=f3;
}
getch();

OUPUT
**** FIBONACCI SERIES *****
Enter the value of n 8
First 8 FIBONACCI numbers are
Dept. of MCA
0 1 1 2 3 5 8 13 Sir MVIT 11
C PROGRAMMING LABORATORY
[10MCA11]

Dept. of MCA Sir MVIT 12


C PROGRAMMING LABORATORY
[10MCA11]

11. WRITE A PROGRAM TO FIND A FACTORIAL OF A


GIVEN NUMBER USING RECURSIVE FUNCTION.

#include<stdio.h>
#include<conio.h>
int rec_fun(int);
void main()
{
int n,fact;
clrscr();
printf("***** FACTORIAL OF A GIVEN NUMBER *****\n");
printf("Enter the number ");
scanf("%d",&n);
fact=rec_fun(n);
printf("Factorial of %d = %d\n",n,fact);
getch();
}
int rec_fun(int n)
{
int f;
if(n==0)
return 1;
else
f=n*rec_fun(n-1);
return f;
}

OUPUT

***** FACTORIAL OF A GIVEN NUMBER *****


Enter the number 5
Factorial of 5 = 120

12. WRITE A PROGRAM TO CONVERT UPPERCASE


ALPHABETS TO LOWERCASE ALPHABETS IN A GIVEN
STRING AND VICE-VERSA.

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char str[30];
int ch,i;

Dept. of MCA Sir MVIT 13


C PROGRAMMING LABORATORY
[10MCA11]

clrscr();
printf(" ***** CHANGING UPPERCASE LETTER TO LOWERCASE AND
VICEVERSA ***\n");
printf("Enter a string ");
scanf("%s",str);
for(i=0;str[i]!='\0';i++)
{
ch=islower(str[i])?toupper(str[i]):tolower(str[i]);
putchar(ch);
}
getch();
}

OUPUT

***** CHANGING UPPERCASE LETTER TO LOWERCASE AND VICEVERSA ***


Enter a string CProgramming
cpROGRAMMING

13. WRITE A PROGRAM TO READ TWO STRINGS AND


CONCATENATE THEM (WITHOUT USING LIBRARY
FUNCTIONS).
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[20],str2[20],str3[50];
int i,j,k;
clrscr();
printf("****** CONCATENATE TWO STRINGS WITHOUT USING LIBRARY
FUNCTION ****\n");
printf("Enter string1 ");
scanf("%s",str1);
printf("Enter string2 ");
scanf("%s",str2);
for(i=0;str1[i]!='\0';i++)
str3[i]=str1[i];
for(j=i,k=0;str2[k]!='\0';k++,j++)
str3[j]=str2[k];
str3[j]='\0';
printf("%s",str3);
getch();
}

OUPUT
****** CONCATENATE TWO STRINGS WITHOUT USING LIBRARY FUNCTION ****
Enter string1 Gopal
Enter string2 Krishna
GopalKrishna
Dept. of MCA Sir MVIT 14
C PROGRAMMING LABORATORY
[10MCA11]

14. WRITE A PROGRAM TO READ A SENTENCE AND


COUNT THE NUMBER OF VOWELS AND CONSTANTS.

#include<stdio.h>
#include<conio.h>
void main()
{
char sen[80];
int i,vowels=0,consonants=0,special=0;
clrscr();
printf(" ***** NUMBER OF VOWELS AND CONSONANTS IN A
SENTENCE*******\n");
printf("Enter a sentence : ");
gets(sen);
for(i=0;sen[i]!='\0';i++)
{
if(sen[i]=='a'||sen[i]=='e'||sen[i]=='i'||sen[i]=='o'||sen[i]=='u'||sen[i]=='A'||
sen[i]=='E'||sen[i]=='I'||sen[i]=='O'||sen[i]=='U')
{
vowels=vowels+1;
}
else
consonants=consonants+1;
if(sen[i]=='\t'||sen[i]=='\0'||sen[i]==' ')
special=special+1;
}
consonants=consonants-special;
printf("No. of vowels in %s = %d\n",sen,vowels);
printf("No. of consonants %s = %d\n",sen,consonants);
getch();
}

OUPUT

***** NUMBER OF VOWELS AND CONSONANTS IN A SENTENCE*******


Enter a sentence : Welcome to C Programming
No. of vowels in Welcome to C Programming = 7
No. of consonants Welcome to C Programming = 14

Dept. of MCA Sir MVIT 15


C PROGRAMMING LABORATORY
[10MCA11]

PART - B
1. WRITE A PROGRAM TO READ N INTEGERS (ZERO, +VE
AND –VE) INTO AN ARRAY AND FIND SUM OF POSITIVE
NUMBERS, SUM OF NEGATIVE NUMBERS AND AVERAGE
OF ALL INPUT NUMBERS.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,n,sump=0,sumn=0,suma=0;
float avg;
clrscr();
printf("\n **** Sum of all +ve,-ve and Average of all numbers in an array ***\n\
n");
printf("Enter size of array -: ");
scanf("%d",&n);
printf("Enter %d element -: ",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
if(a[i]>=0)
sump=sump+a[i];
if(a[i]<0)
sumn=sumn+a[i];
suma=suma+a[i];
avg=(float)suma/n;
}

printf("Sum of all positive numbers -: %d\n",sump);


printf("Sum of all negative numbers -: %d\n",sumn);
printf("Average of all numbers -: %.2f\n",avg);
getch();
}

OUTPUT

**** Sum of all +ve,-ve and Average of all numbers in an array ***

Enter size of array 6


Enter 6 element  5 -4 -2 6 4 0
Sum of all positive numbers  15
Sum of all negative numbers  -6
Average of all numbers  1.50

2. WRITE A PROGRAM TO INPUT N REAL NUMBERS AND


TO FIND THE MEAN, VARIANCE AND STANDARD

Dept. of MCA Sir MVIT 16


C PROGRAMMING LABORATORY
[10MCA11]

DEVIATION WHERE MEAN = ∑ XI/N , VARIANCE = ∑(XI-


MEAN)2 & DEVIATION = √VARIANCE.

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,n;
float x[10],mean,var,sd,sum=0,sum1=0;
clrscr();
printf("Enter size of array\n");
scanf("%d",&n);
printf("Enter element one by one\n");
for(i=0;i<n;i++)
scanf("%f",&x[i]);
for(i=0;i<n;i++)
sum=sum+x[i];
mean=sum/n;
for(i=0;i<n;i++)
sum1=sum1+pow((x[i]-mean),2);
var=sum1/n;
sd=sqrt(var);

printf("Average of all elements : %.2f\n",mean);


printf("Varience of all elements : %.2f\n",var);
printf("Standard deviation : %.2f",sd);
getch();
}

OUTPUT
Enter size of array
5
Enter element one by one
53476
Average of all elements : 5.00
Varience of all elements : 2.00
Standard deviation : 1.41

3. WRITE A PROGRAM TO INPUT N NUMBERS (INTEGERS


OR REAL) AND STORE THEM IN AN ARRAY. CONDUCT A
LINEAR SEARCH FOR A GIVEN KEY NUMBER AND
REPORT SUCCESS OR FAILURE IN THE FORM OF A
SUITABLE MESSAGE.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,a[10],key,f=0;

Dept. of MCA Sir MVIT 17


C PROGRAMMING LABORATORY
[10MCA11]

clrscr();
printf("Enter size of array\n");
scanf("%d",&n);
printf("Enter elements one by one\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter key element\n");
scanf("%d",&key);
for(i=0;i<n;i++)
if(key==a[i])
{
f=1;
break;
}
if(f==1)
printf("Key element is found\n");
else
printf("Key element is not found\n");
getch();
}

OUTPUT1
Enter size of array
5
Enter elements one by one
52481
Enter key element
1
Key element is found
OUTPUT2
Enter size of array
5
Enter elements one by one
12345
Enter key element
8
Key element is not found

4. WRITE A PROGRAM TO SORT N NUMBERS IN


ASCENDING OR DESCENDING ORDER USING BUBBLE
SORT.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n,a[10],temp;
clrscr();
printf("Enter size of array\n");
scanf("%d",&n);
printf("Enter elements one by one\n");
for(i=0;i<n;i++)

Dept. of MCA Sir MVIT 18


C PROGRAMMING LABORATORY
[10MCA11]

scanf("%d",&a[i]);
for(i=1;i<n;i++)
for(j=0;j<n-i;j++)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
printf("Sorted array elements are\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);

getch();
}

OUTPUT
Enter size of array 6
Enter elements one by one
196842
Sorted array elements are
124689

5. WRITE A PROGRAM TO ACCEPT N NUMBERS SORTED IN


ASCENDING ORDER AND SEARCH FOR A GIVEN
NUMBER USING BINARY SEARCH. REPORT SUCCESS OR
FAILURE IN THE FORM OF SUITABLE MESSAGES.

# include <stdio.h>
void main()
{
int x[10],i,low, high, mid,key,n,f=0;
clrscr();
printf("Enter size of array\n");
scanf("%d",&n);
printf("Enter elements in ascending order\n");
for(i=0;i<n;i++)
scanf("%d",&x[i]);

printf("Enter key elements \n");


scanf("%d",&key);
low = 0;
high = n;
while (low <= high)
{
mid = (low+high) / 2;
if (key == x[mid])
{
f=1;
break;
}
else if (key < x[mid])

Dept. of MCA Sir MVIT 19


C PROGRAMMING LABORATORY
[10MCA11]

high = mid - 1;
else if (key > x[mid])
low = mid + 1 ;
}
if(f==1)
printf("Sucessfull search\n");
else
printf("Unsucessfull search\n");
getch();
}

OUTPUT1
Enter size of array
6
Enter elements in ascending order
123456
Enter key elements
4
Sucessfull search
OUTPUT2
Enter size of array
5
Enter elements in ascending order
12345
Enter key elements
6
Unsucessfull search

6. WRITE A PROGRAM TO READ TWO MATRICES A AND B


OF SIZE M X N AND PERFORM PRODUCT OF TWO GIVEN
MATRICES.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,a[10][10],b[10][10],c[10][10];
int r1,r2,c1,c2;
clrscr();
printf("Enter 1st matrix size\n");
scanf("%d%d",&r1,&c1);
printf("Enter 2nd matrix size\n");
scanf("%d%d",&r2,&c2);

if(c1!=r2)
printf("Matrix multiplication is not possible\n");
else
{
printf("Enter elements to 1st matrix\n");
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);

Dept. of MCA Sir MVIT 20


C PROGRAMMING LABORATORY
[10MCA11]

printf("Enter elements to 2nd matrix\n");


for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
scanf("%d",&b[i][j]);

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

printf("Product of 2 matrix is\n");


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

OUTPUT1
Enter 1st matrix size
23
Enter 2nd matrix size
23
Matrix multiplication is not possible
OUTPUT2
Enter 1st matrix size
22
Enter 2nd matrix size
22
Enter elements to 1st matrix
12
34
Enter elements to 2nd matrix
12
34
Product of 2 matrix is
7 10
15 22

7. WRITE A PROGRAM TO LIST THE NAMES OF STUDENTS


WHO HAVE SCORED MORE THAN 60% OF TOTAL MARKS
IN THREE SUBJECTS USING STRUCTURE VARIABLES.

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

Dept. of MCA Sir MVIT 21


C PROGRAMMING LABORATORY
[10MCA11]

struct student
{
char name[20];
char USN[10];
int m1,m2,m3;
float tot,avg;
}s[10];
void main()
{
int i,n;
printf("Enter how many student record U want to enter\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter %d student record\n",i);
printf("Enter Name : ");
scanf("%s",s[i].name);
printf("Enter USN : ");
scanf("%s",s[i].USN);
printf("Enter Subject1 marks : ");
scanf("%d",&s[i].m1);
printf("Enter Subject2 marks : ");
scanf("%d",&s[i].m2);
printf("Enter Subject3 marks: ");
scanf("%d",&s[i].m3);
s[i].tot=s[i].m1+s[i].m2+s[i].m3;
s[i].avg=s[i].tot/3;
}
printf("Name of Students who scored more than 60%\n");
for(i=1;i<=n;i++)
{
if(s[i].avg>=60)
printf("%s",s[i].name);
}
getch();
}

OUTPUT
Enter how many student record U want to enter
3
Enter 1 student record
Enter Name : Vikram
Enter USN : MCA12
Enter Subject1 marks : 69
Enter Subject2 marks : 89
Enter Subject3 marks: 78
Enter 2 student record
Enter Name : Rahul
Enter USN : MCA13
Enter Subject1 marks : 44
Enter Subject2 marks : 56
Enter Subject3 marks: 61
Enter 3 student record
Enter Name : Rakesh

Dept. of MCA Sir MVIT 22


C PROGRAMMING LABORATORY
[10MCA11]

Enter USN : MCA23


Enter Subject1 marks : 99
Enter Subject2 marks : 87
Enter Subject3 marks: 84
Name of Students who scored more than 60%
Vikram
Rakesh

8. WRITE A PROGRAM TO COMPUTE THE SUM OF TWO


COMPLEX NUMBERS – PASSING A STRUCTURE TO A
FUNCTION.
#include<stdio.h>
#include<conio.h>

struct Complex_Number
{
float real;
float img;
};
typedef struct Complex_Number Complex;

Complex Sum(Complex,Complex);
void main()
{
Complex c1,c2,c3;
clrscr();
printf("Enter values for real & imaginary part of 1st complex number : ");
scanf("%f%f",&c1.real,&c1.img);
printf("Enter values for real & imaginary part of 2nd complex number : ");
scanf("%f%f",&c2.real,&c2.img);
printf("Sum of Complex number : ");
c3=Sum(c1,c2);
printf("%.3f+i%.3f",c3.real,c3.img);
getch();
}
Complex Sum(Complex c1,Complex c2)
{
Complex c3;
c3.real=c1.real+c2.real;
c3.img=c1.img+c2.img;
return c3;
}

OUTPUT
Enter values for real & imaginary part of 1st complex number: 3 5
Enter values for real & imaginary part of 2nd complex number: 2 8
Sum of Complex number: 5.000+i13.000

Dept. of MCA Sir MVIT 23


C PROGRAMMING LABORATORY
[10MCA11]

9. DEFINE A BOOK STRUCTURE HAVING TITLE OF THE


BOOK, ISBN, AUTHOR, PRICE AND MONTH AND YEAR
OF PUBLICATION AS ITS MEMBERS. USE A
SUBSTRUCTURE TO STORE THE MONTH IN FIVE
SUBJECTS, TOTAL AND PERCENTAGE OF MARKS AS ITS
MEMBERS. MARKS OF ALL THE SUBJECTS ARE TO BE
STORED IN AN ARRAY. DEVELOP A PROGRAM TO LIST
THE NAMES OF ALL THE STUDENTS WHO HAVE FAILED.

#include<stdio.h>
#include<conio.h>
struct Book
{
char title[20];
char ISBN[10];
char author[20];
float price;
struct publication
{
int month;
int year;
}p;
}b[10];
void main()
{
int i,n,j,month,year;
float price;
clrscr();

printf("Enter how many books entry U want to enter\n");


scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter %d Book information\n",i);
printf("Enter Title : ");
fflush(stdin);
gets(b[i].title);
printf("Enter ISBN : ");
fflush(stdin);
gets(b[i].ISBN);
printf("Enter author : ");
fflush(stdin);
gets(b[i].author);
printf("Enter price : ");
scanf("%f",&price);
b[i].price=price;
printf("Enter Month & Year of publication : ");
scanf("%d%d",&b[i].p.month,&b[i].p.year);
}
printf("Enter Date of publication\n");
printf("Enter Month & Year of publication : \n");
scanf("%d%d",&month,&year);

Dept. of MCA Sir MVIT 24


C PROGRAMMING LABORATORY
[10MCA11]

printf("Following are the List of Books published in %d/%d\n",month,year);


printf("========================================\n");
printf("TITLE\t\tISBN\t\tAUTHOR\t\tPRICE\n");
printf("========================================\n");
for(i=1;i<=n;i++)
if(month == b[i].p.month && year == b[i].p.year)
{
printf("%s\t%s\t%s\t%f\n",b[i].title,b[i].ISBN,b[i].author,b[i].price);
}

getch();
}

OUTPUT
Enter how many books entry U want to enter
3
Enter 1 Book information
Enter Title : C Programming
Enter ISBN : 87023423
Enter author : Rajaraman
Enter price : 380
Enter Month & Year of publication : 2 2010
Enter 2 Book information
Enter Title : Java Programming
Enter ISBN : 902398988
Enter author : Suresh Kumar
Enter price : 460
Enter Month & Year of publication : 2 2010
Enter 3 Book information
Enter Title : Software Testing
Enter ISBN : 1231098
Enter author : Young
Enter price : 410
Enter Month & Year of publication : 2 2009
Enter Date of publication
Enter Month & Year of publication :
2 2010
Following are the List of Books published in 2/2010
===================================================
=
TITLE ISBN AUTHOR PRICE
===================================================
=
C Programming 87023423 Rajaraman 380.00
Java Programming 902398988 Suresh Kumar 460.00

10. DEFINE A STUDENT STRUCTURE HAVING THE


NAME, USN(UNIVERSITY SEAT NUMBER), MARKS IN
FIVE SUBJECTS, TOTAL AND PERCENTAGE OF MARKS
AS ITS MEMBERS. MARKS OF ALL THE SUBJECTS ARE
TO BE STORED IN AN ARRAY. DEVELOP A PROGRAM TO

Dept. of MCA Sir MVIT 25


C PROGRAMMING LABORATORY
[10MCA11]

LIST THE NAMES OF ALL THE STUDENTS WHO HAVE


FAILED.

#include<stdio.h>
#include<conio.h>
struct student
{
char name[20];
char USN[10];
int m[10];
float tot,avg;
}s[10];
void main()
{
int i,n,j;
clrscr();
printf("Enter how many student record U want to enter\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter %d student record\n",i);
printf("Enter Name : ");
scanf("%s",s[i].name);
printf("Enter USN : ");
scanf("%s",s[i].USN);
for(j=1;j<=5;j++)
{
printf("Enter Subject%d marks : ",j);
scanf("%d",&s[i].m[j]);
}
s[1].tot=0;
for(j=1;j<=5;j++)
{
s[i].tot=s[i].tot+s[i].m[j];
s[i].avg=s[i].tot/3;
}
}
printf("Following are the list of Students who have failed\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=5;j++)
if(s[i].m[j]<35)
{
printf("====> %s \n",s[i].name);
break;
}
}
getch();
}

OUTPUT
Enter how many student record U want to enter : 3
Enter 1 student record

Dept. of MCA Sir MVIT 26


C PROGRAMMING LABORATORY
[10MCA11]

Enter Name : Vikram


Enter USN : MCA12
Enter Subject1 marks : 68
Enter Subject2 marks : 78
Enter Subject3 marks : 76
Enter Subject4 marks : 75
Enter Subject5 marks : 78

Enter 2 student record


Enter Name : Raju
Enter USN : MCA34
Enter Subject1 marks : 45
Enter Subject2 marks : 46
Enter Subject3 marks : 44
Enter Subject4 marks : 34
Enter Subject5 marks : 46

Enter 3 student record


Enter Name : Shekar
Enter USN : MCA35
Enter Subject1 marks : 12
Enter Subject2 marks : 14
Enter Subject3 marks : 23
Enter Subject4 marks : 34
Enter Subject5 marks : 22

Following are the list of Students who have failed


====> Raju
====> Shekar

11. WRITE A PROGRAM TO READ N INTEGERS AND


STORE THEM IN AN ARRAY, FIND THE SUM OF ALL
THESE ELEMENTS USING POINTERS. OUTPUT THE
GIVEN ARRAY AND THE COMPUTED SUM WITH
SUITABLE HEADING.

#include<stdio.h>
#include<conio.h>
void main()
{
int *ptr,a[10],n,sum=0,i;
clrscr();
printf("Enter size of array\n");
scanf("%d",&n);
printf("Enter elements one by one\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
ptr=a;
for(i=0;i<n;i++)

Dept. of MCA Sir MVIT 27


C PROGRAMMING LABORATORY
[10MCA11]

sum=sum+(*ptr)++;

printf("Sum =%d",sum);
getch();
}

OUTPUT
Enter size of array : 5
Enter elements one by one : 1 2 5 6 4
Sum = 15

12. WRITE A PROGRAM TO READ AND WRITE TO A


FILE.

#include <stdio.h>

main()
{
FILE *f1;
char c;
printf("Data Input\n\n");
/* Open the file INPUT */
f1 = fopen("INPUT", "w");

/* Get a character from keyboard */


while((c=getchar()) != EOF)

/* Write a character to INPUT */


putc(c,f1);
/* Close the file INPUT */
fclose(f1);
printf("\nData Output\n\n");
/* Reopen the file INPUT */
f1 = fopen("INPUT","r");

/* Read a character from INPUT*/


while((c=getc(f1)) != EOF)

/* Display a character on screen */


printf("%c",c);

/* Close the file INPUT */


fclose(f1);
}

OUTPUT
Data Input

This is to test^Z

Data Output

Dept. of MCA Sir MVIT 28


C PROGRAMMING LABORATORY
[10MCA11]

This is to test

13. WRITE A PROGRAM TO CREATE AND COUNT


NUMBER OF CHARACTERS IN A FILE.

#include <stdio.h>

void main()
{
FILE *f1;
char c;
int count=0;
printf("Data Input\n\n");
/* Open the file INPUT */
f1 = fopen("INPUT", "w");

/* Get a character from keyboard */


while((c=getchar()) != EOF)

/* Write a character to INPUT */


putc(c,f1);
/* Close the file INPUT */
fclose(f1);
printf("\nData Output\n\n");
/* Reopen the file INPUT */
f1 = fopen("INPUT","r");

/* Read a character from INPUT*/


while((c=getc(f1)) != EOF)
{ count=count+1;
printf("%c",c);
}
printf("Number of character in the file is %d",count);
/* Close the file INPUT */
fclose(f1);
}

OUTPUT
Data Input

This is to test^Z

Data Output

This is to test

Number of character in the file is 15

14. WRITE A PROGRAM TO HANDLE FILES WITH


MIXED DATA TYPES

Dept. of MCA Sir MVIT 29


C PROGRAMMING LABORATORY
[10MCA11]

#include <stdio.h>

main()
{
FILE *fp;
int number, quantity, i;
float price, value;
char item[10], filename[10];

printf("Input file name\n");


scanf("%s", filename);
fp = fopen(filename, "w");
printf("Input inventory data\n\n");
printf("Item name Number Price Quantity\n");
for(i = 1; i <= 3; i++)
{
fscanf(stdin, "%s %d %f %d",
item, &number, &price, &quantity);
fprintf(fp, "%s %d %.2f %d",
item, number, price, quantity);
}
fclose(fp);
fprintf(stdout, "\n\n");

fp = fopen(filename, "r");

printf("Item name Number Price Quantity Value\n");


for(i = 1; i <= 3; i++)
{
fscanf(fp, "%s %d %f %d",item,&number,&price,&quantity);
value = price * quantity;
fprintf(stdout, "%-8s %7d %8.2f %8d %11.2f\n",
item, number, price, quantity, value);
}
fclose(fp);
}

OUTPUT
Input file name
Computer
Input inventory data

Item name Number Price Quantity


Monitor 123 3400 5
CPU 124 2400 4
RAM 125 1200 4

Item name Number Price Quantity Value


Monitor 123 3400.00 5 17000.00
CPU 124 2400.00 4 9600.00
RAM 125 1200.00 4 4800.00

Dept. of MCA Sir MVIT 30


C PROGRAMMING LABORATORY
[10MCA11]

Dept. of MCA Sir MVIT 31

You might also like