[go: up one dir, main page]

0% found this document useful (0 votes)
25 views8 pages

Oop 4

The document contains code snippets and sample outputs for 4 programming problems involving recursion. The problems include counting digits of a number, calculating powers of a number, finding factorials, and evaluating a recursive series. For each problem, the document provides the question, sample C code implementing a recursive solution, and example runs of the code.

Uploaded by

Aysha Noushad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views8 pages

Oop 4

The document contains code snippets and sample outputs for 4 programming problems involving recursion. The problems include counting digits of a number, calculating powers of a number, finding factorials, and evaluating a recursive series. For each problem, the document provides the question, sample C code implementing a recursive solution, and example runs of the code.

Uploaded by

Aysha Noushad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

OOPS ASSESSMENT - 4

NAME: Aysha Noushad


REGISTER NO: 23BRS1014

1.Imagine you are developing a software module for a financial application that deals with large
sets of numerical data. As part of the functionality, you need to determine the number of digits in
each financial transaction amount. You decide to implement a recursive function to achieve this.

//to count number of digits


#include <stdio.h>
int digicount(int num);
int digicount(int num)
{

if(num==0)
{
return 0;
}
else
{
return 1+digicount(num/10);
}
}
void main()
{
int num=0,digit=0;
printf("Enter the number");
scanf("%d",&num);
digit=digicount(num);
printf("The number of digits is %d",digit);
}

SAMPLE OUTPUT:
1. Enter the number3456
The number of digits is 4

2.Suppose you are designing a scientific computing library, and you


need to implement a function for calculating powers of a number. You
decide to use recursion for this purpose to make your code more
modular. Construct the following into C program.

//to get powers

#include <stdio.h>
int power(int a, int n);
void main()
{
int a,n,res;
printf("Enter value of a\n");
scanf("%d",&a);
printf("Enter value of n\n");
scanf("%d",&n);
res=power(a,n);
printf("The value of %d power %d is %d",a,n,res);
}
int power(int a,int n)
{
if(n==0)
{
return 1;
}
else
{
return a*power(a,n-1);
}
}

SAMPLE OUTPUT
Enter value of a
2
Enter value of n
8
The value of 2 power 8 is 256

3.Write a program to find factorial of a given number using recursion


#include <stdio.h>

int factorial(int num);


void main()
{
int num,fact;
printf("Enter the number");
scanf("%d",&num);
fact=factorial(num);
printf("The factorial of %d is %d",num,fact);
}
int factorial(int num)
{
if(num==1)
{
return 1;
}
else
{
return num*factorial(num-1);
}
}
SAMPLE OUTPUT
Enter the number5
The factorial of 5 is 120

4.WAP to use recursive calls to evaluate F(x) = x-x³/3! +


x³/5! – x7/7! +…+x/n!
CODE

// to print the series


#include <stdio.h>
#include <math.h>
int factorial(int n);
int power(int n, int x);
float pattern(float n, float x,float t);
void main()
{
int n=0,x=0,t=0;
float result;
printf("Enter x\n");
scanf("%d",&x);
printf("Enter n (must be odd)\n");
scanf("%d",&n);
t=n/2;
result=pattern(n,x,t);
printf("The result of the series pattern is %f",result);
}
int factorial(int n)
{
if(n==1)
{
return 1;
}
else
{
return n*factorial(n-1);
}
}
int power( int n, int x)
{
if(n==0)
{
return 1;
}
else
{
return x*power(n-1,x);
}
}
float pattern(float n,float x,float t)
{
if(n==1)
{
return x;
}
else
{
return pow(-1,t)*power(n,x)/factorial(n)+pattern(n-2,x,t-1);
}
}
SAMPLE OUTPUT 1
Enter x
2
Enter n (must be odd)
5
The result of the series pattern is 0.933333

SAMPLE OUTPUT 2
Enter x
2
Enter n (must be odd)
7
The result of the series pattern is 0.907937

You might also like