Introduction to programming in C Week 3
Course Name: Introduction to programming in C
Course Link: Click Here
These are Introduction to programming in C Assignment 3 Answers
Question 1
Complete the function int find_factorial(int k) to find the factorial of the positive number k.
The factorial of a positive integer k, denoted by k!, is the product of all positive integers less than or equal to k.
k!=k×(k−1)×⋯×1.
Input
The first line of input is a positive integer N.
The next line contains N positive integers ki for i=1 to N.
Output
For each ki given as input, print factorial of ki.
Code:-
#include <stdio.h>
int find_factorial(int k){
int i,f = 1;
for(i=1;i<=k;i++)
f*=i;
return f;
}
int main(){
int n,k;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&k);
printf("%d ", find_factorial(k));
}
return 0;
}
These are Introduction to programming in C Assignment 3 Answers
Question 2
Write a C function to find the kth occurrence of an odd integer in a sequence of non-negative integers, and then call your function from main.
Your function should be according to the following declaration:
int find_odd(int k);
Input
——–
You are given the input in two lines:
The first line contains a positive integer k.
In the second line, you will be given a sequence of numbers terminated with a -1.
You have to find the kth occurrence of an odd integer in the sequence.
Note: The -1 is not part of the sequence.
Output
———-
If there are k odd numbers in the sequence, then output the kth occurrence of an odd number in the sequence, if present. If there are less than k odd numbers in the sequence, output -1.
Sample Input
——————
2
1 4 3 6 5 2 3 4 1 -1
Sample Output
——————–
3
Code:-
#include <stdio.h>
int find_odd(int k){
int x0=1,cnt=0;
while(x0!=-1)
{
scanf("%d",&x0);
if(x0%2)
++cnt;
if(cnt==k)
{
return x0;
break;
}
}
return -1;
}
int main(){
int k;
scanf("%d",&k);
printf("%d",find_odd(k));
return 0;
}
These are Introduction to programming in C Assignment 3 Answers
Question 3
The two moving average is the sequence of averages of the last 2 entries.
For the first number, no average is output.
For example, if the sequence of numbers is a1,a2,a3,a4,a5
The 2-moving average is (a1+a2)/2,(a2+a3)/2,(a3+a4)/2,(a4+a5)/2.
Code:-
#include <stdio.h>
int main()
{
int x0,x1;
scanf("%d",&x0);
while(1)
{
scanf("%d",&x1);
if(x1==-1)
break;
printf("%.1f ",((x0+x1)/2.0));
x0=x1;
}
return 0;
}
These are Introduction to programming in C Assignment 3 Answers
More Weeks of Introduction to programming in C: Click Here
More Nptel courses: https://progiez.com/nptel
These are Introduction to programming in C Assignment 3 Answers

This content is uploaded for study, general information, and reference purpose only.