Problem Solving Through Programming In C NPTEL Assignment 5

                     

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 5


Q1) What will be the output?
a) 10 9 8 7 6 5 4 3 2 1
b) 1 2 3 4 5 6 7 8 9 10
c) No output
d) None of the above statements are correct

Answer: c) No output


Q2) Which of the following is not an infinite loop?
a) for(; 😉
b) for(x=0; x<=10)
c) while(1)
d) while(0)

Answer: d) while(0)


Q3) Consider the following and identify the false statement(s)?
i) ‘do-while’ loop must be terminated by a semi colon.
ii) ‘do-while’ loop is always an infinite loop.
iii) Even if the condition is false, the ‘do-while’ loop executes once.
iv) ‘do-while’ loop is an entry-controlled loop.

Answer: (ii), (iv)


Q4) Compute the printed value of ‘m’ and ‘n’ of the C program given below?
a) 5,7
b) 5,5
c) 7,7
d) 0,0

Answer: c) 7,7


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


Q5) What should be in the place of so that except i=8, rest of the values of i (as defined in the ‘for’ loop : i=0 to i=19) will be printed?
a) break
b) continue
c) switch
d) exit

Answer: b) continue


Q6) What will be the output?
a) NPTEL
b) IIT/IISc
c) NPTELSWAWAMIIT/IISC
d)Compilation error

Answer: c) NPTELSWAWAMIIT/IISC


Q7) What will be the output?
a) 1,3
b) 1,3 3,1
c) 1,3 2,2 3,1
d) 0,0

Answer: c) 1,3 2,2 3,1


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


Q8) What will be the output of the program?
a) 4 will print 1 times
b) 4 will print 3 times
c) 4 will print 4 times
d) No output

Answer: b) 4 will print 3 times


Q9) For the C program given below, if the input given by the user is 7. What will be shown on the output window?
a) The number is odd
b) The number is prime
c) The number is odd
d) The number is prime Syntax Error

Answer: b) The number is prime


Q10) What will be the output?
a) 0123456789 11 12…..infinite times
b) 123456789 11 12….infinite times
c) Won’t print anything
d) Error

Answer: c) Won’t print anything


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


Programming Assignment Answers

Question 1
Write a C program to count total number of digits of an Integer number (N).

Solution:

#include <stdio.h>
int main()
{
    int N;
    scanf("%d",&N);
    int temp, count;
    count=0;
    temp=N;
    while(temp>0)
    {
     	count++;
     	temp/=10;
    }
    printf("The number %d contains %d digits.",N,count);
}

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


Question 2
Write a C program to find sum of following series where the value of N is taken as input
1+ 1/2 + 1/3 + 1/4 + 1/5 + .. 1/N

Solution:

#include < stdio.h >
int main()
{
    int N;
    float sum = 0.0;
    scanf("%d",&N);
    int i;
    for(i=1;i < = N;i++)
    {
  	    sum = sum + ((float)1/(float)i);
    }
    printf("Sum of the series is: %.2f\n",sum);
}

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


Question 3
Write a C program to check whether the given number(N) can be expressed as Power of Two (2) or not.
For example 8 can be expressed as 2^3

Solution:

#include < stdio.h>
int main()
{
    int N;
    scanf("%d",&N);
    int temp, flag;
    temp=N;
    flag=0;

    while(temp!=1)
    {
        if(temp%2!=0)
        {
            flag=1;
            break;
        }
        temp=temp/2;
    }

    if(flag==0)
        printf("%d is a number that can be expressed as power of 2.",N);
    else
        printf("%d cannot be expressed as power of 2.",N);
}

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


Question 4
Write a C program to print the following Pyramid pattern upto Nth row. Where N (number of rows to be printed) is taken as input.
For example when the value of N is 5 the pyramid will be printed as follows:
*****
****
***
**
*

Solution:

#include<stdio.h>
int main()
{
    int N;
    scanf("%d", &N);
    int i, j;
    for(i=N; i>0; i--)
    {
        for(j=0;j < i;j++)
        {
  		    printf("*");
        }
  	    printf("\n");
    }
}

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

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 5