Saturday, 12 May 2012

Frequency of a character in a disk file

TESTF.txt   is  the  name  of  the  file. it contains the following text : 
             this is the file from which the character will be searched         




PROGRAM




#include <stdio.h>
#include <conio.h>
void main()
{
FILE *p;
char c,d;
int c1=0;
clrscr();
p=fopen("TESTF.txt","r");
printf("Given file = ");
while((c=getc(p))!=EOF)
{
printf("%c",c);
}
rewind(p);
printf("\nEnter the character to be searched : ");
scanf("%c",&d);


while((c=getc(p))!=EOF)
{
if(c==d)
c1++;
}
printf("\nFrequency of %c = %d",d,c1);
fclose(p);
getch();
}



Recursive Function for GCD

#include <stdio.h>
#include <conio.h>
int gcd(int);
int a,b;
void main()
{
int s,g;
clrscr();
printf("Enter 2 NUmbers : ");
scanf("%d%d",&a,&b);
s=(a>b)?b:a;
g=gcd(s);
printf("GCD = %d",g);
getch();
}
int gcd(int n)
{
if(a%n==0 && b%n==0)
return(n);
else
gcd(n-1);
}

Recursive Function for factorial of number


#include <stdio.h>
#include <conio.h>
int fact(int);
int f=1;
void main()
{
int f,n;
clrscr();
printf("Enter Number : ");
scanf("%d",&n);
f=fact(n);
printf("Factorial = %d",f);
getch();
}
int fact(int n)
{
if(n==1)
return(1);
else
{
f=n*fact(n-1);
return(f);
}
}







Friday, 11 May 2012

Roots of Quadratic Equation


#include <stdio.h>
#include <conio.h>
#include <math.h>
void quad(int,int,int);
void main()
{
int a,b,c;
clrscr();
printf("Enter Values Of a, b & c : ");
scanf("%d%d%d",&a,&b,&c);
quad(a,b,c);
}
void quad(int a,int b,int c)
{
float d,e,f;
f=sqrt((b*b)-(4*a*c));
d=(((-1)*b)+f)/(float)(2*a);
e=(((-1)*b)-f)/(float)(2*a);
printf("Roots of euaation = %.2f AND  %.2f",d,e);
getch();
}

Largest from an array


#include <stdio.h>
#include <conio.h>
void main()
{
int n,i,l,a[50];
clrscr();
printf("Number of elements : ");
scanf("%d",&n);
printf("Enter Elements : ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
l=a[0];
for(i=0;i<n;i++)
{
if(a[i]>l)
l=a[i];
}
printf("Largest Number : %d",l);
getch();
}