NPTEL » Introduction to Programming in C
Please scroll down for latest Programs. 👇
Week 1: Assignment 1 - Question 1
The first input is the integer a.
The second input is the integer b.
The third input is is the integer c.
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
Use the following conversions: 1 inch = 2.54 cm, 1 foot = 12 inches, 1 foot = 30.48cm.
Note: The code for input and output handling as well as the variables are given to you. Do not change these parts.
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
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
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
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
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 |
Week 6: Assignment 6 - Question 1
Check Symmetric Matrix
A square matrix is said to be symmetric if it is equal to its transpose.
In other words, a matrix A of size n × n is symmetric if and only if:
for all Write a C program to check whether a given square matrix is symmetric or not.
Complete the function isSymmetric(int A[][n], int n) that checks if A is a symmetric matrix and returns 1 if symmetric, 0 otherwise.
Input Format
The first line contains an integer n, the size of the square matrix.
The next n lines each contain n integers, each line representing the elements in each row of the matrix.
Output Format
Print 1 if the matrix is symmetric.
Otherwise, print 0.
Note: You can assume that n < 10.
Sample Input 1
3
1 2 3
2 4 5
3 5 6
Sample Output 1
1
Explanation
The Input matrix is symmetric.
A[i][i] = A[i][i] for i = 0,1,2
A[0][1] = A[1][0] = 2
A[0][2] = A[2][0] = 3
A[1][2] = A[2][1] = 5
Sample Input 2
3
1 0 3
2 4 5
3 4 6
Sample Output 1
0
Explanation
The Input matrix is not symmetric.
A[0][1] = 0 but A[1][0] = 2
A[1][2] = 5 but A[2][1] = 4
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 3
1 2 3
2 4 5
3 5 6 | 1 | 1 | Passed |
Test Case 2 | 3
1 0 3
2 4 5
3 4 6 | 0 | 0 | Passed |
Week 6: Assignment 6 - Question 2
Simple Path Finding
Given an n×n binary Matrix A , where each entry is 0 or 1.
A has a unique path of 1's from A[0][0] to A[n-1][n-1].
The path always goes Right (R) or Down (D).
Write a C Program.to print the directions of this path.
Note: You can assume that there is exactly one correct path.
All 1's in A are in this unique path, there are no dead ends.
Input
The first line contains the dimension of the matrix n. Assume n < 100.
The second line onwards contains the contents of the matrix A, each row per line.
Output
The directions in the path of 1's in the Matrix from [0][0] to [n-1][n-1]
If the path goes Right print "R" and if it goes down, print "D".
Sample Input
4
1 1 0 0
0 1 1 0
0 0 1 0
0 0 1 1
Sample Output
RDRDDR
Explanation
(0,0) → (0,1): R
(0,1) → (1,1): D
(1,1) → (1,2): R
(1,2) → (2,2): D
(2,2) → (3,2): D
(3,2) → (3,3): R
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 4
1 1 0 0
0 1 1 0
0 0 1 0
0 0 1 1 | RDRDDR | RDRDDR | Passed |
Week 6: Assignment 6 - Question 3
Finding Islands
You are given an N × N binary matrix, where 1 represents land and 0 represents water.
Each island in the matrix is a non-overlapping rectangle of 1s, and no two islands touch each other (not even diagonally).
Write a C program to count the number of islands in the matrix.
Input
The first line contains a positive integer n (n < 20), the size of the matrix.
The next n lines each contain n integers (0 or 1), separated by spaces, representing the grid.
Output
The number of distinct islands in the matrix.
Sample input
6
1 1 1 1 0 0
1 1 1 1 0 0
0 0 0 0 0 0
1 1 0 0 1 1
1 1 0 0 1 1
1 1 0 0 0 0
3
Explanation
There are three islands in the matrix, illustarted below
1 1 1 1 0 0
1 1 1 1 0 0
0 0 0 0 0 0
1 1 0 0 1 1
1 1 0 0 1 1
1 1 0 0 0 0
Island #1 → From (0,0) to (1,3)
Island #2 → From (3,0) to (5,1)
Island #1 → From (3,4) to (4,5)
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 6
1 1 1 1 0 0
1 1 1 1 0 0
0 0 0 0 0 0
1 1 0 0 1 1
1 1 0 0 1 1
1 1 0 0 0 0 | 3 | 3 | Passed |
Week 7: Assignment 7 - Question 1
Student Database with Structures
Write a C program that creates a database of students using a struct. Each student record must contain:
- Name: a string of at most 20 characters (no spaces; input is a single token).
- Physics marks: an integer in [0, 100].
- Chemistry marks: an integer in [0, 100].
- Mathematics marks: an integer in [0, 100].
Read the data for n students and output the list sorted by the following rules (in order of priority):
- If a student ’A’ has higher marks in physics than a student ’B’, then A’s data is listed before B.
- If A and B have the same physics marks and A has higher chemistry marks than B, then A is listed before B.
- If A and B have the same marks in physics and chemistry, and A has higher marks in mathematics than B, then A is listed before B.
You may assume each student has a distinct Mathematics mark (i.e., no ties in Maths).
Input Format
- First line: an integer n (1 ≤ n ≤ 100).
- Next n lines: name physics chemistry mathematics (space-separated).
Output Format
Print the sorted database in n lines. Each line must be:
name physics chemistry mathematics
Requirements
- Define a struct to store a student’s data.
- Use an array (size up to 100) of these structures.
- Implement sorting according to the rules above.
Constraints
- 1 ≤ n ≤ 100
- Marks are integers in [0, 100]
- Name length ≤ 128 characters
Sample Input
5 alice 90 85 92 bob 90 88 80 charlie 95 70 78 diana 90 85 99 ed 95 65 81
Sample Output
charlie 95 70 78 ed 95 65 81 bob 90 88 80 diana 90 85 99 alice 90 85 92
Notes
- If two students tie on Physics and Chemistry, their relative order is decided by the higher Mathematics mark.
- Because all Mathematics marks are distinct, there will be no complete ties.
- You may use qsort to sort the elements. The template code is given to you.
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 5
alice 90 85 92
bob 90 88 80
charlie 95 70 78
diana 90 85 99
ed 95 65 81 | charlie 95 70 78\n
ed 95 65 81\n
bob 90 88 80\n
diana 90 85 99\n
alice 90 85 92 | charlie 95 70 78\n
ed 95 65 81\n
bob 90 88 80\n
diana 90 85 99\n
alice 90 85 92\n
| Passed after ignoring Presentation Error |
Week 7: Assignment 7 - Question 2
Linked List Operations
In this question, a linked list is partially implemented where each element in the linked list is a structure of the following format:
struct node { int id; int priority; struct node *next; };
The field priority is a positive integer, which denotes the priority of an element inside the list.
You have to complete the C code for performing the following operations on the linked list:
- Create and return a node e with given id and value val
struct node *create_node(int id, int val);
struct node *append(struct node *list, struct node *e);
struct node *search(struct node *list, int id);
void change_priority(struct node *list, int id, int val);
Note: The code for reading input and printing output is provided; you only need to write these functions.
Input
Each line contains an operation character followed by its arguments:
- A id val
Add a node with id and val to the list (insert at start). - C id val
Change the priority of element with id to val. If id not found, do nothing. - S id
Search for element with id. If found, print id and value on a line. Else print id and -1. - E
End of input; exit program.
Output
For each S operation print one line. Each printed line should contain two integers:
id value
When the element is not found print value as -1.
Example
Input: A 1 50 A 2 60 S 1 C 2 99 S 2 S 3 E Output: 1 50 2 99 3 -1
Constraints & Notes
- id and priority are integers.
- You may assume there is enough memory for node allocations.
- append inserts at the head (beginning) of the list.
- search returns a pointer to the node with matching id or NULL if not found.
- change_priority updates the priority field of the found node; if not found, it does nothing.
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | A 1 50
A 2 60
S 1
C 2 99
S 2
S 3
E | 1 50\n
2 99\n
3 -1 | 1 50\n
2 99\n
3 -1\n
| Passed after ignoring Presentation Error |
Week 8: Assignment 8 - Question 1
Add Two Numbers using Linked Lists
You are given two non-empty singly linked lists representing two non-negative integers. The digits are stored in reverse order, and each node contains a single digit. Add the two numbers and return the sum as a linked list with digits are stored in reverse order.
You may assume that both the list contain the same number of elements. The two lists do not contain any trailing zeros ( eg. 1 → 7 → 0 will not be used to represent 71).
This is the structure of the linked list.
struct ListNode { int val; struct ListNode* next; };Complete the following function. The function inputs two linked lists, representing two non-negative integers. Each node carries a single digit integer (0-9). The digits are stored in reverse order. Create and return a new linked list which represents the number obtained after addtion, in the reverse order.
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2);
Example1
Function Input
l1: 2 → 4 → 3
l2: 5 → 6 → 4
Function Output
7 → 0 → 8
Explanation
The first number is 342 (stored as 2 → 4 → 3).
The second number is 465 (stored as 5 → 6 → 4).
Their sum is 807, which is returned as 7 → 0 → 8.
Example 2
Function Input
l1: 9 → 9
l2: 1 → 1
Function Output
0 → 1 → 1
Explanation
The first number is 99 (stored as 9 → 9).
The second number is 11 (stored as 1 → 1).
Their sum is 110, which is returned as 0 → 1 → 1 (a new digit is created for the carry).
We specify the Program I/O description for Example 1.
Program Input
- First line: digits of the first number in reverse order (space separated).
- Second line: digits of the second number in reverse order (space separated).
Program Output
- Digits of the resulting sum in reverse order (space separated).
Example
Input
2 4 3
5 6 4
Output
7 0 8
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 2 4 3
5 6 4 | 7 0 8 | 7 0 8 | Passed |
Test Case 2 | 9 9
1 1 | 0 1 1 | 0 1 1 | Passed |
Week 8: Assignment 8 - Question 2
Merging two sorted lists
You are given the heads of two sorted linked lists.Complete the function
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2);
that merges the two lists into one sorted linked list and returns its head.
Each list node is defined as:
struct ListNode { int val; struct ListNode* next; };
Constraints
- Both lists are sorted in non-decreasing order.
- Each list has at least one element.
Example
Function Input
l1: 1 → 3 → 4 → 7 → 8
l2: 2 → 5 → 6
Function Output
1 → 2 → 3 → 4 → 5 → 6 → 7 → 8
Explanation
The first list is [1, 3, 4, 7 , 8] and the second list is [2, 5, 6].
Merging them while preserving sorted order gives [1, 2, 3, 4, 5, 6, 7 , 8].
Program Input
- First line: elements of the first linked list (space separated, in ascending order).
- Second line: elements of the second linked list (space separated, in ascending order).
Program Output
Print the merged linked list in ascending order, space separated.
Public Test Cases | Input | Expected Output | Actual Output | Status |
---|---|---|---|---|
Test Case 1 | 1 3 4 7 8
2 5 6 | 1 2 3 4 5 6 7 8 | 1 2 3 4 5 6 7 8 | 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.