Showing posts with label Recursive. Show all posts
Showing posts with label Recursive. Show all posts

wap to find sum of first n terms using recursive function


/*wap to find sum of first n terms using recursive function*/

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

int addition(int sum);
void main()
{
 int n,sum;
 clrscr();
 printf("enter value of n:");
 scanf("%d",&n);
 sum=addition(n);
 printf("sum of first %d numbers:%d",n,sum);
 getch();
}

int addition(int n)
{
 int sum=0;
 if(n==1)
        { return(1);          }
 else
        { sum=n+addition(n-1);
   return(sum);        }
}

wap using recursive function to generatr n terms and print the sum of following series for N terms.. 1+4+9+16+25+36+......+n=sum


/*wap using recursive function to generatr n terms and print the sum of
following series for N terms.. 1+4+9+16+25+36+......+n=sum*/

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

int addition(int n);

void main()

{
 int n,sum;
 clrscr();
 printf("enter value of n:");
 scanf("%d",&n);
 sum=addition(n);
 printf("%d",sum);
 getch();
}

int addition(int n)

{
 int sum;

 if(n==1)
        { return(1);          }
 else
        {
   return(pow(n,2)+addition(n-1));        }
}

wap to find prime factors of a given positive int number (make function "factor" to find prime factor)


/*wap to find prime factors of a given positive int num(make function "factor"
to find prime factor)*/

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

void factor(int n);
void main()
{
	int n;
	clrscr();
	printf("Enter any int to find its Prime Factors:\n");
	scanf("%d",&n);
	factor(n);
	getch();
}
void factor(int n)
{
	int a,i,j,c=0;
	printf("prime factor\n");
	for(i=2;i<=n;i++)
	{
		c=0;
		if(n%i==0)
		{
			a=i;
			for(j=2;j<i;j++)
			{
				if(a%j==0)
				{c++;}
			}
				if(c==0)
				{printf("%d\n",a);}
		}
	}
}

WAP to find factorial of any number

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

void main()
{
	int i,n,f=1;
	clrscr();
	printf("Enter No.");
	scanf("%d",&n);
	for(i=n;i>0;i--)
		f=f*i;
	printf("%d's Factorial value is : %d",n,f);
	getch();
}

WAP to find factorial using recursive of function

#include<conio.h>
#include<stdio.h>
int fact(int);

void main()
{
int n,f;
clrscr();

printf("\nEnter the number :-");
scanf("%d",&n);

f=fact(n);

printf("\nThe Factorial=%d",f);

getch();

}

int fact(int n)
{
 int f=1;
 if(n==1)
 {
 return(f);

 }
 else
 {
 f=n*fact(n-1);
 return(f);
 }
 }