# Introduction to Programming in C Week 3

**Session: JAN-APR 2024**

**Course Name: Introduction to Programming in C**

**Course Link: Click Here**

**For answers or latest updates join our telegram channel: Click here to join **

**These are Introduction to Programming in C Assignment 3 Answers**

**Question 1**

**Find moving average**

In this question, you have to output the “two moving average” of a sequence of non-negative numbers.

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

**Input**

The input is a sequence of non-negative numbers, terminated by a -1.

There will be at least 3 numbers in the sequence.

Note: The -1 is not part of the sequence. It is just to indicate that the input has ended.**Output**

You have to output the moving average of the sequence. The output should be printed correct to one digit after the decimal.

Hint: Use the format specifier “%.1f” inside printf.

**Solution:**

```
#include <stdio.h>
int main() {
double current, prev, twoMovingAverage;
scanf("%lf", &prev);
scanf("%lf", ¤t);
while (current != -1) {
twoMovingAverage = (prev + current) / 2.0;
printf("%.1f ", twoMovingAverage);
prev = current;
scanf("%lf", ¤t);
}
return 0;
}
```

**For answers or latest updates join our telegram channel: Click here to join **

**These are Introduction to Programming in C Assignment 3 Answers**

**Question 2**

**Prime Checking**Complete the function

**int is_prime(int n)**to check if a positive number n is prime or not.

The function returns 1 if n is prime, and 0 otherwise.

The function will be used in a program (code given) that prints the prime numbers in a given sequence.

**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**

———

The elements in the input list which are primes, in the original order.

**Solution:**

```
#include <stdio.h>
int is_prime(int n) {
if (n <= 1) {
return 0;
}
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return 0;
}
}
return 1;
}
int main() {
int N;
scanf("%d", &N);
int sequence[N];
for (int i = 0; i < N; i++) {
scanf("%d", &sequence[i]);
}
for (int i = 0; i < N; i++) {
if (is_prime(sequence[i])) {
printf("%d ", sequence[i]);
}
}
return 0;
}
```

**For answers or latest updates join our telegram channel: Click here to join **

**These are Introduction to Programming in C Assignment 3 Answers**

**Question 3**

**Find the kth Odd integer in sequence**

Write a C function to find the kth occurrence of an odd integer in a sequence of non-negative integers.

**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.

**Solution:**

```
#include <stdio.h>
int find_kth_odd(int k, int sequence[]) {
int countOdd = 0;
for (int i = 0; sequence[i] != -1; i++) {
if (sequence[i] % 2 != 0) {
countOdd++;
if (countOdd == k) {
return sequence[i];
}
}
}
return -1;
}
int main() {
int k;
scanf("%d", &k);
int sequence[100];
int num, i = 0;
while (1) {
scanf("%d", &num);
if (num == -1) {
break;
}
sequence[i++] = num;
}
int result = find_kth_odd(k, sequence);
printf("%d", result);
return 0;
}
```

**For answers or latest updates join our telegram channel: Click here to join **

**These are Introduction to Programming in C Assignment 3 Answers**

More Solutions of Introduction to Programming in C: Click Here

More NPTEL Solutions: https://progiez.com/nptel-assignment-answers/

**Session: JAN-APR 2023**

**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.

The factorial of a positive integer k, denoted by k!, is the product of all positive integers less than or equal to k.

**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

**These are Introduction to Programming in C Assignment 3 Answers**

**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,a5The 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**

The content uploaded on this website is for reference purposes only. Please do it yourself first. |