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









































































































































































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.