Sie sind auf Seite 1von 26

PRACTICAL FILE

ON
PRACTICAL OF C

SUBMITTED TO: SUBMITTED BY:


PIMG SANDEEP KUMAR SINGH
DECLARATION

I, Sandeep Kumar Singh student of BCA I Semester of Prestige Institute of Management


Gwalior, hereby declare that the practical file entitled “Practical in c” is submitted by me in
the line of partial fulfillment of course objectives for the Bachelor of Computer Application
Degree.

I assure that this practical file is the result of my own efforts and that any other institute for
the award of any degree or diploma has not submitted it.

Date:

Place: Gwalior
CERTIFICATE

This is to certify that Sandeep Kumar Singh Student of BCA I Semester of Prestige Institute
of Management Gwalior has successfully completed their Practical file. He has prepared this
practical file entitled “Practical in c” under my direct supervision and guidance.

Prof. NITIN PAHARIA

(Faculty Guide)
ACKNOWLEDGEMENT

I express my sincere gratitude to Prof. Nitin Paharia for giving me the opportunity to work
under her guidance on the report entitled “Practical in c”.

I am grateful to our Director Dr. S. S. Bhakar, Faculty Member and other friends for their
valuable suggestions in the execution of report preparation.

I am also thankful to other staff that guided and helped me very kindly at each and every step
whenever I required.

I also acknowledge & convey thanks to the library staff, computer department of PIMG for
their kind and valuable support.
Contents
1. WRITE A PROGRAM TO ADDING TWO NUMBERS? ............................................................................ 1
2.WRITE A PROGRAM TO CHECK GIVEN NUMBER IS PRIME OR NOT?................................................... 2
3. W.A.P TO PRINT THE ASSOCIATIVE DAY WITH NO. OF WEEK. ........................................................... 3
4.W.A.P TO CALCULATE THE FACTORIAL OF GIVEN NUMBER? .............................................................. 4
5. WRITE A PROGRAM TO SWAP THE VALUES BETWEEN TWO VARIABLES? ......................................... 5
6.WRITE A PROGRAM TO PRINT MULTIPLICATION TABLE OF GIVEN NUMBERS? ........... 6
7.TO PERFORM ARETHMETIC OPERATIONS USING SWITCH CASE? ...................................... 7
8.W.A.P TO COUNT THE NUMBER OF GIVEN DIGIT IN A INTEGER NUMBER?........................................ 8
9.PROGRAM TO UNDERSTAND THE STRCPY() FUNCTION ? ................................................ 9
10.PROGRAM TO PRINT THE TYPE OF ELEMENT OF STRING. ............................................................... 10
11. WAP TO INPUT 10 ELEMENTS OF AN ARRAY & DISPLAY THESE ELEMENTS INN
REVERSE ORDER. ............................................................................................................................. 11
12.W.A.P TO CHECK GIVEN STRING IS PALINDROME OR NOT USING STRING
FUNCTION?......................................................................................................................................... 12
13.TO FIND THE MINIMUM AND MAXIMUM NUMBER IN AN ARRAY? ............................... 13
14. WAP TO INPUT ELEMENTS IN 2D ARRAY AND DISPLAY THIS ARRAY IN MARRIX
FORM. .................................................................................................................................................. 14
15.WAP TO INPUT ELEMENTS IN 2D ARRAY AND DISPLAY THE SUM OF MAIN
DIAGONALS OF THIS ARRAY. ....................................................................................................... 15
16. FUNCTION TO EXCHANGE FIRST HALF WITH SECOND HALF OF AN ARRAY ............ 16
17. FUNCTION TO FIND THE SUM OF EACH ROW AND COLUMN OF AN ARRAY USING
FUNCTION?..................................................................................................................................... 17
18. TO PRINT THE VALUE AND ADDRESS OF ELEMENTS IN ARRAY ................................... 18
19. WRITE A FUNCTION IN C++ WHICH ACCEPTS AN INTEGER ARRAY AND ITS SIZE AS
ARGUMENTS/PARAMETERS AND ASSIGNS THE ELEMENTS INTO A TWO-DIMENSIONAL
ARRAY OF INTEGERS IN THE FOLLOWING FORMAT: ............................................................. 19
20.DEFINED A STRUCTURE LIBRARY OF GIVEN MEMBER .................................................... 20
STRING- BOOK NAME ...................................................................................................................... 20
STRING-STUDENT NAME ................................................................................................................ 20
INTERGER-ID OF STUDENT ............................................................................................................ 20
AND ACCESS EACH MEMBER FOR THREE STUDENT .............................................................. 20
1. WRITE A PROGRAM TO ADDING TWO NUMBERS?

#include<stdio.h>
#include<conio.h>
void main()
{ int x,y,z;
clrscr();
printf("Enter the Numbers:");
scanf("%d%d",&x,&y);
z=x+y;
printf("\nAddition=%d",z);
getch();
}
OUTPUT:

1
2.WRITE A PROGRAM TO CHECK GIVEN NUMBER IS
PRIME OR NOT?

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{ int n,i,flag=1;
clrscr();
printf("Enter a number:")
scanf("%d",&n);
for(i=2;i<n;i++)
{if(n%i==0)
flag=0;
}
if(flag==1)
printf("%d is prime number",n);
else
printf("%d is not prime number",n);
getch();
}

OUTPUT

2
3. W.A.P TO PRINT THE ASSOCIATIVE DAY WITH NO. OF
WEEK.

#include<stdio.h>
#include<conio.h>
void main()
{ int ch;
clrscr()
printf(“\n\tEnter any nunber between 1-7”);
scanf("%d",&ch);
{ if(ch==1)
printf("\tMONDAY");
else if(ch==2)
printf("\tTUESDAY");
else if(ch==3)
printf("\tWEDNESDAY");
else if(ch==4)
printf("\tTHURSDAY");
else if(ch==5)
printf("\tFRIDAY");
else if(ch==6)
printf("\tSATURDAY");
else if(ch==7)
printf("\tSUNDAY");
else if(ch>=8)
printf("\tInvalid choice!");
}
printf("Want to check again?");
getch();
} OUTPUT

3
4.W.A.P TO CALCULATE THE FACTORIAL OF GIVEN
NUMBER?
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{ int n,i,fact=1;
clrscr();
printf("Enter a number:");

scanf("%d",&n);
for(i=n;i>0;i--)
{
fact=fact*i;
}

printf("%d is factorial of %d ",fact,n);

getch();
}
OUTPUT

4
5. WRITE A PROGRAM TO SWAP THE VALUES BETWEEN
TWO VARIABLES?

#include<stdio.h>
#include<conio.h>
void main()
{ int a,b,t;
clrscr();
printf("Enter values of 'A' and 'B'=");
scanf("%d%d",&a,&b);
t=a;
a=b;
b=t;
printf("A=%d B=%d",a,b);
getch();
}

OUTPUT:

5
6.WRITE A PROGRAM TO PRINT MULTIPLICATION TABLE
OF GIVEN NUMBERS?

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,m;
clrscr();
printf("enter the number");
scanf(“%d”,&n);
for(i=1;i<=10;i++)
{
m=i*n;
printf("\n %d*%d=%d",i*n,m);
}
getch();
}

OUTPUT:

6
7.TO PERFORM ARETHMETIC OPERATIONS USING
SWITCH CASE?
#include<stdio.h>
main ( )
{
char op;
int a,b;
printf ("Enter number operator and another number ") ;
scanf ("%d%c%d", &a, &op, &b) ;
switch(op)
{
case '+':
printf ("Result = %d\n", a+b) ;
break;
case '-':
printf ("Result = %d\n", a-b);
break;
case '*':
printf("Result = %d\n",a*b);
break;
case '/':
printf("Result = %d\n",a/b);
break;
case '%':
printf("Result = %d\n",a%b);
break;
default:
printf("Enter valid operator\n");
}
}
OUTPUT

7
8.W.A.P TO COUNT THE NUMBER OF GIVEN DIGIT IN A
INTEGER NUMBER?

#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,s,c=0;
clrscr();
printf("\nEnter the Numbers :");
scanf("%d",&n);
printf("\nEnter the Digit to Count :");
scanf("%d",&s);
while(n!=0)
{
r=n%10;
if(r==s)
c++;
n=n/10;
}
printf("\n%d Occurs %d times",s,c);
getch();
}

OUTPUT

8
9.PROGRAM TO UNDERSTAND THE STRCPY() FUNCTION ?

#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char str1[10], str2[10];
clrscr();
printf ("Enter the first string:" );
scanf("%s",&str1);
printf ("Enter the second string:");
scanf("%s",&str2) ;
strcpy(str1,str2);
printf ("First string ; %s\n ",str1);
strcpy(str1,"Delhi");
strcpy(str2,"Calcutta");
printf ("First string: %s\nSecond string: %s",str1,str2);
getch();

OUTPUT

9
10.PROGRAM TO PRINT THE TYPE OF ELEMENT OF
STRING.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{ char NAME[]="SaNDeeP sINgH";
int x,y;
clrscr();
for(y=0;NAME[y]!=’\0’,y++)
printf(" %s\n --------------------\n",NAME[y]);
for( x=0;x<strlen(NAME);x++)
{if(NAME[x]>=’a’&& NAME[x]<=’z’)
printf("%s - small letter.\n ",NAME[x]);
else if(NAME[x]>=’A’ && NAME[x]<=’Z’)
printf(" %s- capital letter.\n",NAME[x]);
else
printf("%s - Not a alphabet.\n",NAME[x]);
}
getch();
}
OUTPUT

10
11. WAP TO INPUT 10 ELEMENTS OF AN ARRAY & DISPLAY
THESE ELEMENTS INN REVERSE ORDER.

#include<stdio.h>
#include<conio.h>
void rev(int a[],int size);
void main()
{int n,i,a[100];
printf("Enter the size of array:");
scanf("%d",&n);
printf("enter the element of array:\n");
for(i=0;i<n;i++)
printf("%d",&a[i]);
rev(a,n);
printf(”REVERSED ARRAY IS :”);
for(i=0;i<n;i++)
printf(“%d”,a[i]);
getch();
}
void rev(int a[],int size)
{ int j,k,c;
for(k=0,j=size-1;k<j;k++,j--)
{ c=a[k];
a[k]=a[j];
a[j]=c;
}
}
OUTPUT

11
12.W.A.P TO CHECK GIVEN STRING IS PALINDROME OR
NOT USING STRING FUNCTION?

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{ char x[50],y[50];
clrscr();
gets(x);
strcpy(y,x);
strrev(y);
if(strcmp(y,x)==0)
printf("it is palindrome");
else
printf("it is not palindrome");
getch();
}
OUTPUT

12
13.TO FIND THE MINIMUM AND MAXIMUM NUMBER IN AN
ARRAY?

#include<stdio.h>
void main()
{
int i,max,min,arr[10]={23,56,3,78,47,8,35,7,4,45,9};
max=min=arr[0];
for(i=0;i<10;i++)
{
if(min>arr[i])
min=arr[i];
if(max<arr[i])
max=arr[i];
}
printf("Minimum number in the array is %d\n",min);
printf("Maximum number in the array is %d",max);
}

OUTPUT

13
14. WAP TO INPUT ELEMENTS IN 2D ARRAY AND DISPLAY
THIS ARRAY IN MARRIX FORM.

#include<stdio.h>
#include<conio.h>
void main()
{int Ar[10][10], i,j,r,c;
printf("Enter the no. of rows and columns of the array :\n");
scanf("%d%d",&r,&c);
printf("Enter the elements of the %dX%d array :\n",r,c);
for(i=0;i<r;i++)
{ for(j=0;j<c;j++)
scanf("%d",&Ar[i][j]);
}
printf("Entered Array :");
for(i=0;i<r;i++)
{ printf(" \n");
for(j=0;j<c;j++)
{ printf("%d ",Ar[i][j]);
}
}
}

OUTPUT

14
15.WAP TO INPUT ELEMENTS IN 2D ARRAY AND DISPLAY
THE SUM OF MAIN DIAGONALS OF THIS ARRAY.
#include<stdio.h>
#include<conio.h>
void main()
{ int a[10][10],row,col,i,j,sum=0;
clrscr();
printf("Enter the size of rows and columns of the array :\n");
scanf("%d%d",&row,&col);
printf("Enter the elements of the array :\n");
for(i=0;i<row;i++)
{for(j=0;j<col;j++)
scanf("%d",&a[i][j]);
}
printf("Enetred Matrix :\n");
for(i=0;i<row;i++)
{ for(j=0;j<col;j++)
printf("%d ",a[i][j]);
printf(" \n");
}
printf("Sum of main diagonals of the matrix : ");
for(i=0;i<row;i++)
{ for(j=0;j<col;j++)
{ if(i==j)
sum+=a[i][j];
}
}
printf(“%d”,sum);
getch();
}
OUTPUT

15
16. FUNCTION TO EXCHANGE FIRST HALF WITH SECOND
HALF OF AN ARRAY

#include<stdio.h>
#include<conio.h>
void EXCHANGE(int a[],int size);
void main()
{
int n,i,a[50];
clrscr();
printf("enter the no of element:");
scanf("%d",&n);
printf("enter the element of array:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
EXCHANGE(a,n);
getch();
}
//……………………………………………………………………………………………..
void EXCHANGE(int a[],int size)
{ int j,k,c;
for(k=0,j=size/2;k<size;k++,j++)
{ c=a[k];
a[k]=a[j];
a[j]=c;
}
printf(”the array after interchange 1st half with 2nd half is:”);
for(j=0;j<size;j++)
printf(" %d",a[j]);

}
//………………………………………………………………………………………………
OUTPUT

16
17. FUNCTION TO FIND THE SUM OF EACH ROW AND
COLUMN OF AN ARRAY USING FUNCTION?
#include<iostream.h>
#include<conio.h>
void sumofrows(a[][],int x,int y);
void main()
{ int a[100][100],n,m,i,j;
clrscr();
printf("enter the no. of row and coloumn is array:");
scanf("%d%d",&n,&m);
printf("enter the element of array:\n");
for(i=0;i<n;i++)
{for(j=0;j<m;j++)
scanf("%d",&a[i][j];
}
sumofrows(a,n,m);
getch();
}//…………………………………………………………………………………………………
void sumofrows(a[][],int x,int y)
{int i, j,sum=0,sum2=0,sum3=0 ;
printf("the sum of first row :");
for(i=0,j=0;j<y;j++)
sum+=a[i][j];
printf(“%d\n”,sum);
printf("the sum of 2nd row is:");
for(i=1,j=0;j<y;j++)
sum2+=a[i][j];
printf(“%d\n”sum2);
printf("the sumof 3rd row is:");
for(i=2,j=0;j<y;j++)
sum3+=a[i][j];
printf(“%d\n”,sum3);
}//…………………………………………………………………………………………………
OUTPUT

17
18. TO PRINT THE VALUE AND ADDRESS OF ELEMENTS IN
ARRAY

#include <stdio.h>
#include <stdlib.h>
int main()
{
int iarr[3]={1,2,3},*iptr,i;
float farr[3]={4.5,3.5,7.4},*fptr;
char carr[3]={'p','t','r'},*cptr;
iptr=iarr; fptr=farr; cptr=carr;
for(i=0;i<3;i++)
printf("\t %d at address %u \n", *(iptr+i) , (iptr+i));
printf("\n");
for(i=0;i<3;i++)
printf("\t %.2f at address %u \n", *(fptr+i) , (fptr+i) );
printf("\n");
for(i=0;i<3;i++)
printf("\t %c at address %u \n", *(cptr+i) , (cptr+i) );
return 0;
}
OUTPUT

18
19. WRITE A FUNCTION IN C++ WHICH ACCEPTS AN
INTEGER ARRAY AND ITS SIZE AS
ARGUMENTS/PARAMETERS AND ASSIGNS THE ELEMENTS
INTO A TWO-DIMENSIONAL ARRAY OF INTEGERS IN THE
FOLLOWING FORMAT:
IF THE ARRAY IS 1, 2, 3 THE RESULTANT 2-D ARRAY IS:
123
120
100
#include<stdio.h>
#include<conio.h>
void new2D(int a[],int n);
void main()
{ int a[50],b[50][50],n,i,j;
clrscr();
printf("enter the size of 1d array:");
scanf(“%d”,&n);
printf("enter the element od 1D array");
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
printf("\nafter assigning the 1D array into 2D array in above formats is:\n");
new2D(a,n);
getch();
}//…………………………………………………………………………………
void new2D(int a[], int n)
{ int b[50][50],i,j;
for(i=0;i<n;i++)
{for(j=0;j<n;j++)
{ if(i+j>=n)
b[i][j]=0;
else
b[i][j]=a[j];
printf(" %d\n",b[i][j]);}
}
} //………………………………………………………………………………
OUTPUT

19
20.DEFINED A STRUCTURE LIBRARY OF GIVEN MEMBER
STRING- BOOK NAME
STRING-STUDENT NAME
INTERGER-ID OF STUDENT
AND ACCESS EACH MEMBER FOR THREE STUDENT

#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
struct library
{
char b_name [20] ;
char s_name[30];
int id;
}stu1={"c in depth","Sandeep",35};
void main()
{
struct library stu2,stu3;
clrscr();
stu2.id=28; strcpy(stu2.b_name,"It basics "); strcpy(stu2.s_name,"Manohar");
printf("Enter the details of stu3 :\n");
scanf("%s%s%d",stu3.b_name,stu3.s_name,&stu3.id);
printf("stu1.b_name= %s\tstu1.id=%d\t",stu1.b_name,stu1.id);
printf("stu1.s_name=%s\n",stu1.s_name);
printf("stu2.b_name= %s\tstu2.id=%d\t",stu2.b_name,stu2.id);
printf("stu2.s_name=%s\n",stu2.s_name);
printf("stu3.b_name= %s\tstu3.id=%d\t",stu3.b_name,stu3.id);
printf("stu3.s_name=%s\n",stu3.s_name);
getch();
}

OUTPUT

20
21

Das könnte Ihnen auch gefallen