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

C Lab File

The document contains 25 C programming code examples that demonstrate basic programming concepts like input/output, if-else statements, loops, functions, arrays, and more. The code examples include programs to print "Hello World", calculate areas of shapes, check if a number is even/odd, prime, sort arrays, add/multiply matrices, and construct a basic calculator. The practical file submitted by a student contains the code and output for each program.

Uploaded by

Lagneshwar S
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

C Lab File

The document contains 25 C programming code examples that demonstrate basic programming concepts like input/output, if-else statements, loops, functions, arrays, and more. The code examples include programs to print "Hello World", calculate areas of shapes, check if a number is even/odd, prime, sort arrays, add/multiply matrices, and construct a basic calculator. The practical file submitted by a student contains the code and output for each program.

Uploaded by

Lagneshwar S
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 30

COMM-IT CAREER academy

(Affiliated GGSIP University)


FC-1 Sheikh Sarai,
Chirag Delhi – 110017.

Practical file
on
‘c’program lab
(BCA 171)

SUBMITTED BY submitted to
LAgneshwar.s ms. juveria mam Enroll no.
112002021 (ASSISTANT PROFESSOR )
C PROGRAMS
1.WAP To Print HELLOWORLD
#include<stdio.h> voidmain(){

printf("HelloWorld");
}

OUT P UT :

2.WAP TO ADD TWO NUMBERS ENTERED BY THE


USER
#include<stdio.h> voidmain(){
inta,b,sum; printf("Entertwonumbers"); scanf("%d
%d",&a,&b);
sum=a+b;

printf("%d+%d=%d",a,b,sum);
}

OUT P UT :

3.WAP TO CALCULATE AREA OF RECTANGLE


#include<stdio.h> voidmain(){ intlength,breadth; intarea;
printf("EnterthelengthofRectangle:"); scanf("%d",&length);
printf("EnterthebreadthofRectangle:");
scanf("%d",&breadth); area=length*breadth; printf("\
nAreaofRectangleis:%d",area);
}

OUTPUT:
4.WAP TO CALCULATE AREA OF CIRCLE
#include<stdio.h> voidmain(){
floatradius,area; printf("\nEntertheradiusofCircle:");
scanf("%f",&radius);

area=3.14*radius*radius; printf("\nAreaofCircle:%f",area);
}

OUTPUT:

5.WAP TO CHECK IF INTEGER IS EVEN OR ODD


#include<stdio.h> voidmain(){
inta; printf("EnterTheNumber");
scanf("%d",&a); if(a%2==0)
printf("%disEven",a);

else printf("%disOdd",a);
}

OUT P UT :

6.WAP TO CHECK IF A GIVEN NUMBERIS PRIME OR


NOT
#include<stdio.h> voidmain(){
inta,i,c=0; printf("Enterapositiveinteger:"); scanf("%d",&a);

for(i=2;i<=a/2;i++){ //conditionfornon-prime if(a%i==0){


c=1;

break;

if(a==1){

printf("1isneitherprimenorcomposite.");

else{

if(c==0)

printf("%disaprimenumber.",a);
else

printf("%disnotaprimenumber.",a);

}
OUT P UT :

7.WAP TO DISPLAY FIBONACCISERIES


#include<stdio.h> voidmain(){
inti,n;

//initializefirstandsecondterms intt1=0,t2=1;
//initializethe3rdterm intnextTerm=t1+t2;
//getno.oftermsfromuser printf("Enterthenumberofterms:");
scanf("%d",&n);
//printthefirsttwotermst1andt2 printf("FibonacciSeries:%d,%d,",t1,t2);
//print3rdtonthterms for(i=3;i<=n;++i){ printf("%d,",nextTerm);
t1=t2; t2=nextTerm; nextTerm=t1+t2;
}
}

OUT P UT :

8.WAP TO CALCULATE FACTORIAL OF A NUMBER


#include<stdio.h> voidmain(){
inti,fact=1,num; printf("EnteraNumber:");
scanf("%d",&num); for(i=1;i<=num;i++){
fact=fact*i;

printf("Factorialof%dis:%d",num,fact);

OUTPUT:

9.WAP TO CONVERT TEMPERATURE FROM


CELSIUSTOFAHRENHE IT BY TAKING INPUT BY THE
USER
#include<stdio.h> voidmain()
{

floatcelsius,fahrenheit; printf("EntertemperatureinCelsius:"); scanf("%f",&celsius);


fahrenheit=(celsius*9/5)+32; printf("%.3fCelsius=
%.3fFahrenheit",celsius,fahrenheit);
}

OUTPUT:

10.WAP TO CHECK GREATEST NUMBER AMONG


THREE OF THEM
#include<stdio.h> voidmain()
{

intnum1,num2,num3; printf("Enterthevaluesofnum1,num2andnum3"); scanf("%d%d


%d",&num1,&num2,&num3); printf("num1=%d\num2=%d\num3=%d\n",num1,num2,num3);
if(num1>num2)
{

if(num1>num3)

{
printf("num1isthegreatestamongthree");

else

printf("num3isthegreatestamongthree");

elseif(num2>num3)

printf("num2isthegreatestamongthree");

else

printf("num3isthegreatestamongthree");

}
OUT P UT :

11.WAP TO MAKE A SIMPLE CALCULATOR USING


SWITCH STATEMENT
#include<stdio.h> voidmain(){
charop; doublefirst,second;
printf("Enteranoperator(+,-,*,/):"); scanf("%c",&op);
printf("Entertwooperands:"); scanf("%lf
%lf",&first,&second);

switch(op){
case'+': printf("%.1lf+%.1lf=%.1lf",first,second,first+second); break; case'-':
printf("%.1lf-%.1lf=%.1lf",first,second,first-second); break;
case'*': printf("%.1lf*%.1lf=%.1lf",first,second,first*second); break;
case'/': printf("%.1lf/%.1lf=%.1lf",first,second,first/second); break;

default:

printf("Error!operatorisnotcorrect");

OUTPUT:

12.WAP TO DISPLAY THE PATTERN UPTON


ROWS,TAKING THE VALUE OFNFROMUSER
*
** *** **** *****
#include<stdio.h> voidmain()
{ inti,j,rows; printf("Entertherows.");
scanf("%d",&rows); for(i=1;i<=rows;i+
+)
{

for(j=1;j<=i;j++)
{

printf("*");

printf("\n");

}
}

OUT P UT :

13.WAP TO DISPLAY THE FLOYD'STRIANGLE


,ROWS ENTERED BY THE USER
1
1 456 78910 1112131415
#include<stdio.h> voidmain()
{

intnum,i,j,k=1;
printf("EnteranumbertodefinetherowsinFloyd'striangle:\n");
scanf("%d",&num); for(i=1;i<=num;i++)
{

for(j=1;j<=i;j++)

printf("%2d",k++);

}
printf("\n");

}
}

OUT P UT :

14.WAP TO READ AND PRINT ELEMENTS OF AN


ARRAY
#include<stdio.h> voidmain(){
inta[5]={2,3,4,5,6};

inti; printf("Elementsofanarray:"); for(i=0;i<5;i++){


printf("\n%d",a[i]);

}
}

OUT P UT :

15.WAP TO INPUT MARKS OF 50 STUDENTS


USING AN ARRAY AND DISPLAY THE AVERAGE MARKS
OF THE CLASS
#include<stdio.h> voidmain(){ inta[50],i,sum=0,n;
floatavg; printf("Entertheno.ofstudents");
scanf("%d",&n); for(i=0;i<n;i++){ scanf("%d",&a[i]);
sum=sum+a[i];

avg=(float)sum/i; printf("AverageMarks:%f",avg);
}

OUT P UT :

16.WAP TO SORT AN ARRAY IN ASCENDING ORDER


#include<stdio.h> voidmain(){
inti,j,a,n,num[20]; printf("Entertheno.ofelements:");
scanf("%d",&n); printf("Entertheelements:"); for(i=0;i<n;i++){
scanf("%d",&num[i]);

for(i=0;i<n;i++){
for(j=i+1;j<n;j++){ if(num[i]>num[j])
{ a=num[i]; num[i]=num[j]; num[j]=a;
}

printf("AscendingOrder:\n"); for(i=0;i<n;i++){
printf("\n%d",num[i]);

OUTPUT:

17.WAP TO READ AND PRINT ELEMENTS OF TWO


DIMENSIONAL ARRAY
#include<stdio.h> voidmain()
{

inti=0,j=0;

inta[4][3]={{5,10,15},{1,2,3},{3,4,5},{6,7,8}};
printf("EnterElementsof2-DArray:"); for(i=0;i<4;i++)
{ for(j=0;j<3;j++){ printf("a[%d][%d]=%d\n",i,j,a[i][j]);
}

OUTPUT:

18.WAP TO ADD TWO MATRICES USING ARRAY


#include<stdio.h> voidmain()
{

intm,n,c,d,first[10][10],second[10][10],sum[10][10];
printf("Enterthenumberofrowsandcolumnsofmatrix\n"); scanf("%d%d",&m,&n);
printf("Entertheelementsoffirstmatrix\n"); for(c=0;c<m;c++)
for(d=0;d<n;d++)

scanf("%d",&first[c][d]);

printf("Entertheelementsofsecondmatrix\n"); for(c=0;c<m;c++)
for(d=0;d<n;d++)

scanf("%d",&second[c][d]); printf("Sumofenteredmatrices:-\n");
for(c=0;c<m;c++){

for(d=0;d<n;d++){

sum[c][d]=first[c][d]+second[c][d]; printf("%d\t",sum[c][d]);
}
printf("\n");

}
}

OUT P UT :

19.WAP TO MULTIPLY TWO MATRICES USING


ARRAY
#include<stdio.h> voidmain()
{

inta,b,c,d,first[10][10],second[10][10],multiply[10][10];
printf("Enterthenumberofrowsandcolumnsofmatrix\n"); scanf("%d%d",&a,&b);
printf("Entertheelementsoffirstmatrix\n"); for(c=0;c<a;c++)
for(d=0;d<b;d++)

scanf("%d",&first[c][d]);
printf("Entertheelementsofsecondmatrix\n"); for(c=0;c<a;c++)
for(d=0;d<b;d++)

scanf("%d",&second[c][d]);

printf("Multiplicationofenteredmatrices:-\n"); for(c=0;c<a;c++){
for(d=0;d<b;d++){

multiply[c][d]=first[c][d]*second[c][d]; printf("%d\t",multiply[c]
[d]);
}

printf("\n");

OUTPUT:

20.WAP TO ADD TWO NUMBERS USING FUNCTIONS


#include<stdio.h> intsum(int,int); //functiondeclaration
intmain()
{
intnum1,num2,total; printf("Enterthetwonumber"); scanf("%d
%d",&num1,&num2); total=sum(num1,num2);//callingfunction
printf("Thesumofthesenumbers:%d",total); getch(); return0;
}

intsum(inta,intb) //functiondefinition

intresult=a+b; returnresult;
}

OUTPUT:

21.WAP TO FIND FACTORIAL OF A NUMBER USING


RECURSION
#include<stdio.h> intmultiplyNumbers(intn); intmain(){
intn;

printf("Enterapositiveinteger:"); scanf("%d",&n); printf("Factorialof%d=


%d",n,multiplyNumbers(n)); return0;
}

intmultiplyNumbers(intn){

if(n>=1)

returnn*multiplyNumbers(n-1); else

return1;

OUTPUT:
22.WAP TO FIND AREA OF CIRCLEUSING FUNCTION
#include<stdio.h> //Functiondeclaration
floatareaOfcircle(floatradius_circle); intmain(){
floatradius; printf("Entertheradiusofcircle:"); scanf("%f",&radius);
printf("Areaofcircle:%.2f",areaOfcircle(radius)); printf("\n"); return0;
}

//functiondefinitiontocalculateareaofcircle
floatareaOfcircle(floatradius_circle){
floatarea_circle; area_circle=3.14*radius_circle*radius_circle;
returnarea_circle;

OUTPUT:

23.WAP TO FIND THE SQUARE OF ANY NUMBER


USING FUNCTIONS
#include<stdio.h>

//functiondeclaration&definition

floatsquareOfNumber(floatnum)

return(num*num);

intmain()
{

floatnumber,square;//callingfunction

printf("PleaseEnteranyintegerValue:"); scanf("%f",&number);
square=squareOfNumber(number); printf("squareofagivennumber%.2fis=
%.2f",number,square); return0;
}

OUTPUT:

24.WAP TO SWAP TWO NUMBERS USING FUNCTIONS


#include<stdio.h> voidswap(int*xp,int*yp)
{

inttemp=*xp; *xp=*yp;
*yp=temp;

intmain()

intx,y; printf("EnterValueofx:"); scanf("%d",&x); printf("\


nEnterValueofy:"); scanf("%d",&y); swap(&x,&y); printf("\
nAfterSwapping:x=%d,y=%d",x,y); return0;
}

OUTPUT:
25.WRITE A MENU DRIVEN PROGRAM TO
CONSTRUCT A CALCULATOR FOR FOLLOWING
ARITHMETIC OPERATIONS:
•ADDITION •SUBTRACTION •MULTIPLICATION
•DIVISION •AVERAGE
#include<stdio.h>
voidmain()
{
intch,a,b;
do
{
printf("\nMenu\n1.Add\n2.Subtract\n3.Divide\n4.Multiply\n
5.Average\n6.Exit"); printf("\nEnterYourChoice"); scanf("%d",&ch);
switch(ch)
{
case1:
printf("\nEnter2Numbers");

scanf("%d%d",&a,&b); printf("\nAdd
%d",a+b); break;
case2:
printf("\nEnter2Numbers"); scanf("%d%d",&a,&b);
printf("\nSubtract%d",a-b); break;
case3:
printf("\nEnter2Numbers"); scanf("%d%d",&a,&b);
printf("\nDivide%d",a/b); break;
case4:
printf("\nEnter2Numbers"); scanf("%d%d",&a,&b);
printf("\nMultiply%d",a*b); break;
case5:
printf("\nEnter2Numbers"); scanf("%d%d",&a,&b);
printf("\nAverage%d",(a+b)/2); break;
case6:
break;
default:
printf("\nInvaidInput");
}
}while(ch!=6);
}
OUTPUT:

26.WAP TO CHECK IF ASTRINGISA PALLINDROME OR


NOT
#include<stdio.h> #include<string.h>
voidisPalindrome(charstr[])
{

intl=0; inth=strlen(str)-1; while(h>l)


{

if(str[l++]!=str[h--])

printf("%sisnotapalindrome\n",str); return;
}

}
printf("%sisapalindrome\n",str);

intmain()

isPalindrome("tenet"); isPalindrome("level");
isPalindrome("apple"); return0;
}

OUTPUT:

27.WAP TO ADD,SUB,MULTIPLY AND DIVIDE TWO


NUMBERS USING POINTERS
#include<stdio.h> intmain()
{

intfirst,second,*p,*q,sum,sub,multiply,division; printf("Entertwointegers:\n");
scanf("%d%d",&first,&second); p=&first; q=&second; sum=*p+*q; sub=*p-*q;
multiply=*p**q; division=*p/*q; printf("Sumofthenumbers=%d\n",sum);
printf("Subtractionofthenumbers=%d\n",sub);
printf("Multiplicationofthenumbers=%d\n",multiply);
printf("divisionofthenumbers=%d\n",division); return0;
}

OUT P UT :
28.WAP TO CREATE A STRUCTURE FOR EMPLOYEES
CONTAINING THE FOLLOWING DATA MEMBERS:
EmployeeID,EmployeeName,Age,Address,Salara
y
andDepartment
INPUTDATAFOR10EMPLOYEESANDDISPLAY
THEDETAILSOFTHEEMPLOYEEFROMTHE
EmployeeIDGIVENBYTHEUSER
#include<stdio.h>
#include<string.h>
structemployee{ intid;
charname[10]; intage;
intsalary; charaddress[20];
chardepartment[20];
};

intmain(){ inti;
structemployee
st[10];

printf("\nEnterTheDetailsof10Employees....."); for(i=0;i<10;i++)
{ printf("\nEnterId:");

scanf("%d",&st[i].id); printf("\nEnterName:");
scanf("%s",st[i].name); printf("\nEnterAge:");
scanf("%s",&st[i].age); printf("\nEnterSalary:");
scanf("%s",&st[i].salary); printf("\nEnterAddress:");
scanf("%s",st[i].address); printf("\nEnterDepartment:");
scanf("%s",st[i].department);
}
printf("\nEmployeesDetails...."); for(i=0;i<10;i++){
printf("\nId:%d,Name:%s,Age:%d,Salary:%d,
Address:%s,Department:%s",st[i].id,st[i].name,st[i].age,st[i].salary,st[i].
address,st[i].department);

return0;

OUTPUT:

29.WAP TO MAKE USE OF ARRAYS WITH STRUCTURES


IN THE FOLLOWING WAYS:
•USE ARRAY AS A STRUCTURE DATA MEMBER
#include<stdio.h> structstudent
{

charname[20]; intid;
floatmarks; }; voidmain()
{

structstudents1,s2,s3; inta;
printf("Enterthename,id,andmarksofstudent1:"); scanf("%s%d
%f",s1.name,&s1.id,&s1.marks); scanf("%c",&a);
printf("Enterthename,id,andmarksofstudent2:"); scanf("%s%d
%f",s2.name,&s2.id,&s2.marks); scanf("%c",&a);
printf("Enterthename,id,andmarksofstudent3:"); scanf("%s%d
%f",s3.name,&s3.id,&s3.marks); scanf("%c",&a);
printf("Studentsdetails....\n"); printf("%s%d%f\n",s1.name,s1.id,s1.marks);
printf("%s%d%f\n",s2.name,s2.id,s2.marks); printf("%s%d%f\
n",s3.name,s3.id,s3.marks);
}
OUT P UT :

•CREATE ARRAY OF STRUCTURE VARIABLES


#include<stdio.h> #include<string.h>
structstudent{ introllno;
charname[10]; };
intmain(){ inti; structstudentst[5];
printf("EnterRecordsof5students"); for(i=0;i<5;i++)
{ printf("\nEnterRollno:"); scanf("%d",&st[i].rollno);
printf("\nEnterName:"); scanf("%s",&st[i].name);
}

printf("\nStudentInformationList:"); for(i=0;i<5;i++){ printf("\nRollno:


%d,Name:%s",st[i].rollno,st[i].name);
}

return0;

OUTPUT:
30.WAP TO CREATE TWO FILES WITH NAMES
Even File AND Odd File.INPUT 20 NUMBERS
FROMTHEUSERANDSAVEEVENN0.SIN Even File AND
ODD NO.SIN Odd File.
#include<stdio.h>

#include<math.h> #include<stdlib.h>
voidmain()
{

FILE*fp1,*fp2,*fp3; intn,i,size;
printf("Enterno.ofdigits:"); scanf("%d",&size);
fp1=fopen("NUMBER.txt","w");
printf("Enterthenumbers:\n");
for(i=0;i<size;i++)
{
fflush(stdin); scanf("%d",&n); fputc(n,fp1);
}

fclose(fp1); fp1=fopen("NUMBER.txt","r");
fp2=fopen("EVEN.txt","w"); fp3=fopen("ODD.txt","w");
while((n=fgetc(fp1))!=EOF)
{

if(n%2==0) fputc(n,fp2);
else fputc(n,fp3);
}

fclose(fp1); fclose(fp2); fclose(fp3);


fp1=fopen("NUMBER.txt","r");
fp2=fopen("EVEN.txt","r");
fp3=fopen("ODD.txt","r");
printf("Thecontentofnumberfileare:");
while((n=fgetc(fp1))!=EOF) printf("%d",n);
printf("\nThecontentofevenfileare:");
while((n=fgetc(fp2))!=EOF) printf("%d",n);
printf("\nThecontentofoddfileare:");
while((n=fgetc(fp3))!=EOF) printf("%d",n);
fclose(fp1); fclose(fp2); fclose(fp3);
}

OUTPUT:

You might also like