Thursday, 13 December 2012

Write a MENU DRIVEN program using function call (CALL BY REFERENCE) for the following : 1. FACTORIAL 2. ODD OR EVEN 3. PRIME OR NOT

*NOTE : 1. MENU DRIVEN REFERS TO USE OF SWITCH-CASE STATEMENT
                2. CALL BY REFERENCE IMPLIES PASSING THE ADDRESS OF THE VARIABLE DURING FUNCTION CALL



PROGRAM :


#include <conio.h>
#include <stdio.h>
int fact(int *);
int oden(int *);
int prime(int *);
void main()
{
int n,c;
int *b;
b=&n;
clrscr();
choice:
printf("\nENTER : \n\n 1 FOR FACTORIAL OF A NUMBER 2 TO CHECK ODD OR EVEN  3 TO CHECK PRIME OR NOT\n\n ENTER CHOICE : ");
scanf("%d",&c);
if(c>=1 && c<=3)
{
printf("\nEnter Number : ");
scanf("%d",&n);
}
switch(c)
{
case 1:
fact(b);
break;
case 2:
oden(b);
break;
case 3:
prime(b);
break;
default:
printf("\nInvalid Input");
}
printf("\n\nRun more processes ? (Enter 1 for YES, 0 for NO) : ");
scanf("%d",&c);
if(c==1)
goto choice;
printf("END");
getch();
}
int fact(int *c)
{
int a=*c;
long s=1;
int i;
for(i=1;i<=a;i++)
s=s*i;
printf("Factorial of number = %u",s);
return(0);
}
int oden(int *c)
{
int a=*c;
if(a%2==0)
printf("Number is Even");
else
printf("Number is Odd");
return(0);
}
int prime(int *d)
{
int a=*d;
int i,c=0;
for(i=1;i<=a;i++)
if(a%i==0)
c++;
if(c==2)
printf("Number is Prime");
else
printf("Number is Not prime");
return(0);
}



OUTPUT
 

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();
}