Problems
Problems
4. Nancy, Simon, and Swati were all attending campus interviews. they got selected
for the second round. Nancy failed to clear the second round and others to
selected for the next round of interviews.Nancy discussed with her friend the
question which came in the interview.one of the questions was, to create a
program for the Fibonacci series. Nanc e doesn't know, how to solveit.But it's in
the syllabus of his exam. So can you help to create a program in the specified
concept to get an offer in the next interview ?.(using recursion)
Ans:
#include <stdio.h>
int fibonacci(int num)
{
if (num == 0)
{
return 0;
}
else if (num == 1)
{
return 1;
}
else
{
return fibonacci(num - 1) + fibonacci(num - 2);
}
}
int main()
{
int num;
scanf("%d", &num);
for (int i = 0; i < num; i++)
{
printf("%d, ", fibonacci(i));
}
return 0;
}
Input
5
Output
0, 1, 1, 2, 3,
5. Yasir is a very active young man who is very interested in making money in a
simple way. So he is always looking for a way to make some money. One day, a
money-making show called Jackpot on popular channel news came to Yasir's
ears. So he was going to the JACKPOT game in a game park it has a dial full of
numbers in random order. If it is arranged in ascending order using function ,
he will win a million-dollar prize. can you help him to input an array of size n
and sort it in ascending order?
Ans:
#include<stdio.h>
void asc(int a[100],int n)
{
int i,j,temp;
for(i=0; i<n-1; i++)
{
for(j=0; j<n-i-1; j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
int main()
{
int a[100],i,n;
scanf("%d", &n);
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
asc(a,n);
for(i=0; i<n; i++)
{
printf("%d ",a[i]);
}
return 0;
}
Input:
5
6
3
5
6
2
1
Output:
12566
6. Sajid is an eighth-grader in a CBSE school. Although he scored well in many
subjects, he did not an expert in computer programming languages. But Sajid's
computer examination is scheduled for next week. As per the blueprint, many
questions would come from the recursive function topic. He collected previous
year's questions. one of the repeated questions is to calculate the factorial value
for the given number using a recursive function. Can you help him to calculate
the factorial using recursion?
Ans:
#include <stdio.h>
long facto(int n)
{ if (n>=1) return n*facto(n-1); else
return 1;
}
int main()
{
int q;
scanf("%d",&q);
printf("%ld", facto(q));
return 0;
}
Input:
5
Output:
120
Ans:
#include <stdio.h>
int sum(int arr[],int start, int len);
int main()
{
int N,i;
scanf("%d",&N);
int arr[N];
for (i=0;i<N;i++)
scanf ("%d",&arr[i]);
int sumofarray=sum(arr,0,N);
printf("%d",sumofarray);
return 0;
}
int sum(int arr[],int start,int len)
{
int i;
for(i=0;i<len;i++)
start+=arr[i];
return start;
}
Input:
4
5
6
3
2
Output:
16