Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 12

List of Experiments (list of experiments given by G.G.S.I.P.

U)
 
1. Write a program to produce ASCII equivalent of given number

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

void main()
{
char a;
int b;
clrscr();
printf(" \n \n enter the number whose ASCII value is to be determined : ");
scanf("%d",&b);
a=b;
printf(" \n \n the ASCII value of the number entered %d is : %c",b,a);
getch();
}

2. Write a program to find divisor or factorial of a given number.

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

main()
{

int a,n,b=1,i,fac;
printf("\n \n \t Enter the number whose factorial is to be found :");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
a=i;
fac=a*b;
b=fac;
}
printf("\n \n the factorial of %d is : %d",n,fac);
getch();
return 0;
}

5. Write a program to cipher a string


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char str[40],ch;
int a;
printf("\n \n Enter the string to be encrypted : ");
gets(str);

for(int i=0;i<strlen(str);i++)
{
str[i]=str[i]+12;
}
printf("\n \n The encrypted string is : ");
puts(str);

for(i=0;i<strlen(str);i++)
{
str[i]=str[i]-12;
}
printf("\n \n The decrypted string is : ");
puts(str);
getch();
}

6. Write a program to check whether a given string follows English capitalization rules
7. Write a program to find sum of the following series
1+ ½ + 1/3 +________+1/20
#include<stdio.h>
#include<conio.h>
main ()
{
float sum=1.0;
for(int i=1;i<=20;i++)
sum= sum + (1.0/i);
printf("\n the sum of the series is : %f ",sum);
getch();
return 0;
}
 
12. Write a program that takes two operands and one operator from the user perform the
operation and then print the answer
#include<stdio.h>
#include<conio.h>

void main()
{
clrscr();
int a,b,c;
char ch;
printf("\n \n Enter the operator : ");
scanf("%c",&ch);
printf("\n \n Enter the operands a & b : ");
scanf("%d %d",&a,&b);
switch(ch)
{
case '+': c=a+b;
printf("\n \n a+b = %d",c);
break;
case '-': c=a-b;
printf("\n \n a-b = %d",c);
break;
case '*': c=a*b;
printf("\n \n a*b = %d",c);
break;
case '/': c=a/b;
printf("\n \n a/b = %d",c);
break;
case '%': c=a%b;
printf("\n \n a%b = %d",c);
break;
default : printf(" \n \n wrong choice ");
}
getch(); }
13. Write a program to print the following outputs:
  1 1
2 2 2 2
3 3 3 3 3 3
4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5 5

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

main()
{

for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
printf(" %d ",i);
}
printf("\n");
}
getch();
return 0;
}
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

14. Write functions to add, subtract, multiply and divide two complex numbers (x+iy) and
(a+ib) Also write the main program.
#include<stdio.h>
#include<conio.h>
#include<math.h>

int sumr(int x,int y)


{
int c;
c=x+y;
return c;
}
int sumi(int x,int y)
{
int d;
d=x+y;
return d;
}
int sub(int x,int y)
{
int d;
d=x-y;
return d;
}
int subi(int x,int y)
{
int d;
d=x-y;
return d;
}
void main()
{
clrscr();
int a,b,c,d,x,y,n;
printf("\n \n enter the two real part of the complex numbers : ");
scanf("%d %d",&a,&c);
printf("\n \n enter the two imaginary part of the complex numbers : ");
scanf("%d %d",&b,&d);
printf("\n \n The first complex number entered is %d + i%d ",a,b);
printf("\n \n The second complex number entered is %d + i%d ",c,d);
printf("\n \n Enter the choice1. ADDITION \n \n \t \t 2. SUBTRACTION \n \n \t \t 3.
MULTIPLICATION \n \n\t \t 4. ADDITION \n \n");
scanf("%d",&n);
switch(n)
{
case 1:
x=sumr(a,c);
y=sumi(b,d);
printf("\n \n The sum of the complex numbers is : %d + i%d ",x,y);
break;
case 2:
x=sub(a,c);
y=subi(b,d);
printf("\n \n The difference of the complex numbers is : %d + i(%d) ",x,y);
break;
default : printf("\n \n wrong choice ");
}
getch();
}

/// OBJECTIVE :
WRITE A MENU DRIVEN PROGRAM FOR SEARCHING AN SORTING WITH
FOLLOWING OPTION -
/// A) SEARCHING
/// 1) LINEAR SEARCHING 2) BINARY SEARCHING
/// B) SORTING
/// 1) INSERTION SORTING 2) SELECTION SORTING
///
PROGRAM:

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

void main()

int n,ch,i,a[10],t,k,j,temp,c,pos=-1,b,end,beg,mid;
clrscr();

/// TAKING THE INPUT FOR NUMBER OF ELEMENTS TO BE SORTED


OR SEARCHED ///

printf("\n Enter the number of elements :");


scanf("%d",&n);

/// TAKING THE INPUT FOR VALUES FOR ELEMENTS ///

printf("\n\n Enter the numbers but for binary searching enter the numbers
in ascending order");
for(i=0;i<=n-1;i++)
scanf("%d",&a[i]);

/// TAKING THE INPUT FOR CHOICE OF USER ///

printf("\t\n Enter the choice:\n\t1.SORTING\n\t2.SEARCHING");


scanf("%d",&c);
switch(c)
{

case 1: /// CASE FOR SORTING THE ELEMENTS ///

printf("\n\tEnter the choice for sorting:\n\t1.SELECTION


SORT\n\t2.INSERTION SORT: ");
scanf("%d",&ch);

switch(ch)
{
case 1: /// CASE FOR SELECTION SORT ///
printf("\n\n******SELECTION SORT******");
for(i=0;i<=(n-2);i++)
{
for(int j=i+1;j<=n-1;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}

/// PRINTING THE SORTED LIST ///

printf("\n\n Sorted numbers are:\n");


for(i=0;i<=n-1;i++)
printf("%d\t",a[i]);

break;

case 2: /// CASE FOR INSERTION SORT ///

printf("\n\n********Insertion sort********");
for(i=1;i<=n-1;i++)
{
t=a[i];
for(j=0;j<i;j++)
{
if(t<a[j])
{
for(k=i;k>=j;k--)
a[k]=a[k-1];
a[j]=t;
break;
}
}
}

/// PRINTING THE SORTED LIST ///

printf("\n \n Sorted numbers are:\n");


for(i=0;i<=n-1;i++)
printf("%d\t",a[i]);

break;

default:printf("\n U HAVE ENTERED A WRONG CHOICE ");

case 2: //// CASE FOR SEARCHING THE ELEMENT FROM THE LIST ///

printf("\n\n Enter the choice for searching:\n\t1.linear searching\n\t2.binary


searching: ");
scanf("%d",&ch);

switch(ch)
{
case 1: /// CASE FOR LINEAR SEARCH ///

printf("\n***********LINEAR
SERCHING******************");

/// TAKING THE INPUT FOR NUMBER TO BE SEARCH ///

printf("\n\n enter the number to be searched:");


scanf("%d",&b);

/// SEARCHING THE LIST ///

for(i=0;i<=n-1;i++)
{
if(b==a[i])
{
pos=i;
break;
}
}

/// PRINTING THE RESULT///

if(pos>=0)
printf("\n\n %d is found in position %d",b,pos+1);
else
printf("\n\n number is not there");

break;

case 2: /// CASE FOR BINARY SEARCH ///

printf("\n\n**************binary searching***********");

/// TAKING THE INPUT FOR NUMBER TO BE SEARCH ///

printf("\n\n enter the number to be searched: ");


scanf("%d",&b);

/// SEARCHING THE LIST ///

beg=0;
end=n-1;
mid=(beg+end)/2;
while(beg<=end&&b!=a[mid])
{
if(b>a[mid])
beg=mid+1;
else
end=mid-1;
mid=(beg+end)/2;
}

/// PRINTING THE RESULT ///

if(a[mid]==b)
printf("\n\n%d is found in position: %d",b,mid+1);
else
printf("\n the number is not there");

break;

default:
printf("\n u have entered the wrong choice");
}
getche();

}
}

//////////////OUTPUT//////////////////

//// Enter the choice:


/// 1.SORTING
/// 2.SEARCHING 1

/// Enter the choice for sorting:


/// 1.SELECTION SORT
/// 2.INSERTION SORT: 2

///********Insertion sort********

/// Sorted numbers are:


///0 1 2 3 5 7 9

/// Enter the choice for searching:


/// 1.linear searching
/// 2.binary searching: 2

///**************binary searching***********

/// enter the number to be searched: 7

///7 is found in position: 6


//////Write a recursive program for tower of Hanoi problem//////

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

void transfer(int n,char from,char to,char temp);

main()
{
int n;
clrscr();
printf("\n Enter the number of disks:");
scanf("%d",&n);
transfer(n,'L','R','C');
getch();
}

void transfer(int n,char from,char to,char temp)


{
if(n>0)
{
transfer(n-1,from,temp,to);
printf("\ Move disk %d from %c to %c\n",n,from,to);
transfer(n-1,temp,to,from);
}
return;
}

///////OUTPUT///////

/// Enter the number of disks:4


/// Move disk 1 from L to C
/// Move disk 2 from L to R
/// Move disk 1 from C to R
/// Move disk 3 from L to C
/// Move disk 1 from R to L
/// Move disk 2 from R to C
/// Move disk 1 from L to C
/// Move disk 4 from L to R
/// Move disk 1 from C to R
/// Move disk 2 from C to L
/// Move disk 1 from R to L
/// Move disk 3 from C to R
/// Move disk 1 from L to C
/// Move disk 2 from L to R
/// Move disk 1 from C to R
//////////////////////////////////////////////////////////////
/// Write a program to copy one file to other, use command
/// line arguments.
/// STEP 1: DEFINE THE main FUNCTION WITH REQUIRED NUMBERS OF
ARGUMENTS.
/// STEP 2: OPEN THE SOURCE FILE AND TAGRET FILE.
/// STEP 3: COPY THE DATA OF SOURCE FILE TO TARGET FILE CHARACTER
BY CHARACTER.
///

/// PROGRAM :

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

void main(int argc,char *argv[])


{
FILE *fs,*ft;
char ch;
clrscr();

if(argc!=3)
{
puts("insufficient arguments");
}

fs = fopen(argv[1],"r");

if(fs==NULL)
{
puts("cannot open the source file");
}
ft = fopen(argv[2],"w");

if(ft==NULL)
{
puts("cannot open target file");
fclose(fs);
}

while(1)
{
ch=fgetc(fs);

if(ch==EOF)
break;
else
fputc(ch,ft);
}

fclose(fs);
fclose(ft);
getch();

You might also like