Linear Search of A Number: Program Coding

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

LINEAR SEARCH OF A NUMBER

PROGRAM CODING:
#include<stdio.h>
void linear(int [],int,int);
main()
{
int i,n,x,a[20];
printf("\n Enter the limit:");
scanf("%d",&n);
printf("\n enter the elements:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n enter the elements to be searched:");
scanf("%d",&x);
linear(a,n,x);
printf("\n");
}
void linear(int a[],int n,int x)
{
int i,flag=0;
for(i=0;i<n;i++)
{
if(x==a[i])
{
flag=1;
break;
}
}
if(flag==1)
printf("\n the element %d is in the position %d",a[i],i+1);
else
printf("\n the element %d is not in the list\n ",a[i]);
}

~
~
~
~
"linear.c" 35L, 554C 1,1 All
OUTPUT:

[prince01@prince subha]$ cc linear.c


[prince01@prince subha]$ ./a.out

Enter the limit:5

enter the elements:22


55
12.

enter the elements to be searched:


the element is not in the list

[prince01@prince subha]$ cc linear.c


[prince01@prince subha]$ ./a.out

Enter the limit:5

enter the elements:11


22
33
44
55

enter the elements to be searched:22

the element 22 is in the position 2


[prince01@prince subha]$ cc linear.c
[prince01@prince subha]$ ./a.out

Enter the limit:5

enter the elements:1


2
3
4
5

enter the elements to be searched:6

the element 6 is not in the list

[prince01@prince subha]$
RECUSION-FACTORIAL OF A NUMBER

PROGRAM CODING:
#include<stdio.h>
long fact(int n);
main()
{
int num;
long fact1;
printf("\n Enter a number:");
scanf("%d",&num);
fact1=fact(num);
printf("Th factorial of a given number %d is %d",num,fact1);
printf("\n");
}
long fact(int n)
{
if(n==0 | n== 1)
return 1;
else
return(n*fact(n-1));
}

~
~
~
~
~
~
~
"fact.c" 19L, 284C 1,1 All

OUTPUT:

[prince01@prince subha]$ cc fact.c


[prince01@prince subha]$ ./a.out

Enter a number:5
Th factorial of a given number 5 is 120
[prince01@prince subha]$
POINTERS - SWAPPING OF TWO NUMBER

PROGRAM CODING:

#include<stdio.h>
void swap(int*,int*);
main()
{
int a,b,*p1,*p2;
p1=&a;
p2=&b;
printf("Enter the two :\n");
scanf("%d%d",p1,p2);
printf("Entered values are:a=%d,b=%d\n",a,b);
swap(p1,p2);
printf("values after interchange:a=%d,b=%d\n",a,b);
}
void swap(int*p,int *q)
{
int t;
t=*p;
*p=*q;
*q=t;
}
~
~
~
~
~
~
~
~
~
"swap.c" 20L, 297C written 10,46 All

OUTPUT:

[prince01@prince subha]$ cc swap.c


[prince01@prince subha]$ ./a.out
Enter the two :
12
Entered values are:a=1,b=2
values after interchange:a=2,b=1
[prince01@prince subha]$
COPY THE CONTENT OF ONE STRING TO ANOTHER

PROGRAM CODING:

#include<stdio.h>
#include<malloc.h>
#define length 30
main()
{
char *s1,*s2,c;
int i;
s1=(char*)malloc(length*sizeof(char));
s2=(char*)malloc(length*sizeof(char));
printf("\n Enter the string1:");
scanf("%s",s1);
//printf("\n the entered string is %s",s1);
i=0;
while((c=*(s1+i)) !='\0')
{
s2[i]=c;
i++;
}
s2[i]='\0';
printf("\nThe copied string1 in string2 is %s",s2);
printf("\n");
}
~
~
~
~
~
copy.c" 22L, 398C written

OUTPUT:

[prince01@prince subha]$ cc copy.c


[prince01@prince subha]$ ./a.out

Enter the string1:education

The copied string1 in string2 is education


[prince01@prince subha]$
IMPLEMENTATION OF CP COMMAND

PROGRAM CODING:

#include<stdio.h>
#include<stdlib.h>
main()
{
FILE *f1,*f2;
char c;
if(!(f1=fopen("input.txt","r")))
printf("cannot open the file \n");
else
{
f2=fopen("output.txt","w");
while((c=fgetc(f1))!=EOF)
{
fputc(c,f2);
}
fclose(f1);
fclose(f2);
}
}
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"cp.c" 19L, 270C written
OUTPUT:

[prince01@prince subha]$ cc cp.c


[prince01@prince subha]$ ./a.out
cannot open the file
[prince01@prince subha]$ cat>input.txt
rose
jasmine
lily
lotus
[prince01@prince subha]$ cat>output.txt
hibiscus
[prince01@prince subha]$ cc cp.c
[prince01@prince subha]$ ./a.out
[prince01@prince subha]$ cat output.txt
rose
jasmine
lily
lotus
[prince01@prince subha]$
IMPLEMENTATION OF CAT COMMAND

PROGRAM CODING:

#include<stdio.h>
#include<stdlib.h>
main()
{
FILE *f1;
char c;
if(!(f1=fopen("file.txt","r")))
printf("cannot open the file \n");
else
{
while((c=fgetc(f1))!=EOF)
{
printf("%c",c);
}
fclose(f1);

}
}
~
~
~
~
"cat1.c" 18L, 225C written

OUTPUT:

[prince01@prince subha]$ cc cat1.c


[prince01@prince subha]$ ./a.out
cannot open the file
[prince01@prince subha]$ cat>file.txt
table
pen
pencil
eraser
[prince01@prince subha]$ cc cat1.c
[prince01@prince subha]$ ./a.out
table
pen
pencil
eraser
[prince01@prince subha]$

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy