To Be Familiarize With Recursive Function and One Dimensional Array
- WAP to print "Sagarmatha Engineering College" ten times using recursive function.
- WAP to print factorial of any number using recursive function.
- WAP to print the value of Y=x^n using recursive function.
- WAP to print the value of HCF of Two numbers using recursive function.
- WAP to print the sum of (sum = 1^2-2^2+3^2-4^2+.........)
- WAP to enter 5 elements in an array and display sum of all elements.
- WAP to enter 10 elements in an array and find the largest and smallest.
===========================================================
1.) Question 1
#include <stdio.h>
#include <conio.h>
void sec(int);
void main ()
{
clrscr ();
sec(10);
getch ();
}
void sec (int x)
{
if (x>0)
{
printf ("Sagarmatha Engineering College.\n");
x--;
sec(x);
}
}
====================================================================
2.) Question 2
#include <stdio.h>
#include <conio.h>
int fact(int);
void main ()
{
int x,f;
clrscr ();
printf ("Enter the valude of n:\n");
scanf ("%d",&x);
f=fact(x);
printf ("Factorial of %d is %d.\n",x,f);
getch ();
}
int fact (int n)
{
if (n==0||n==1)
return 1;
else
return (n*fact(n-1));
}
====================================================================
3.) Question 3
#include <stdio.h>
#include <conio.h>
int power (int , int );
void main ()
{
int x,n,y;
clrscr ();
printf ("Enter x to the power n:\n");
scanf ("%d%d",&x,&n);
y=power (x,n);
printf ("The result is %d.",y);
getch ();
}
int power (int b,int p)
{
if (p==0)
return 1;
else
return (b*power(b,p-1));
}
====================================================================
4.) Question 4
#include <stdio.h>
#include <conio.h>
int hcf (int , int );
void main ()
{
int a,b,r;
clrscr ();
printf ("Enter two numbers:\n");
scanf ("%d%d",&a,&b);
r=hcf(a,b);
printf ("The HCF is %d.",r);
getch ();
}
int hcf (int x,int y)
{
while (x!=y)
{
if (x>y)
return (hcf(x-y,y));
else
return (hcf(x,y-x));
}
return x;
}
====================================================================
5.) Question 5
#include <stdio.h>
#include <conio.h>
#include <math.h>
int sum (int n);
void main ()
{
int t,s;
clrscr ();
printf ("Enter the no of terms:\n");
scanf ("%d",&t);
s=sum(t);
printf ("The sum is %d.",s);
getch ();
}
int sum (int n)
{
if (n==0)
return 0;
else
return (pow(-1,n+1)*n*n+sum(n-1));
}
====================================================================
6.) Question 6
#include <stdio.h>
#include <conio.h>
void main ()
{
int x[5],i,s=0;
clrscr ();
printf ("Enter elements for array:\n");
for (i=0;i<5;i++)
{
printf ("Enter data: ");
scanf ("%d",&x[i]);
}
for (i=0;i<5;i++)
{
s=s+x[i];
}
printf ("The sum is %d.",s);
getch ();
}
====================================================================
7.) Question 7
#include <stdio.h>
#include <conio.h>
void main ()
{
int x[10],i,l,s;
clrscr ();
printf ("Enter elements for array:\n");
for (i=0;i<10;i++)
{
printf ("Enter data: ");
scanf ("%d",&x[i]);
}
l=x[0];
s=x[0];
for (i=0;i<10;i++)
{
if (x[i]>=l)
l=x[i];
if (x[i]<=s)
s=x[i];
}
printf ("The largest is %d.\n",l);
printf ("The smallest is %d.\n",s);
getch ();
}
No comments