Thursday, December 9, 2010

Reverse an integer by using Recursion

#include<stdio.h>
#include<conio.h>
int re (int,int,int*,int);
void main()
{
clrscr();
int i,j=0,n[5],k=0,m;
printf("Enter a number: ");
scanf("%d",&i);
m=re(i,j,n,k);
for(j=0;j<m;j++)
printf("%d",n[j]);
getch();
}
re(int i,int j,int *n,int k)
{
if(i==0) return k;
j=i%10;
n[k]=j;
i=i/10;
re(i,j,n,k+1);
program written by arnob;
Writer a C program that stores the student's name, identity and GPA. and do a serach possibility that when we give a name to search if the name is so show that the student's name, identity and GPA. If the name is not there then shows NOT FOUND



#include<stdio.h>
#include<conio.h>
#include<string.h>
struct student
{
char name[40];
char id[20];
char gpa[5];
}stu[100];
void main()
{
clrscr();
int i=0,j,n;
char name[40];
printf("How many students informatio: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the name: ");
scanf("%s",stu[i].name);
printf("\nEnter the id: ");
scanf("%s",stu[i].id);
printf("\nEnter the gpa: ");
scanf("%s",stu[i].gpa);
clrscr();
}
printf("\nEnter the search name: ");
scanf("%s",name);
for(i=0;i<n;i++)
{
clrscr();
j=strcmp(name,stu[i].name);
if(j==0)
{
printf("\nName :");
puts(stu[i].name);
printf("\nId :");
puts(stu[i].id);
printf("\nGPA :");
puts(stu[i].gpa);
break;
}
}
if(j!=0)
printf("Not found !");
getch();
}

program written by arnob;

Friday, December 3, 2010

47. Find the hight and second hight number by using RECURSION

/* Find the hight number by using recursion */

#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
int n,a[100],i,j;
printf("How many nmber you want to input: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the %d number",i+1);
scanf("%d",&a[i]);
}
int hight(int,int,int,int*);
j=hight(n,0,0,a);
printf("The hight number is %d",j);
return 0;
}

int hight(int n,int i,int m,int *a)
{
if(i>n) return m;
if(m<a[i])
m=a[i];
hight(n,i+1,m,a);
}


/* Find the second hight number by using recursion */

#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
int n,a[100],i;
printf("How many nmber you want to input: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the %d number",i+1);
scanf("%d",&a[i]);
}
int hight(int,int,int,int,int*);
hight(n,0,0,0,a);
printf("The second hight number is %d",a[1]);
return 0;
}

int hight(int n,int i,int j,int k,int *a)
{
if(i>n-1) return 0;

for(j=i+1;j<n;j++)
if(a[i]<a[j])
{
k=a[i];
a[i]=a[j];
a[j]=k;
}
hight(n,i+1,j,k,a);
}


written by anrob.

46. Find the even number from a array by using RECURSION

Question: Get input a array frmon user and find the hight number. You can only use one array and can not use any globel variable.

Ans: If you can use more then one array then you can easyle find the hight number by only using for loop. But you can not use more then one array. So you can solve this problem by using recursion.

/*program for find the even number */

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

int main()
{
clrscr();
int a[100];n,i;
printf("How many number you want to input: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the %d number: ");
scanf("%d",&a[i]);
}
void chk_evn(int,int,int*,int*)
chk_evn(0,0,&n,a);
for(i=0;i<n;i++)
printf("%d",a[i]);
return 0;
}

void chk_evn (int i,int j,int *n,int *a)
{
if(i==n){*n=j;return n;}
if(a[i]%2==0)
a[j++]=a[i];
chk_evn(i+1;j,n,a);
}

program written by arnob