# Problem Solving Through Programming In C Week 5

Session: JULY-DEC 2023

Course Name: Problem Solving Through Programming In C

#### These are Problem Solving Through Programming In C Assignment 5 Answers

Q1. The statement that transfers control to the beginning of the loop is called
a) break
b) continue
c) goto
d) None of the above

Q2. In C three way transfer of control is possible using
a) Unary operator
b) Logical operator
c) Ternary operator
d) None

Q3. What will be the output of the following code?
a) ‘while vs do-while’ once
b) ‘Out of loop’ infinite times
c) Both ‘while vs do-while’ and ‘Out of loop’ once
d) ‘while vs do-while’ infinite times

Answer: d) ‘while vs do-while’ infinite times

These are Problem Solving Through Programming In C Assignment 5 Answers

Q4. What is the output of the following C program?
a) 5
b) 10
c) No output
d) Compilation error

Q5. What is the output of the following C code?
a) True
b) False
c) Both ‘True’ and ‘False’
d) Compilation error

Answer: c) Both ‘True’ and ‘False’

Q6. What will be the output?
a) After loop x=1
b) 1
After loop x=2
c) 1 2 3 4 5 6 7 8 9 10
d) No output

These are Problem Solving Through Programming In C Assignment 5 Answers

Q7. What will be the output?
a) Error
b) I love C – will be printed 3 times
c) I love C – will be printed 6 times
d) I love C – will be printed 5 times

Answer: b) I love C – will be printed 3 times

Q8. What will be the output?
a) 1
b) 2
c) 3
d) Error

Q9. The following program is used to find the reverse of a number using C language. Find the missing condition inside while statement (indicated as ‘xxxx’).
a) n!=0
b) n==0
c) n%10==0
d) n/10==0

These are Problem Solving Through Programming In C Assignment 5 Answers

Q10. Compute the printed value of i & j of the C program given below
a) 8,10
b) 8,9
c) 6, 9
d) 7, 10

### Assignment

Question 1
Write a C program to check whether a given number (N) is a perfect number or not.
[Perfect Number – A perfect number is a positive integer number which is equals to the sum of its proper positive divisors. For example 6 is a perfect number because its proper divisors are 1, 2, 3 and it’s sum is equals to 6.]

Solution:

``````int sum=0;
for(int i = 1; i <= N / 2; i++)
{
if(N % i == 0)
{
sum += i;
}
}

if(sum == N){
printf("%d is a perfect number.", N);
}
else{
printf("%d is not a perfect number.", N);
}
return 0;
}``````

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

Solution:

``````int temp, count;
count=0;
temp=N;
while(temp>0)
{
count++;
temp/=10;
}
printf("The number %d contains %d digits.",N,count);
}``````

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:

``````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);
}``````

Question 4
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:

``````int i;
for(i=1;i<=N;i++)
{
sum = sum + ((float)1/(float)i);
}
printf("Sum of the series is: %.2f",sum);
}``````

These are Problem Solving Through Programming In C Assignment 5 Answers

Session: JAN-APR 2023

Course Name: Problem Solving Through Programming In C

These are Problem Solving Through Programming In C Assignment 5 Answers

#### Q1. “continue” statement in C is useda) to continue to the next line of codeb) to debugc) to stop the current iteration and begin the next iteration from the beginningd) None of the above are true

Answer: c) to stop the current iteration and begin the next iteration from the beginning

Q2. The operators << and >> are
a) Relational operator
b) Logical operator
c) Assignment operator
d) Bitwise shift operator

These are Problem Solving Through Programming In C Assignment 5 Answers

Q3. Compute the printed value of i of the C program given below
a) 4,5
b) 4,4
c) 5,5
d) 0,0

Q4. How many times ‘Hello’ will be printed while executing the below C code?
a) 4 times
b) 20 times
c) 5 times
d) no print

These are Problem Solving Through Programming In C Assignment 5 Answers

Q5. What is the output of the following C code?
a) True
b) False
c) Both ‘True’ and ‘False’
d) Compilation error

Answer: c) Both ‘True’ and ‘False’

Q6. Find the output of the following C program
a) 0
b) 1
c) No output
d) Compilation error

These are Problem Solving Through Programming In C Assignment 5 Answers

Q7. How many times the ‘Hello” will be printed in the below C code?
a) 1 time
b) 10 times
c) 11 times
d) Compilation error

Q8. How many times Hello will be printed in the below C code?
a) 10 times
b) 5 times
c) 6 times
d) Infinite times

These are Problem Solving Through Programming In C Assignment 5 Answers

Q9. ‘What will be the output?
a) 1
b) 2
c) 3
d) Error

Q10. What is the output of the following C program?
a) 5
b) 10
c) No output
d) Compilation error

These are Problem Solving Through Programming In C Assignment 5 Answers

### Problem Solving Through Programming In C Programming Assignment

Question 1

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

Solution:

``````int temp, count;
count=0;
temp=N;
while(temp>0)
{
count++;
temp/=10;
}
printf("The number %d contains %d digits.",N,count);
}``````

Question 2

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:

``````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 Problem Solving Through Programming In C Assignment 5 Answers

Question 3

Write a C program to find sum of following series where the value of N (odd integer number) is taken as input
123252+….+n2?

Solution:

``````for (i = 1; i <= n; i += 2) {
sum += i * i;
}
printf("Sum=%d\n", sum);
return 0;
}``````

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:

``````int i,j;
for(i=N; i>0; i--)
{
for(j=0;j<i;j++)
{
printf("*");
}
printf("\n");
}
}``````

These are Problem Solving Through Programming In C Assignment 5 Answers

More Nptel courses: https://progiez.com/nptel

Session: JULY-DEC 2022

Course Name: Problem Solving Through Programming In C NPTEL

#### These are Problem Solving Through Programming In C Assignment 5 Answers

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

Q2) Which of the following is not an infinite loop?
a) for(; ; )
b) for(x=0; x<=10)
c) while(1)
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.

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

These are Problem Solving Through Programming In C Assignment 5 Answers

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

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

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

These are Problem Solving Through Programming In C Assignment 5 Answers

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

These are Problem Solving Through Programming In C Assignment 5 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 Problem Solving Through Programming In C Assignment 5 Answers

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 Problem Solving Through Programming In C Assignment 5 Answers

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 Problem Solving Through Programming In C Assignment 5 Answers

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 Problem Solving Through Programming In C Assignment 5 Answers

More NPTEL Solution: https://progiez.com/nptel

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