c Manual
c Manual
[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.
7. Write a program to find the value of Sin (x) using the series.
Sin (x) = x – x3/3! + x5/5! – x7/7! + …………….
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.
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.
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.
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.
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.
Note: Students are required to execute one question from Part A and one
from Part B
OUPUT
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
OUPUT
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
OUPUT
** DISPLAYING .EXE .OBJ .BAT & .BAK FILES OF CURRENT DIRECTORY **
FOLLOWING ARE THE .EXE FILES IN THE CURRENT DIRECTORY ARE .....
FOLLOWING ARE THE .OBJ FILES IN THE CURRENT DIRECTORY ARE .....
Directory of E:\VAS\LM
FOLLOWING ARE THE .BAT FILES IN THE CURRENT DIRECTORY ARE .....
FOLLOWING ARE THE .BAT FILES IN THE CURRENT DIRECTORY ARE .....
FOLLOWING ARE THE .BAK FILES IN THE CURRENT DIRECTORY ARE .....
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
#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;
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
#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)
{
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
#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);
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
#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]
#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
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char str[30];
int ch,i;
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
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]
#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
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;
}
OUTPUT
**** Sum of all +ve,-ve and Average of all numbers in an array ***
#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);
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
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,a[10],key,f=0;
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
#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++)
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
# 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]);
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
#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]);
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];
}
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
#include<stdio.h>
#include<conio.h>
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
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
#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();
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
#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
#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++)
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
#include <stdio.h>
main()
{
FILE *f1;
char c;
printf("Data Input\n\n");
/* Open the file INPUT */
f1 = fopen("INPUT", "w");
OUTPUT
Data Input
This is to test^Z
Data Output
This is to test
#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");
OUTPUT
Data Input
This is to test^Z
Data Output
This is to test
#include <stdio.h>
main()
{
FILE *fp;
int number, quantity, i;
float price, value;
char item[10], filename[10];
fp = fopen(filename, "r");
OUTPUT
Input file name
Computer
Input inventory data