Home

Learn Programming & Prepare for NPTEL Exams... Swayam Solver is your one-stop destination for NPTEL exam preparation.

NPTEL Introduction to programming in C Programming Assignments July-2025 Swayam

 

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.

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.


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.
Your last recorded submission was on 2025-07-27, 11:06 IST
Select the Language for this assignment. 
1
#include <stdio.h>
2
int main() {
3
    int a, b, c;
4
    scanf("%d %d %d", &a, &b, &c);
5
    int secondLargest;
6
    if ((b < a && a < c) || (c < a && a < b))
7
        secondLargest = a;  
8
    else if ((a < b && b < c) || (c < b && b < a))
9
        secondLargest = b;
10
    else
11
        secondLargest = c;
12
    printf("%d",secondLargest);
13
    return 0;
14
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
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.

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.


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. 
Your last recorded submission was on 2025-07-27, 11:08 IST
Select the Language for this assignment. 
1
#include <stdio.h>
2
int main() {
3
    int cm;
4
    int feet;
5
    float inches;
6
    scanf("%d", &cm);
7
    feet = cm/30.48;
8
    inches = (cm - feet*30.48)/2.54;
9
    printf("%d %.2f", feet, inches);
10
    return 0;
11
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
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: 

57=35

Your last recorded submission was on 2025-07-27, 11:11 IST
Select the Language for this assignment. 
1
#include <stdio.h>
2
int main() {
3
    int l,b;
4
    int area;
5
    scanf("%d %d", &l,&b);
6
    area = l * b;
7
    printf("%d",area);
8
    return 0;
9
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
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 1,2,,1.  , then  

+1 for all i from 1 to n-1. You can assume that are at least two numbers before the ending -1.

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.

Your last recorded submission was on 2025-07-27, 11:18 IST
Select the Language for this assignment. 
1
#include <stdio.h>
2
int main() {
3
  int curr = 0; /* current value being read */
4
  int prev = 0; /* previous value read */
5
  int num_distinct = 0; /* number of distinct values read */
6
  scanf("%d", & curr);
7
  num_distinct = 1;
8
  while (curr != -1) {
9
    prev = curr;
10
    scanf("%d", & curr);
11
    if (prev != curr && curr != -1) {
12
      num_distinct = num_distinct + 1;
13
    }
14
  }
15
  printf("%d", num_distinct);
16
  return 0;
17
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
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

Your last recorded submission was on 2025-07-27, 11:20 IST
Select the Language for this assignment. 
1
#include <stdio.h>
2
int main() {
3
    int N;
4
    scanf("%d", &N); 
5
    for (int i = 1; i <= N; i++) {
6
        printf("%d\n", i * i); 
7
    }
8
    return 0;
9
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
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  !=1×2×.

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.


Your last recorded submission was on 2025-07-27, 11:22 IST
Select the Language for this assignment. 
1
#include <stdio.h>
2
int main() {
3
    int N;
4
    long factorial = 1;
5
    scanf("%d", &N);
6
    for (int i = 1; i <= N; i++) {
7
        factorial *= i;
8
    }
9
    printf("%ld", factorial);
10
    return 0;
11
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
Test Case 1
3
6
6
Passed
Test Case 2
4
24
24
Passed




Week 6: Assignment 6 - Question 1

Due on 2025-09-04, 23:59 IST

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 0,<

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

Your last recorded submission was on 2025-09-01, 18:22 IST
Select the Language for this assignment. 
1
#include <stdio.h>
2
int isSymmetric(int A[10][10], int n) {
3
    for (int i = 0; i < n; i++) {
4
        for (int j = 0; j < n; j++) {
5
            if (A[i][j] != A[j][i]) {
6
                return 0;
7
            }
8
        }
9
    }
10
    return 1;
11
}
12
int main() {
13
    int n;
14
    scanf("%d", &n);
15
    int A[10][10];
16
    for (int i = 0; i < n; i++) {
17
        for (int j = 0; j < n; j++) {
18
            scanf("%d", &A[i][j]);
19
        }
20
    }
21
    printf("%d",isSymmetric(A,n));
22
    return 0;
23
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
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

Due on 2025-09-04, 23:59 IST

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



Your last recorded submission was on 2025-09-01, 18:24 IST
Select the Language for this assignment. 
1
#include <stdio.h>
2
int main() {
3
    int n;
4
    scanf("%d", &n);
5
    int matrix[100][100];
6
    for (int i = 0; i < n; i++) {
7
        for (int j = 0; j < n; j++) {
8
            scanf("%d", &matrix[i][j]);
9
        }
10
    }
11
    int i = 0, j = 0;
12
    while (i != n-1 || j != n-1) {
13
        if (j+1 < n && matrix[i][j+1] == 1) {
14
            printf("R");
15
            j++;
16
        } else if (i+1 < n && matrix[i+1][j] == 1) {
17
            printf("D");
18
            i++;
19
        }
20
    }
21
    return 0;
22
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
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

Due on 2025-09-04, 23:59 IST

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

Sample Output

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)


Your last recorded submission was on 2025-09-01, 18:29 IST
Select the Language for this assignment. 
1
#include <stdio.h>
2
int main() {
3
    int n;
4
    scanf("%d", &n);
5
    int grid[20][20];
6
    for(int i = 0; i < n; i++)
7
        for(int j = 0; j < n; j++)
8
            scanf("%d", &grid[i][j]);
9
    int count = 0;
10
  
11
    int visited[20][20] = {0}; 
12
    for (int i = 0; i < n; i++) {
13
        for (int j = 0; j < n; j++) {
14
            if (grid[i][j] == 1 && !visited[i][j]) {
15
                int right = j;
16
                while (right < n && grid[i][right] == 1) {
17
                    right++;
18
                }
19
                right--; // last column of the rectangle
20
                int down = i;
21
                while (down < n && grid[down][j] == 1) {
22
                    down++;
23
                }
24
                down--; // last row of the rectangle
25
                int isRectangle = 1;
26
                for (int r = i; r <= down; r++) {
27
                    for (int c = j; c <= right; c++) {
28
                        if (grid[r][c] != 1) {
29
                            isRectangle = 0;
30
                            break;
31
                        }
32
                    }
33
                    if (!isRectangle) break;
34
                }
35
                if (isRectangle) {
36
                    for (int r = i; r <= down; r++) {
37
                        for (int c = j; c <= right; c++) {
38
                            visited[r][c] = 1;
39
                        }
40
                    }
41
                    count++;
42
                }
43
            }
44
        }
45
    }
46
    printf("%d", count);
47
    return 0;
48
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
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

Due on 2025-09-11, 23:59 IST

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):

  1. If a student ’A’ has higher marks in physics than a student ’B’, then A’s data is listed before B.
  2. If A and B have the same physics marks and A has higher chemistry marks than B, then A is listed before B.
  3. 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

  1. First line: an integer n (1 ≤ n ≤ 100).
  2. 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.
Your last recorded submission was on 2025-09-10, 21:27 IST
Select the Language for this assignment. 
1
#include <stdio.h>
2
#include <string.h>
3
#include <stdlib.h>
4
5
struct Student {
6
    char name[21];
7
    int physics;
8
    int chemistry;
9
    int maths;
10
};
11
12
// comparator for qsort
13
int compare(const void *a, const void *b) {
14
    struct Student *s1 = (struct Student *)a;
15
    struct Student *s2 = (struct Student *)b;
16
17
    if (s1->physics != s2->physics) {
18
        return s2->physics - s1->physics; // Higher physics first
19
    } else if (s1->chemistry != s2->chemistry) {
20
        return s2->chemistry - s1->chemistry; // Higher chemistry first
21
    } else {
22
        return s2->maths - s1->maths; // Higher maths first
23
    }
24
}
25
26
int main() {
27
    int n;
28
    scanf("%d", &n);
29
30
    struct Student arr[100];
31
32
    for (int i = 0; i < n; i++) {
33
        scanf("%s %d %d %d", arr[i].name, &arr[i].physics,
34
              &arr[i].chemistry, &arr[i].maths);
35
    }
36
37
    qsort(arr, n, sizeof(struct Student), compare);
38
39
    for (int i = 0; i < n; i++) {
40
        printf("%s %d %d %d\n", arr[i].name,
41
               arr[i].physics, arr[i].chemistry, arr[i].maths);
42
    }
43
44
    return 0;
45
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
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

Due on 2025-09-11, 23:59 IST

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);
  • Add a node e to the beginning of the list. Return the new list head.
  • struct node *append(struct node *list, struct node *e);
  • Search for a node e with id inside the list. Return a pointer to e if found, else return NULL.
  • struct node *search(struct node *list, int id);
  • Change the priority (value) of an element with given id (if found) to the new value val.
  • 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.
Your last recorded submission was on 2025-09-10, 21:33 IST
Select the Language for this assignment. 
1
#include <stdio.h>
2
#include <stdlib.h>
3
4
struct node {
5
    int id;
6
    int value;
7
    struct node *next;
8
};
9
10
struct node *create_node(int id, int val) {
11
    struct node *new_node = (struct node *)malloc(sizeof(struct node));
12
    new_node->id = id;
13
    new_node->value = val;
14
    new_node->next = NULL;
15
    return new_node;
16
}
17
18
struct node *append(struct node *list, struct node *e) {
19
    e->next = list;
20
    return e;
21
}
22
23
struct node *search(struct node *list, int id) {
24
    struct node *current = list;
25
    while (current != NULL) {
26
        if (current->id == id) {
27
            return current;
28
        }
29
        current = current->next;
30
    }
31
    return NULL;
32
}
33
34
void change_value(struct node *list, int id, int val) {
35
    struct node *e = search(list, id);
36
    if (e != NULL) {
37
        e->value = val;
38
    }
39
}
40
41
int find_value(struct node *list, int id) {
42
    struct node *e = search(list, id);
43
    if (e != NULL)
44
        return e->value;
45
    return -1;
46
}
47
48
int main() {
49
    char op;
50
    int id, val;
51
    struct node *list = NULL;
52
53
    int flag = 1;
54
    do {
55
        scanf(" %c", &op);
56
        switch (op) {
57
            case 'A':
58
                scanf("%d %d", &id, &val);
59
                list = append(list, create_node(id, val));
60
                break;
61
            case 'S':
62
                scanf("%d", &id);
63
                printf("%d %d\n", id, find_value(list, id));
64
                break;
65
            case 'C':
66
                scanf("%d %d", &id, &val);
67
                change_value(list, id, val);
68
                break;
69
            case 'E':
70
                flag = 0;
71
                break;
72
        }
73
    } while (flag == 1);
74
75
    return 0;
76
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
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

Due on 2025-09-18, 23:59 IST

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

Your last recorded submission was on 2025-09-17, 12:47 IST
Select the Language for this assignment. 
1
#include <stdio.h>
2
#include <stdlib.h>
3
4
struct ListNode {
5
    int val;
6
    struct ListNode* next;
7
};
8
9
struct ListNode* newNode(int val) {
10
    struct ListNode* node = (struct ListNode*)malloc(sizeof(struct ListNode));
11
    node->val = val;
12
    node->next = NULL;
13
    return node;
14
}
15
16
void appendNode(struct ListNode** head, int val) {
17
    struct ListNode* node = newNode(val);
18
    if (*head == NULL) {
19
        *head = node;
20
        return;
21
    }
22
    struct ListNode* temp = *head;
23
    while (temp->next != NULL) temp = temp->next;
24
    temp->next = node;
25
}
26
27
void printList(struct ListNode* head) {
28
    while (head != NULL) {
29
        printf("%d", head->val);
30
        if (head->next != NULL) printf(" ");
31
        head = head->next;
32
    }
33
}
34
35
struct ListNode* readList() {
36
    struct ListNode* head = NULL;
37
    int x;
38
    while (scanf("%d", &x) == 1) {
39
        appendNode(&head, x);
40
        int c = getchar();
41
        if (c == '\n' || c == EOF) break;
42
    }
43
    return head;
44
}
45
46
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
47
    struct ListNode* dummyHead = newNode(0);
48
    struct ListNode* current = dummyHead;
49
    int carry = 0;
50
    
51
    while (l1 != NULL || l2 != NULL || carry != 0) {
52
        int x = (l1 != NULL) ? l1->val : 0;
53
        int y = (l2 != NULL) ? l2->val : 0;
54
        int sum = carry + x + y;
55
        carry = sum / 10;
56
        current->next = newNode(sum % 10);
57
        current = current->next;
58
        if (l1 != NULL) l1 = l1->next;
59
        if (l2 != NULL) l2 = l2->next;
60
    }
61
    
62
    struct ListNode* result = dummyHead->next;
63
    free(dummyHead);
64
    return result;
65
}
66
67
int main() {
68
    struct ListNode* l1 = readList();
69
    struct ListNode* l2 = readList();
70
    
71
    struct ListNode* result = addTwoNumbers(l1, l2);
72
    printList(result);
73
    
74
    return 0;
75
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
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

Due on 2025-09-18, 23:59 IST

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].

We give the I/O specification for the program as well below.

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.

Your last recorded submission was on 2025-09-17, 12:50 IST
Select the Language for this assignment. 
1
#include <stdio.h>
2
#include <stdlib.h>
3
4
struct ListNode {
5
    int val;
6
    struct ListNode* next;
7
};
8
9
struct ListNode* newNode(int val) {
10
    struct ListNode* node = (struct ListNode*)malloc(sizeof(struct ListNode));
11
    node->val = val;
12
    node->next = NULL;
13
    return node;
14
}
15
16
void appendNode(struct ListNode** head, int val) {
17
    struct ListNode* node = newNode(val);
18
    if (*head == NULL) {
19
        *head = node;
20
        return;
21
    }
22
    struct ListNode* temp = *head;
23
    while (temp->next != NULL) temp = temp->next;
24
    temp->next = node;
25
}
26
27
void printList(struct ListNode* head) {
28
    while (head != NULL) {
29
        printf("%d", head->val);
30
        if (head->next != NULL) printf(" ");
31
        head = head->next;
32
    }
33
}
34
35
struct ListNode* readList() {
36
    struct ListNode* head = NULL;
37
    int x;
38
    char ch;
39
    while (scanf("%d", &x) == 1) {
40
        appendNode(&head, x);
41
        ch = getchar();
42
        if (ch == '\n' || ch == EOF) break;
43
    }
44
    return head;
45
}
46
47
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) {
48
    struct ListNode* dummy = newNode(0);
49
    struct ListNode* current = dummy;
50
    
51
    while (l1 != NULL && l2 != NULL) {
52
        if (l1->val <= l2->val) {
53
            current->next = l1;
54
            l1 = l1->next;
55
        } else {
56
            current->next = l2;
57
            l2 = l2->next;
58
        }
59
        current = current->next;
60
    }
61
    
62
    if (l1 != NULL) {
63
        current->next = l1;
64
    } else {
65
        current->next = l2;
66
    }
67
    
68
    struct ListNode* merged = dummy->next;
69
    free(dummy);
70
    return merged;
71
}
72
73
int main() {
74
    struct ListNode* l1 = readList();
75
    struct ListNode* l2 = readList();
76
    
77
    struct ListNode* merged = mergeTwoLists(l1, l2);
78
    printList(merged);
79
    
80
    return 0;
81
}
You may submit any number of times before the due date. The final submission will be considered for grading.
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program , your assignment will not be graded and you will not see your score after the deadline.
   


 
 
Public Test CasesInputExpected OutputActual OutputStatus
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.