You are on page 1of 5

Name:Fahad Aziz

Roll:22102028
Reg No:10842
Gst Merit:173
Problem 11

Problem: Write a C Program to find factorial


of a given number using recursion.

Input

Ask user to enter an integer from console.

Output

Output factorial depending on the user’s input.

Sample

Enter N: 5

Output: 120

Program code:
#include <stdio.h>
int fact(int n) {
if (n == 0 || n == 1)
return 1;
return n * fact(n - 1);
}
int main()
{
int num;
printf("Enter N: ");
scanf("%d", &num);
if (num < 0)
{
printf("Error: Factorial is defined only for non-negative integers.\n");
}
else
{
int result = fact(num);
printf("Output = %d\n",result);
}
return 0;
}

Problem 12

Problem: Write a C Program to find the


Fibonacci series consisting of n numbers using recursive function.

Input

Ask user to enter an integer from console.

Output
Output Fibonacci series of n numbers.

Sample

Enter N: 8

Output: 1 1 2 3 5 8 13 21

Program code:
#include <stdio.h>
int fibonacci(int n)
{
if (n <= 1)
return n;
else
return fibonacci(n - 1) + fibonacci(n - 2);
}

int main()
{
int n, i;
printf("Enter N: ");
scanf("%d", &n);

printf("Output:\n");
for (i = 0; i < n; i++)
{
printf("%d ", fibonacci(i));
}
return 0;
}

You might also like