This post will help you with the solutions of Introduction To Programming in C NPTEL 2022 Week 2 Assignment.
Course layout
Answers COMING SOON! Kindly Wait!
Week 0: Assignment answers
Week 1:Â Assignment answers
Week 2: Assignment answers
Week 3: Assignment answers
Week 4: Assignment answers
Week 5: Assignment answers
Week 6: Assignment answers
Week 7: Assignment answers
Week 8: Assignment answers
Introduction To Programming in C NPTEL 2022 Week 2 Assignment Solutions
Week 2: Question 1
If the input matrix is upper triangular, then print 1.
If the input matrix is both lower and upper triangular, then print 2.
If the input matrix is not a triangular matrix, then print 0.
#include<stdio.h>
int main() {
int N, val, lower = 1, upper = 1;
scanf("%d", &N);
for(int i=0; i<N; i++) {
for(int j=0; j<N; j++) {
scanf("%d", &val);
if(i>j && val!=0)
upper = 0;
if(i<j && val!=0)
lower = 0;
}
}
if((upper == 1) && (lower == 1))
printf("2");
else if(upper == 1)
printf("1");
else if(lower == 1)
printf("-1");
else
printf("0");
return 0;
}
Week 2: Question 2
You are given a sorted(either in the increasing or in the decreasing order) sequence of positive numbers, ending with a -1. You can assume that there are atleast three numbers before the ending -1.
Note : -1 is not a part of input. It only signifies that input has ended.
Let us call the sequence x0 x1 … xn -1.
You have to output 1 if there are at least three distinct numbers in the sequence.
otherwise output 0
Kindly do not use arrays in the code.
#include<stdio.h>
int main() {
int num, count = 0;
scanf("%d", &num);
while(num!=-1) {
int x, y;
x = num;
scanf("%d", &num);
y = num;
if((x!=y) && (num!=-1))
count++;
}
if(count >= 2)
printf("1");
else
printf("0");
return 0;
}
Week 2: Question 3
You are given a non-negative sequence of numbers, ending with a -1. You can assume that there are at least two numbers before the ending -1.
Note : -1 is not a part of input. It only signifies that input has ended.
Let us call the sequence x0 x1 … xn -1.
You have to output the second largest element of the sequence. if there is no second largest element in the sequence then output 0.
Kindly do not use arrays in the code.
#include<stdio.h>
int main() {
int num, num1, num2;
scanf("%d", &num);
num1 = num;
num2 = 0;
while(num!=-1) {
scanf("%d", &num);
if(num1 < num) {
num2 = num1;
num1 = num;
}
}
if(num2 != 0)
printf("%d", num2);
else
printf("0");
return 0;
}
<< Prev- Introduction To Programming in C Week 1 Assignment Solutions
>> Next- Introduction To Programming in C Week 3 Assignment Solutions
DISCLAIMER: Use these answers only for the reference purpose. Quizermania doesn't claim these answers to be 100% correct. So, make sure you submit your assignments on the basis of your knowledge.
NPTEL answers: Problem solving through programming in C
Programming in Java NPTEL week 1 quiz answers
NPTEL – Python for Data Science assignment solutions
Nptel – Deep Learning assignment solutions
For discussion about any question, join the below comment section. And get the solution of your query. Also, try to share your thoughts about the topics covered in this particular quiz.