Problem Solving Through Programming In C NPTEL Assignment 4

                     

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 4


Q1) The loop which is executed at least once is
a) “while” loop
b) “do-while” loop
c) “for” loop
d) None of the above

Answer: b) “do-while” loop


Q2) In the C programming language negative numbers when used in if-else conditional checking, are treated as
a) TRUE
b) FALSE
c) Depends on the implementation
d) None of these

Answer: a) TRUE


Q3) Choose the correct statement to use “if-else” statement in C Language
a) “else if” is compulsory to use with “if” statement.
b) “else” is compulsory to use with “if” statement.
c) “else” or “else if” is optional with the “if” statement.
d) None of the above are correct

Answer: c) “else” or “else if” is optional with the “if” statement.


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


Q4) What is the output of the following C code?
#include
int main()
{
int a = 1;
if (a–)
printf(“True\n”);
if (++a)
printf(“False\n”);
return 0;
}

a) True
b) False
c) Both ‘True’ and ‘False’ are printed
d) Compilation error

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


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


Q5) In the following example, tell which statement is correct
if(condition1== 1)) && if( (condition2==1)
printf(“Swayam”);

a) Condition 1 will be evaluated first, and condition2 will be evaluated second
b) Condition2 will be evaluated first, and condition1 will be evaluated second
c) Condition 1 will be evaluated first, condition2 will be evaluated only if the condition1 is TRUE
d) Condition2 will be evaluated first, and condition1 will be evaluated only if condition2 is TRUE

Answer: c) Condition 1 will be evaluated first, condition2 will be evaluated only if the condition1 is TRUE


Q6) Which one of the following is the correct syntax for Ternary Operator in C language?
a) condition? expression1 : expression2
b) condition : expression1 ? expression2
c) condition? expression 1 < expression2
d) condition expression1 ? expression2

Answer: a) condition? expression1 : expression2


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


Q7) The purpose of the following program fragment is to
b=s+b;
s=b-s;
b=b-s;(where s and b are two integers)

a) Transfer the content of s to b
b) Transfer the content of b to s
c) Exchange (swap) the content of s and b
d) Negate the contents of s and b

Answer: c) Exchange (swap) the content of s and b


Q8) What will be the output?
#include
int main()
{
int x=0;
x = printf(“3”);
printf(“%d”,x);
return 0;
}

a) 11
b) 33
c) 31
d) 13

Answer: c) 31


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


Q9) What will be the output?
#include <stdio.h>
int main()
{
int i=0, j=1;
printf(“\n%d i++ && ++j);
printf(“\n%d”, %d”, ij);
return 0;
}

a) 0 12
b) 1 11
c) 0 00
d) 0 11

Answer: d) 0 11


Q10) What will be the value of a, b, and c after the execution of the following
int a = 5, b = 7, c = 111;
c/= ++a* b–;

a) a=5, b=6, c=2;
b) a=6, b=7, c=1;
c) a=6, b=6, c=2;
d) a=5, b=7, c=1;

Answer: c) a=6, b=6, c=2;


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


Programming Assesgment Answers

Question 1
Write a C Program to Find the Smallest Number among Three Numbers (integer values) using Nested IF-Else statement.

Solution:

#include <stdio.h>
int main()
{
    int n1, n2, n3;
    scanf("%d %d %d", &n1, &n2, &n3);
    if(n1 < n2)
    {
        if(n1 < n3)
            printf("%d is the smallest number.", n1);
        else
            printf("%d is the smallest number.", n3);
    }
    else
    {
        if(n2 < n3)
            printf("%d is the smallest number.", n2);
        else
            printf("%d is the smallest number.", n3);
    }
}

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


Question 2
Write a program to find whether a given character is a Vowel or consonant. A character is taken as input. The character may be in Upper Case or in Lower Case.

Solution:

#include <stdio.h>
int main()
{
    char ch;
    scanf("%c",&ch);
    int lowercase_vowel, uppercase_vowel;

	// evaluates to 1 if variable c is a lowercase vowel
    lowercase_vowel = (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u');

    // evaluates to 1 if variable c is a uppercase vowel
    uppercase_vowel = (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U');

    // evaluates to 1 (true) if c is a vowel
    if (lowercase_vowel || uppercase_vowel)
        printf("%c is a vowel.", ch);
    else
        printf("%c is a consonant.", ch);
    return 0;
}

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


Question 3
Write a C program to calculate the Sum of First and the Last Digit of a given Number.
For example if the number is 1234 the result is 1+ 4 = 5.

Solution:

#include <stdio.h>
int main()
{

    int N, First_digit, Last_digit;
    scanf("%d", &N);

    Last_digit = N%10;
    First_digit = N;

    while(First_digit >=10)
    {
        First_digit = First_digit/10;
    }
    printf("Sum of First and Last digit = %d", First_digit + Last_digit);
    return 0;
}

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


Question 4
Write a C program to find power of a number using while loops. The base number (>0) and exponent (>=0) is taken from the test cases.

Solution:

#include <stdio.h>
int main()
{
    int base, exponent;
    long int result;
    scanf("%d", &base);
    scanf("%d", &exponent);
    if(exponent == 0)
        result = 1;
    else
    {
        result = 1;
        while(exponent != 0)
        {
          result = result * base;
          --exponent;
      	}
    }
    printf("The result is : %ld\n", result);
    return 0;
}

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

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 4