Linear Search of A Number: Program Coding
Linear Search of A Number: Program Coding
Linear Search of A Number: Program Coding
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]$
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:
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:
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:
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:
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: