Problem Solving Through Programming In C NPTEL Assignment 8

                     

Course Name: Problem Solving Through Programming In C NPTEL

Link of course: Click Here

These are answers for Problem Solving Through Programming In C Assignment 8


Q1. What will be the output?
a) 70
b) Garbage value
c) Compilation error
d) None

Answer: c) Compilation error


Q2. Which of the following statements are correct in C?
a) A function with the same name cannot have different signatures
b) A function with the same name cannot have different return types
c) A function with the same name cannot have a different number of parameters
d) All of the mentioned

Answer: d) All of the mentioned


Q3. What will the function return?
a) x * y where x and y are integers
b) x * y where x and y are non-negative integers
c) x + y where x and y are integers
d) x + y where x and y are non-negative integers

Answer: b) x * y where x and y are non-negative integers


These are answers for Problem Solving Through Programming In C Assignment 8


Q4. What is the output of the following C program?
a) Compiler error as foo() is not declared in main
b) 1 2
c) 2 1
d) Compile time error due to declaration of functions inside main

Answer: b) 1 2


Q5. What will be the output?
a) Choice 1
b) CChoice1
c) DefaultChoice1
d) CChoice1Choice2

Answer: b) CChoice1


Q6. What will be the output of the C code?
a) Compilation error
b) 0, 1, 2 …….., 127
c) 0, 1, 2, …….., 127, -128, -127,…infinite loop
d) 1, 2, 3…….,12

Answer: c) 0, 1, 2, …….., 127, -128, -127,…infinite loop


These are answers for Problem Solving Through Programming In C Assignment 8


Q7. What is the output of the following C program?
a) 55
b) 45
c) 66
d) 10

Answer: a) 55


Q8. Consider the function
find(int x, int y)
{
return((x<y) ? 0 : (x-y));
}
Let a and b be two non-negative integers. The call find(a, find(a, b)) can be used to find the

a) Maximum of a, b
b) Positive difference between a and b
c) Sum of a and b
d) Minimum of a and b

Answer: d) Minimum of a and b


These are answers for Problem Solving Through Programming In C Assignment 8


Q9. The function above has a flaw that may result in a serious error during some invocations.
Which one of the following describes the deficiency illustrated above?

a) For some values of n, the environment will almost certainly exhaust its stack space before the calculation completes.
b) An error in the algorithm causes unbounded recursion for all values of n.
c) A break statement should be inserted after each case. Fall-through is not desirable here.
d) The fibonacci() function includes calls to itself. This is not directly supported by Standard C due to its unreliability.

Answer: a) For some values of n, the environment will almost certainly exhaust its stack space before the calculation completes.


Q10. What is the output of the C code given below.

Answer: 27.08


These are answers for Problem Solving Through Programming In C Assignment 8


Programming Assigment

Question: 1  Write a C Program to find HCF of 4 given numbers using recursive function

Solution:

#include<stdio.h>
int HCF(int, int);

int main()
{
   int a, b, c, d, result;
   scanf("%d %d %d %d", &a, &b, &c, &d);
   result = HCF(HCF(a, b), HCF(c,d));
   printf("The HCF is %d", result);
}

int HCF(int x, int y)
{
   while (x != y)
   {
      if (x > y)
      		return HCF(x - y, y);
      else
       		return HCF(x, y - x);
   }
   return x;
}

These are answers for Problem Solving Through Programming In C Assignment 8


Question: 2  Write a C Program to print Binary Equivalent of an Integer using Recursion

Solution:

#include <stdio.h>
int binary_conversion(int);
int main()
{
  int num, bin;

  scanf("%d", &num);
  bin = binary_conversion(num);
  printf("The binary equivalent of %d is %d\n", num, bin);
  return 0;
}

int binary_conversion(int num)
{
   if(num == 0)
   		return 0;
   else
        return (num%2) + 10 * binary_conversion(num/2);
}

These are answers for Problem Solving Through Programming In C Assignment 8


Question: 3  Write a program to calculate the sum of a given series.

Solution:

#include <math.h>
#include <stdio.h>
double Series_Sum(int n);
int main()
{
    int n;
    scanf("%d", &n);
    double Result;
    Result = Series_Sum(n);

    printf("The sum of the series is = %.6f\n", Result);
    return 0;
}

double Series_Sum(int n)
{
    int i;
    double sums = 0.0, ser;
    for(i = 1; i <= n; ++i)
    {
        ser = 1 / pow(i, i);
        sums += ser;
    }
    return sums;
}

These are answers for Problem Solving Through Programming In C Assignment 8


Question : 4  Write a program to express a given integer as a Sum of Two Prime Numbers
For example if the number is 10
The result will be
10 = 3 + 7
10 = 5 + 5

Solution:

#include <stdio.h>
int checkPrime(int n);
int main()
{
    int n;
    scanf("%d", &n);

    int  i,flag = 0;
      for(i = 2; i <= n / 2; ++i)
      {
          if(checkPrime(i) == 1)
          {
            if(checkPrime(n - i) == 1)
            {
              printf("%d = %d + %d\n", n, i, n - i);
              flag = 1;
            }
          }
      }

      if(flag == 0)
          printf("%d cannot be expressed as the sum of two prime numbers.", n);

      return 0;
}

// function to check prime number
int checkPrime(int n){
  int i, isPrime = 1;

  if (n == 0 || n == 1) {
    isPrime = 0;
  }
  else {
    for(i = 2; i <= n/2; ++i) {
      if(n % i == 0) {
        isPrime = 0;
        break;
      }
    }
  }
  return isPrime;
}

These are answers for Problem Solving Through Programming In C Assignment 8

All weeks of Problem Solving Through Programming In C: https://progies.in/answers/nptel/problem-solving-through-programming-in-c

More NPTEL Solution: https://progies.in/answers/nptel


* The material and content uploaded on this website are for general information and reference purposes only. Please do it by your own first.


These are answers for Problem Solving Through Programming In C Assignment 8