Comp Uter Progr Ammi NG: Practical File
Comp Uter Progr Ammi NG: Practical File
uter
Progr
ammi
ng
Practical file
SUBMITTED TO: SUBMITTED BY :
Mrs. Shailja Agnihotri NISHA CHAUHAN
CLASS : BCA (I)
ROLL NO. : 2048
//*PROGRAM TO DISPLAY NAME *//
#include <stdio.h>
#include <conio.h>
void main()
{
char name[15];
clrscr();
printf(“enter name”);
scanf(“%s,name);
printf(“\n name is -%s”,name);
getch();
}
OUTPUT:
enter name anjali
name is – anjali
//*PROGRAM TO FIND WHETHER THE GIVEN NUMBER
IS EVEN OR ODD *//
#include <stdio.h>
#include <conio.h>
void main()
{
int num;
clrscr();
printf(“enter number”);
scanf(“%d”,&num);
if(num%2==0)
{
printf(“\n number is even”);
}
else
printf(“\n number is odd”);
}
getch();
}
OUTPUT:
enter number:9
number is odd
//*PROGRAM TO PRINT FACTORIAL OF A NUMBER*//
#include <stdio.h>
#include <conio.h>
void main()
{
int i,f=1,n;
clrscr();
printf(“enter number”);
scanf(“%d”,&n);
for(i=n;i>=1;i--)
{
f=f*I;
printf(“%d/%d!+”,I,i);
}
i=1;
printf(“%d”,i);
printf(“\nFactorial is %d”,f);
getch();
}
OUTPUT:
rnter number3
3/3!+2/2!+1/1!+1
Factorial is 6
//*PROGRAM TO PRINT FIBONICCI SERIES*//
#include <stdio.h>
#include <conio.h>
void main()
{
int f,i,n;
int a-0,b=1;
clrscr();
printf(“enter any number”);
scanf(“%d”,&n);
f=a+b;
printf(“\n%d”,f);
for(i=0;i<=n;i++);
{
a=b;
b=f;
f=a+b;
ptintf(“|n%d”,f);
}getch();
}
Output:
enter any number5
1
2
3
5
8
13
21
.
.
.
//*PROGRAM TO PRINT PRIME NUMBERS BETWEEN 2 –
100 *//
#include <stdio.h>
#include <conio.h>
void main()
{
int n,d,f=0;
clrscr();
printf(“prime numbers between 2 – 100 are =\n”);
printf(“2”);
{
f=0;
for(d=2;d<n;d++)
{
if(n%d==0)
{
f=1;
break
}
}
if(f==0)
{
printf(“\t%d”,n);
}
}
getch();
}
OUTPUT:
prime numbers between 2-100 are=
2 3 5 7 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71
73 79 83 89 97
//*PROGRAM TO SWAP USING THIRD VARIABLE*//
#include <stdio.h>
#include <conio.h>
void main()
{
int a,b,c;
clrscr();
printf(“enter value of a”);
scanf(“%d”,&a);
printf(“enter value of b”);
scanf(“%d,&b);
printf(“a=%d,b=%d”,a,b);
ghetch();
}
OUTPUT:
enter value of a 22
enter value of b 44
a=22 b=44
a=44 b=22
//*PROGRAM TO SWAP WITHOUT USING THIRD
VARIABLE *//
#include <stdio.h>
#include <conio.h>
void main()
{
int a=10,b=20;
clrscr();
printf(“\n before swapping%d%d,a,b);
a=a+b;
b=a-b;
a=a-b;
printf(“\n after swapping%d5d”,a,b);
getch();
}
Output:
before swapping 1020
after swapping 2010
//*PROGRAM TO GENERATE A PYRAMID USING FOR
LOOP*//
#include <stdio.h>
#include <conio.h>
void main()
{
int a=1;
int i.j;
clrscr();
for(i=0;i<6;i+=)
{
for(j=0;j<=i;j++)
{
printf(“5d”,a);
}
a++;
printf(“\n”);
}
getch();
}
OUTPUT:
1
22
333
4444
55555
666666
//*PROGRAM TO PRINT STAR *//
#include <stdio.h>
#include <conio.h>
void main()
{
int I,j,k,blank=40,num;
clrscr();
printf(“enter the number of lines”);
scanf(“%d”,&num);
for(i=1;I,=num;i++)
{
for(i=1;I,=num;j--)
printf(“ “);
for(k=1;k,=(2*i)-1;k++)
printf(“ “),k);
blank=blank-1;
}
blank=blank+2;
for(i=num;i.=1;i--)
{
for(j=1;j,=blank;j++)
printf(“ “);
for(k=1;k,=(2*i)-1;k++)
printf(“*”,k);
printf(“\n”);
blank=blank+1;
}
getch();
}
OUTPUT:
enter the number of lines 3
*
***
*****
***
*
//*PROGRAM TO PRINT A DESIRED NUMBER *//
#include <stdio.h>
#include <conio.h>
void main()
{
int a,b=1;
int t;
clrscr();
printf(“enter any number”);
scanf(“%d”,&a);
while(b,=10)
{
printf(“\n %d*%d=%d”,a,b,t);
b++;
}
getch();
}
OUTPUT:
enter any number 2
2*1=2
2*2=4
2*3=6
2*4=8
2*5=10
2*6=12
2*7=14
2*8=16
2*9=18
2*10=20
//*PROGRAM TO FIND WHETHER THE NUMBER IS
PALINDROME OR NOT *//
#include <stdio.h>
#include <conio.h>
void main()
{
int n,u,p,r,s,t;
clrscr();
printf(“enter any number in 3 digits:”);
scanf(“%d”,&n);
u=n;
r=n%10;
n=n/10;
s=n%10;
n=n/10;
t=n%10;
p(r*100)+(s*10)+t;
if(p++u)
{
printf(“\n number is palindrome”);
}
else
{
printf(“\n number is not palindrome”)_;
}
getch();
}
OUTPUT:
enter the number in 3 digits
number is palindrome
//*PROGRAM TO FIND WHETHER THE NUMBER IS
PALINDROME OR NOT *//
#include <stdio.h>
#include <conio.h>
#include<string.h>
void main()
{
char str{20},str1[20];
int I,j=0,l,c=0;
clrscr();
printf(“enter string: “);
gets(str);
l=strlen(str);
for(i=l-1;i.=0;i--)
{
str1[j]=str[i];
j++;
}
str1[j]=’\0’;
for(“i=0;i,l;i++)
{
if(str[i]==str1[i])
c++;
}
if(c==l)
printf(“string is palindrome”);
else
printf(“string is not palindrome”);
getch();
}
OUTPUT:
enter string : mam
string is palindrome
WHILE: DO WHILE:
OUTPUT: OUTPUT:
1 1
2 2
3 3
4 4
5 5
//*PROGRAM TO SHOW USAGE OF CONTINUE
STATEMENT*//
#include <stdio.h>
#include <conio.h>
void main()
{
int i;
clrscr();
for(i=1;I,=5;i++)
{
printf(“\n thanks”);
}
getch():
}
OUTPUT:
1
2
3
4
5
//*PROGRAM TO SHOW USAGE OF “goto”
STATEMENT*//
#include <stdio.h>
#include <conio.h>
void main()
{
int a,b,add,sub;
clrscr();
printf(“enter values of a and b”);
scanf(“%d %d”,&a,&b);
add=a+b;
goto xy;
sub=a-b;
printf(“\n subtraction is=%d”,sub);
xy:
printf(“\n addition is =%d”,add);
getch();
}
OUTPUT:
Enter values of a and b 20 10
subtraction =10
Addition is =30
//*PROGRAM TO SHOW VARIOUS OPERATORS USING
SWITCH STATEMENT*//
#include <stdio.h>
#include <conio.h>
void main()
{
char op;
clrscr();
printf(“enter any operator”);
scanf(“%c”,&op);
switch(op)
{
case’+’:
printf(“addition operator”);
break;
case’-‘:
printf(“subtraction operator”);
break;
case’*’:
printf(“multiplication operator”);
break;
case’/’:
printf(“division operator”);
break;
case(%’:
printf(“modulus operator”);
break;
default:
printf(“entered character is not an operator”);
break;
}
getch();
}
OUTPUT:
Enter any operator -
Subtraction operator
//*PROGRAM TO PRINT SUM OF TWO NUMBERS USING
FUNCTION CALL BY VALUE CONCEPT*//
#include <stdio.h>
#include <conio.h>
int sum(intx,inty);
void main()
{
clrscr();
int x,y,s;
printf(“enter value of x & y\n”);
scanf(“%d%d”,&x,&y);
s=sum(x,y);
printf(“sum is %d”,s);
getch();
}
int sum(int x,int y)
{
int z;
z+x+y;
returnz;
}
OUTPUT:
Enter value of x & y
56
sum is 11
//*PROGRAM TO SWAP TWO NUMBERS USING
FUNCTION CALLL BY REFERENCE CONCEPT*//
#include <stdio.h>
#include <conio.h>
void main()
{
void swap(int*a,int*b);
int a,b;
clrscr();
OUTPUT:
Enter any two numbers 10 20
20 10
//*PROGRAM WITH ARGUMENT TYPE AND RETURN
CONCEPT IN FUNCTIONS *//
#include <stdio.h>
#include <conio.h>
void main()
{
int sum(int,int);
int num1,num2,res;
clrscr();
printf(“\nenter first and second number”);
scanf(“%d\n%d”,,&num1,&num2);
res=sum(num1,num2);
printf(“\nsum is =%d”,res);
getch();
}
int sum(int x,int y)
{
int s;
s=x+y;
return(s);
}
OUTPUT:
Enter first and second number
10
40
sum is =50
//*PROGRAM WITH NO RETURN ARGUMENT BUT WITH
RETURN CONCEPT IN FUNCTIONS *//
#include <stdio.h>
#include <conio.h>
void main()
{
int sum();
int res;
clrscr();
res=sum();
printf(“\nsum is =%d”,res);
getch();
}
int sum()
{
int num1,num2,s;
printf(“\nenter first and second number”);
scanf(“%d\n%d”,&num1,&num2);
s=num1+num2
return(s);
}
OUTPUT:
Enter first and second number
30
10
sum is =40
//*PROGRAM WITH ARGUMENT AND NO RETURN
CONCEPT IN FUNCTIONS *//
#include <stdio.h>
#include <conio.h>
void main()
{
int sum(int,int);
int num1,num2,;
clrscr();
printf(“\nenter first and second number”);
scanf(“%d\n%d”,,&num1,&num2);
sum(num1,num2);
printf(“\nsum is =%d”,res);
getch();
}
void sum(int x,int y)
{
int res;
res=x+y;
printf(“\nsum is=%d”,res);
}
OUTPUT:
Enter first and second number
10
10
sum is =20
//*PROGRAM WITH NO ARGUMENT AND NO RETURN
CONCEPT IN FUNCTIONS *//
include <stdio.h>
#include <conio.h>
void main()
{
void sum();
clrscr();
sum();
getch();
}
void sum()
{
int num1,num2,res;
OUTPUT:
Enter first and second number
40
60
sum is =100
//*PROGRAM TO FIND FACTORIAL USING RECURSIVE
FUNCTION *//
#include<stdio.h>
#include<conio.h>
int rec (int);
void main ()
{
int a,fact;
printf(“\n enter any number”);
scanf(“%d”,&a);
fact=rec(a);
printf(“factorial value=%d”,fact);
}
int rec(int x)
{
int f;
if(x==1)
return(1);
else
f=x*rec(x-1);
return (f);
}
OUTPUT:
Enter any number 5
Factorial value=120
//* PROGRAM TO SHOW HOW POINTER TO POINTER
OPERATES*//
##include<stdio.h>
#include<conio.h>
void main()
{
int a. *p1, **p2
clrscr();
a=20;
p1=&a;
p2=&p1;
printf(“\n Value of A stored at address = %d”,p1);
printf(“\n Address of p1 is stored at address= %d”,p2);
printf(“\n Value of A using pointer to pointer is = %d”,**p2);
getch();
}
OUTPUT :
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr( );
int a[20], num , i ;
printf(“enter the number of elements”);
scanf(“%d”, &num);
printf(“enter the elements of the array”);
for(i=0 ; i< num; i ++)
{
scanf(“%d”,&a[i]);
}
array(&a[0],num);
getch( );
}
void array(int*j, int n)
{
int i ;
printf(“ the elements are as follows”);
for(i=0; i, n, i ++)
{
printf(‘\n%d”,*j);
j= j +1;
}
}
OUTPUT :
#include<stdio.h>
#include<conio.h>
void main( )
{
int arr[5];
int p, i , val;
clrscr( );
printf(“\n enter values”);
for(i=0; i,4, i ++)
{
scanf(“%d”,&arr[i]);
}
printf(“\n enter position:”);
scanf(“%d”,&p);
for(i=4; i>p-1; 1- -)
{
arr[i] = arr[i-1];
}
printf(“\n enter missing value:”);
scanf(“%d”,&val);
arr[p-1]=val;
for(i=0; i<=4, i ++
{
printf(“\n%d,arr[i]);
}
getch( ) ;
}
OUTPUT:
enter values
10 20 40 50
enter position: 3
enter missing value : 30
10 20 30 40 50
//*PROGRAM TO DELETE AN ELEMENT FROM AN
ARRAY*//
#include<stdio.h>
#include<conio.h>
void main ( )
{
int arr[10];
int i , n , temp , p;
clrscr( ) ;
printf(“\n enter number of elements:”);
scanf(“%d”,&n);
printf(“\n enter values”);
for(i=0; i < =n, i ++)
{
scanf(“%d”,&arr[i]);
}
printf(“\n enter position :”);
scanf(“%d”,&p);
temp = arr[p-1];
for9i=p-1; i < n; i++)
{
arr[i]= arr[i+1];
}
for(i=0; i<n; i++)
{
printf(“\n%d”,arr[i]);
}
printf(“\n deleted itm is = %d”,temp);
getch( );
}
OUTPUT:
enter number of elements:4
enter values 10 22 20 30
enter position: 2
10 20 30
deleted item is = 22
//* PROGRAM TO FIND THE BIGGEST ELEMENT IN AN
ARRAY*//
#include<stdio.h>
#include<conio.h>
void main( )
{
int a[10];
int i, n , big;
clrscr( ) ;
printf(“\n enter the number of elements:”);
scanf(“%d”,&n);
printf(“\n enter the elements”);
for(i=0; i<n; i ++)
{
scanf(“%d”,&a[i]);
}
big= a[0];
for(i=1; i <n,i ++)
{
if(a[i]>big)
{
big=a[i];
}
}
printf(“\n the biggest number is = %d”,big);
getch( ) ;
}
OUTPUT:
#include<stdio.h>
#include<conio.h>
void main( )
{
int a[10];
int i, n , sm;
clrscr( ) ;
printf(“\n enter the number of elements:”);
scanf(“%d”,&n);
printf(“\n enter the elements”);
for(i=0; i<n; i ++)
{
scanf(“%d”,&a[i]);
}
sm= a[0];
for(i=n-1; i >=0; i- -)
{
if(a[i]<sm)
{
sm=a[i];
}
}
printf(“\n the smallest number is = %d”,sm);
getch( ) ;
}
OUTPUT:
#include<stdio.h>
#include<conio.h>
void main( )
{
int i, j ,r , c;
int x[10], y[10], z[10];
clrscr( );
printf(“\n enter the number of rows and columns for matrices”);
scanf(“%d%d”, &r,&c);
printf(“\n enter the elements for first matrix”);
for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
scanf(“%d”,&x[i][j]);
}
}
printf(“\n enter the elements for second matrix”);
for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
scanf(“%d”,&y[i][j]);
}
}
for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
z[i][j]= x[i][j] + y[i][j];
}
}
printf(“matrix after addition is”);
printf(“\n”);
for(i=0;i<r;i++)
{
for(j=0; j<c; j++)
{
printf(“%d”,z[i][j]);
printf(“\n”);
}
getch( );
}
OUTPUT:
#include<stdio.h>
#include<conio.h>
void main( )
{
int i, j ,r , c;
int x[10], y[10], z[10];
clrscr( );
printf(“\n enter the number of rows and columns for matrices”);
scanf(“%d%d”, &r,&c);
printf(“\n enter the elements for first matrix”);
for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
scanf(“%d”,&x[i][j]);
}
}
printf(“\n enter the elements for second matrix”);
for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
scanf(“%d”,&y[i][j]);
}
}
for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
z[i][j]= x[i][j] - y[i][j];
}
}
printf(“matrix after subtraction is”);
printf(“\n”);
for(i=0;i<r;i++)
{
for(j=0; j<c; j++)
{
printf(“%d”,z[i][j]);
printf(“\n”);
}
getch( );
}
OUTPUT:
#include<stdio.h>
#include<conio.h>
void main( )
{
int i, j ,k,r1, r2 , c1, c2;
int x[10], y[10], z[10];
clrscr( );
printf(“\n enter the number of rows and columns for first
matrix”);
scanf(“%d%d”, &r1,&c1);
printf(“\n enter the number of rows and columns for second
matrices”);
scanf(“%d%d”, &r2,&c2);
if(c1!=r2)
{
printf(“\n multiplication is not possible”);
goto ab;
}
printf(“\n enter the elements for first matrix”);
for(i=0; i<r1; i++)
{
for(j=0; j<c1; j++)
{
scanf(“%d”,&x[i][j]);
}
}
printf(“\n enter the elements for second matrix”);
for(i=0; i<r2; i++)
{
for(j=0; j<c2; j++)
{
scanf(“%d”,&y[i][j]);
}
}
for(i=0; i<r1; i++)
{
for(j=0; j<c2; j++)
{
z[i][j]=0;
for(k=0; k<r2;k++)
{
z[i][j]= z[i][j]+ x[i][k] *z[k][j]
}
}
}
printf(“matrix after multiplication is”);
for(i=0; i<r1; i++)
{
printf(\n”);
for(j=0; j<c2; j++)
{
printf(“%d”,z[i][j]);
}
printf(“\n”);
}
ab
getch( );
}
OUTPUT:
int arr[10];
int j, i ,n, val;
clrscr( );
printf(“\n enter the number of elements:”);
scanf(“%d”,&n);
printf(“\n enter va;ues”);
for(i=0; i<n; 1++)
{
scanf(“%d”,&arr[i]);
}
for(i=0; i<n; 1++)
{
for(j=0; j<n-i-1; j++)
{
if(arr[j]>arr[j+1])
{
val = arr[j];
arr[j] = arr[j+1];
arr[j+1]= val;
}
}
}
printf(“\values after sorting is”);
for(i=0; i<=n, i ++)
{
printf(“\n%d,arr[i]);
}
getch( ) ;
}
OUTPUT:
OUTPUT:
Element =24
Element =34
Element =44
Element =54
Element =64
Element =74
//* PROGRAM TO FIND THE LENGTH OF A STRING
WITHOUT USING LIBRARY FUNCTIONS*//
#include<stdio.h>
#include<conio.h>
void main( )
{
char str[20];
int i ;
clrscr( );
printf(“\n enter a string:”);
scanf(“%s”str);
for(i=0; str[i]!=’\0’, i++)
{
printf(‘\n the length is : %d”,i);
}
getch();
}
OUTPUT:
enter a string: computer
the length is: 8
//* PROGRAM TO SHOW HOW STUDENT DATA IS
ENTERED AND DISPLAYED USING STRUCTURE*//
#include<stdio.h>
#include<conio.h>
struct student
{
char name[10];
int rollno;
float marks;
};
void main( )
{
struct student s[3];
int i;
clrscr( );
for(i=0; i <=2; i++)
{
printf(“\n enter rollno, name and marks of student”);
scanf(“%d %s %f”, &s[i].rollno, &s[i].name, &s[i].marks);
}
printf(“\n entered data is displayed”);
print(“\n”);
for(i=0; i <=2; i++)
{
printf(“\n rollno is : %d\n name is: %s \n marks : %f”, s[i].rollno ,
s[i].name, s[i].marks);
printf(“\n”);
}
getch( );
}
OUTPUT:
rollno is :1
name is: pooja
marks: 80
rollno is: 2
name is: deepak
marks: 78
rollno is :3
name is: shivani
marks: 70
//* PROGRAM TO SHOW NESTED STRUCTURES*//
#include<stdio.h>
#include<conio.h>
void main( )
{
struct address
{
char city[25];
int pin;
};
struct emp
{
char name[25];
struct address a;
};
struct emp e ={“Michael”, “Chandigarh”, 13};
printf(“\n name = %s”,e.name);
printf(“\n city = %s pin= %d”, e.a.city, e.a.pin);
getch( );
}
OUTPUT:
name = Michael
city = Chandigarh pin=13
//* PROGRAM TO SHOW THE USAGE OF ARRAY OF
STRUCTURES*//
#include<stdio.h>
#include<conio.h>
struct student
{
char name[10];
int rollno;
};
void main( )
{
struct student s[3];
int i;
clrscr( );
for(i=0; i <=2; i++)
{
printf(“\n enter rollno and name student:”);
scanf(“%d %s”, &s[i].rollno, &s[i].name);
}
printf(“\n entered data is displayed”);
print(\n”);
for(i=0; i <=2; i++)
{
printf(“\n rollno is : %d \n name is: %s”, s[i].rollno , s[i].name,);
printf(“\n”);
}
getch( );
}
OUTPUT:
rollno is: 2
name is : deepak
rollno is: 3
name is : shivani
//* PROGRAM TO SHOW HOW STUDENT DATA IS
ENTERED AND DISPLAYED USING UNION*//
#include<stdio.h>
#include<conio.h>
union student
{
char name[10];
int rollno;
float marks;
};
void main( )
{
union student s[3];
int i;
clrscr( );
for(i=0; i <=2; i++)
{
printf(“\n enter rollno, name and marks of student”);
scanf(“%d %s %f”, &s[i].rollno, &s[i].name, &s[i].marks);
}
printf(“\n entered data is displayed”);
print(“\n”);
for(i=0; i <=2; i++)
{
printf(“\n rollno is : %d\n name is: %s \n marks : %f”, s[i].rollno ,
s[i].name, s[i].marks);
printf(“\n”);
}
getch( );
}
OUTPUT:
rollno is :1
name is: pooja
marks: 80
rollno is: 2
name is: deepak
marks: 78
rollno is :3
name is: shivani
marks: 70
//* PROGRAM TO SHOW POINTER ARITHEMETIC*//
#include<stdio.h>
#include<conio.h>
void main( )
{
int *p, a=5;
char +ptr, b= ‘a’;
clrscr( );
p=&a;
ptr=&b
printf(“pointer arithemetic for int datatype\n”);
printf(“the value of a is = %d\n”,*p);
printf(the address of value is = %d\n”,p);
p= p+1;
printf(“the updated value is%d\n”,p);
p=p-2;
printf(“the updated value is%d\n”,p);
printf(“pointer arithemetic for char datatype\n”);
printf(“the value of a is = %c\n”,*ptr);
printf(the address of value is = %d\n”,ptr);
ptr= ptr+1;
printf(“the updated value is%d\n”,ptr);
ptr=ptr-2;
printf(“the updated value is%d\n”,ptr);
getch( );
}
OUTPUT:
pointer arithemetic for int datatype
the value of a is =5
the address of value is = -12
the updated value is -10
the updated value is -14
pointer arithemetic for char datatype
the value of a is =a
the address of value is = -13
the updated value is -12
the updated value is -14
INDEX
Sr. No. Program name Signature
1 Program to display name