NPTEL » Introduction to Programming in C
Please scroll down for latest Programs. 👇
Week 1: Assignment 1 - Question 1
Due on 2025-08-07, 23:59 IST
Given three distinct integers a b and c, write a C program to find the second
largest number among them.
Input
Three distinct integers a b c.
The first input is the integer a.
The second input is the integer b.
The third input is is the integer c.
The first input is the integer a.
The second input is the integer b.
The third input is is the integer c.
Output
The second largest among a, b and c
Note: The assignment evaluation is automated.
Therefore, do not print (using printf) anything other than the output value.
Note: The assignment evaluation is automated.
Therefore, do not print (using printf) anything other than the output value.
Statements such as printf("Enter the first integer") printf("The second largest integer is %d", val) etc will lead to incorrect output.
Ideally, the only printf line in the code should be printf("%d",val); Here val can be any variable that stores the output value.
Sample input
------------------
2 3 1
Sample output
-------------------
2
Explanation
----------------
2 is the second largest among {2, 3 , 1}.
1 < 2 and 2 < 3.
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 2 3 1 | 2 | 2 | Passed |
Test Case 2 | 13 34 27 | 27 | 27 | Passed |
Week 1: Assignment 1 - Question 2
Due on 2025-08-07, 23:59 IST
Write a C program to convert a length given in centimeters into feet and inches.
Use the following conversions: 1 inch = 2.54 cm, 1 foot = 12 inches, 1 foot = 30.48cm.
Use the following conversions: 1 inch = 2.54 cm, 1 foot = 12 inches, 1 foot = 30.48cm.
Input
A non-negative integer, the value of the length in cm.
Output
Output the length in feet followed by a space followed by the remaining length in inches (truncated to 2 decimal places).
Note: The code for input and output handling as well as the variables are given to you. Do not change these parts.
Note: The code for input and output handling as well as the variables are given to you. Do not change these parts.
Sample Input
170
Sample Output
5 6.93
Explantion
170 / 30.48 = 5.577 (approx)
So 170 cm is 5 feet and some inches
Remaining length in cm = 170 - (30.48 * 5) = 17.6cm
Remaining length in inches = 17.6 / 2.54 = 6.929 (approx)
The length in inches truncated to two positions is 6.96.
Explantion
170 / 30.48 = 5.577 (approx)
So 170 cm is 5 feet and some inches
Remaining length in cm = 170 - (30.48 * 5) = 17.6cm
Remaining length in inches = 17.6 / 2.54 = 6.929 (approx)
The length in inches truncated to two positions is 6.96.
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 170 | 5 6.93 | 5 6.93 | Passed |
Test Case 2 | 100 | 3 3.37 | 3 3.37 | Passed |
Week 1: Assignment 1 - Question 3
Due on 2025-08-07, 23:59 IST
Write a C program to compute the area of a rectangle.
Use the formula:
Input
Two non-negative integers, the values of the length and breadth of the rectangle.
Output
Output the area of the rectangle as a single integer.
Sample Input
5 7
Sample Output
35
Explanation
The area of a rectangle is calculated as:
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 5 7 | 35 | 35 | Passed |
Week 2: Assignment 2 - Question 1
Due on 2025-08-07, 23:59 IST
Count Distinct Elements in a Sorted Sequence
You are given a non-decreasing sorted sequence of non-negative integers, ending with -1
. That is, if the sequence is , then
You have to output the number of distinct elements in the sorted sequence (excluding the -1).
Input
A sequence of non-negative integers in non-decreasing order, terminated by -1.
Output
Output a single integer: the number of distinct elements in the sequence (excluding -1).
Sample Input
2 2 2 3 4 4 6 6 6 6 -1
Sample Output
5
Explanation
The distinct elements are {2, 3, 4, 6}. So the answer is 4.
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 2 2 2 3 4 4 6 6 6 6 -1 | 4 | 4 | Passed |
Test Case 2 | 5 5 7 19 19 -1 | 3 | 3 | Passed |
Week 2: Assignment 2 - Question 2
Due on 2025-08-07, 23:59 IST
Print Squares of First N Numbers
Write a C program that reads a positive number N and prints the squares of numbers from 1 to N.
Input
A single positive integer N
Output:
Print i*i for each number i from 1 to N, each on a new line.
Sample Input:
3
Sample Output:
1
4
9
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 3 | 1\n
4\n
9 | 1\n
4\n
9\n
| Passed after ignoring Presentation Error |
Test Case 2 | 5 | 1\n
4\n
9\n
16\n
25 | 1\n
4\n
9\n
16\n
25\n
| Passed after ignoring Presentation Error |
Week 2: Assignment 2 - Question 3
Due on 2025-08-07, 23:59 IST
Write a C program that reads a positive number N (less than 8) and print its factorial.
Factorial of a number N is defined as .
Input
A single positive integer N
Output:
Factorial of N
Sample Input:
3
Sample Output:
6
Explanation
3! = 1 * 2 * 3 = 6.
Sample Input:
4
Sample Output:
24
Explanation
4! = 1 * 2 * 3 * 4 = 24.
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 3 | 6 | 6 | Passed |
Test Case 2 | 4 | 24 | 24 | Passed |
No comments:
Post a Comment
Keep your comments reader friendly. Be civil and respectful. No self-promotion or spam. Stick to the topic. Questions welcome.