Home

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

NPTEL Problem Solving Through Programming In C - Programming Assignment | Jan-2025

NPTEL Problem Solving Through Programming In C - Programming Assignment | Jan-2025

 

NPTEL » Problem Solving Through Programming In C



Week-04: Program-01

Due on 2025-02-20, 23:59 IST
Write a C Program to Find the Smallest Number among Three Numbers (integer values) using Nested IF-Else statement.
Your last recorded submission was on 2025-02-17, 07:27 IST
Select the Language for this assignment. 
1
#include <stdio.h>
2
int main()
3
{
4
    int n1, n2, n3; 
5
    scanf("%d %d %d", &n1, &n2, &n3); /* where three number are read from the test cases and are stored in the variable n1, n2 and n3 */
6
 
7
/* Complete the program to get the desired output */
8
/* Only use the printf statement given below to exactly match your output 
9
with the output cases. Change the variable n1 as required.
10
 
11
printf("%d is the smallest number.", n1);    //Copy and paste this printf statement wherever required. 
12
 
13
*/
14
if (n1<n2)
15
    {
16
        if(n1<n3)
17
            printf("%d is the smallest number.", n1);
18
        else
19
            printf("%d is the smallest number.", n3);
20
    }
21
    else
22
    {
23
        if(n2<n3)
24
            printf("%d is the smallest number.", n2);
25
        else
26
            printf("%d is the smallest number.", n3);
27
    }
28
}
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
20 30 10
10 is the smallest number.
10 is the smallest number.
Passed
Test Case 2
77 44 99
44 is the smallest number.
44 is the smallest number.
Passed



Week-04: Program-02

Due on 2025-02-20, 23:59 IST
The length of three sides are taken as input. Write a C program to find whether a triangle can be formed or not. If not display “This Triangle is NOT possible.” If the triangle can be formed then check whether the triangle formed is equilateral, isosceles, scalene or a right-angled triangle. (If it is a right-angled triangle then only print Right-angle triangle do not print it as Scalene Triangle).
Your last recorded submission was on 2025-02-17, 07:29 IST
Select the Language for this assignment. 
1
#include<stdio.h>
2
int main()
3
{
4
    int a,b,c; 
5
    scanf("%d %d %d",&a, &b, &c); /*The length of three sides are entered from the test cases */
6
 
7
/* Complete the program. Copy and paste from the printf statements mentioned below wherever required for printing the outputs 
8
 
9
printf("Triangle is not possible");
10
printf("Right-angle Triangle");
11
printf("Isosceles Triangle");
12
printf("Equilateral Triangle");
13
printf("Scalene Triangle");
14
 
15
*/
16
if(a<(b+c)&&b<(a+c)&&c<(a+b))
17
    {
18
        if(a==b&&a==c&&b==c)
19
        printf("Equilateral Triangle");
20
          else if(a==b||a==c||b==c)
21
          printf("Isosceles Triangle");
22
          else   
23
    if((a*a)==(b*b)+(c*c)||(b*b)==(a*a)+(c*c)||(c*c)==(a*a)+(b*b))
24
        printf("Right-angle Triangle");
25
        else if(a!=b&&a!=c&&b!=c)
26
        printf("Scalene Triangle");
27
    }
28
    else
29
    printf("Triangle is not possible");
30
}
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
10 4 6
Triangle is not possible
Triangle is not possible
Passed
Test Case 2
7 6 8
Scalene Triangle
Scalene Triangle
Passed
Test Case 3
3 4 5
Right-angle Triangle
Right-angle Triangle
Passed



Week-04 Program-03

Due on 2025-02-20, 23:59 IST
Write a program to find the factorial of a given number using while loop.
Your last recorded submission was on 2025-02-17, 07:30 IST
Select the Language for this assignment. 
1
#include<stdio.h>
2
void main()
3
{
4
    int n;
5
    long int fact;  /* n is the number whose factorial we have to find and fact is the factorial */
6
    scanf("%d",&n);  /* The value of n is taken from test cases */
7
 
8
/* complete the program. Use the printf statements in the format mentioned below 
9
to match your output exactly with output test cases 
10
 
11
printf("The Factorial of %d is : %ld",n,fact);
12
 
13
You can declare any other variables if required */
14
int i=1;
15
fact = 1;
16
while(i<=n)
17
    {
18
        fact*=i;
19
        i++;
20
    }
21
    printf("The Factorial of %d is : %ld",n,fact);
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
5
The Factorial of 5 is : 120
The Factorial of 5 is : 120
Passed
Test Case 2
10
The Factorial of 10 is : 3628800
The Factorial of 10 is : 3628800
Passed



Week-04: Program-04

Due on 2025-02-20, 23:59 IST
Write a Program to find the sum of all even numbers from 1 to N where the value of N is taken as input. [For example when N is 10 then the sum is 2+4+6+8+10 = 30]
Your last recorded submission was on 2025-02-17, 07:31 IST
Select the Language for this assignment. 
1
#include <stdio.h>  
2
void main()
3
{
4
int N, sum=0; 
5
scanf("%d", &N); /* The value of N is taken from the test cases */
6
 
7
/* Complete the program. Please use the printf statement given below to 
8
exactly match your output with the test cases.
9
 
10
printf("Sum = %d", sum);
11
 
12
*/
13
for(int i=2; i <= N; i=i+2)
14
  sum+=i;
15
printf("Sum = %d", sum);
16
}
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
10
Sum = 30
Sum = 30
Passed
Test Case 2
30
Sum = 240
Sum = 240
Passed





Week-05 Problem-01

Due on 2025-02-27, 23:59 IST
Write a C program to check whether a given number (N) is a perfect number or not?
[Perfect Number - A perfect number is a positive integer number which is equals to the sum of its proper positive divisors. For example 6 is a perfect number because its proper divisors are 1, 2, 3 and it’s sum is equals to 6.]
Select the Language for this assignment. 
1
#include <stdio.h>
2
int main()
3
{
4
    int N; 
5
    scanf("%d",&N); /* An integer number taken as input from test cases */
6
 
7
/*Complete the program by writing the rest of the code in the space provided.
8
 
9
Please copy and paste the printf statement given below wherever required
10
printf("\n%d is a perfect number.",N);
11
printf("\n%d is not a perfect number.",N);
12
*/
13
int i, sum=0;
14
    for(i=1; i<N;i++)
15
    {
16
        if(N%i==0)
17
            sum+=i;
18
    }
19
 
20
    if(sum==N)
21
        printf("\n%d is a perfect number.",N);
22
    else
23
        printf("\n%d is not a perfect number.",N);
24
}
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
6 is a perfect number.
\n
6 is a perfect number.
Passed after ignoring Presentation Error
Test Case 2
87
87 is not a perfect number.
\n
87 is not a perfect number.
Passed after ignoring Presentation Error



Week-05 Problem-02

Due on 2025-02-27, 23:59 IST
Write a C program to count total number of digits of an Integer number (N).
Select the Language for this assignment. 
1
#include <stdio.h>
2
 int main()
3
{
4
    int N; 
5
    scanf("%d",&N); /*The number is accepted from the test case data*/
6
 
7
/* Complete the rest of the code. Please use the printf statements as below
8
by just changing the variables used in your program 
9
 
10
printf("The number %d contains %d digits.",N,count);
11
 
12
*/
13
int temp, count; 
14
count=0;
15
    temp=N;
16
    while(temp>0)
17
    {
18
        count++;
19
        temp/=10;
20
    }
21
     printf("The number %d contains %d digits.",N,count);
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
3456
The number 3456 contains 4 digits.
The number 3456 contains 4 digits.
Passed
Test Case 2
30001
The number 30001 contains 5 digits.
The number 30001 contains 5 digits.
Passed



Week-05 Problem-03

Due on 2025-02-27, 23:59 IST
Write a C program to check whether the given number(N) can be expressed as Power of Two (2) or not.
For example 8 can be expressed as 2^3.
Select the Language for this assignment. 
1
#include <stdio.h>
2
int main()
3
{
4
    int N;
5
    scanf("%d",&N); /* The value of N is taken from the test case data */
6
 
7
/* Complete the code.
8
Use the printf statements as below
9
printf("%d is a number that can be expressed as power of 2.",N);
10
printf("%d cannot be expressed as power of 2.",N);
11
*/
12
int temp, flag;
13
    temp=N;
14
    flag=0;
15
   
16
    while(temp!=1)
17
    {
18
        if(temp%2!=0){
19
            flag=1;
20
            break;
21
        }
22
        temp=temp/2;
23
    }
24
  
25
    if(flag==0)
26
        printf("%d is a number that can be expressed as power of 2.",N);
27
    else
28
        printf("%d cannot be expressed as power of 2.",N);
29
}
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
8
8 is a number that can be expressed as power of 2.
8 is a number that can be expressed as power of 2.
Passed
Test Case 2
46
46 cannot be expressed as power of 2.
46 cannot be expressed as power of 2.
Passed



Week-05 Program-04

Due on 2025-02-27, 23:59 IST
Write a C program to print the following Pyramid pattern upto Nth row. Where N (number of rows to be printed) is taken as input.
For example when the value of N is 5 the pyramid will be printed as follows
*****
****
***
**
*

Select the Language for this assignment. 
1
#include<stdio.h>
2
int main()
3
{
4
int N;
5
scanf("%d", &N); /*The value of N is taken as input from the test case */
6
int i,j;
7
for(i=N; i>0; i--)
8
  {
9
  for(j=0;j<i;j++)
10
    {
11
    printf("*");
12
    }
13
  printf("\n");
14
  } 
15
}
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
*****\n
****\n
***\n
**\n
*
*****\n
****\n
***\n
**\n
*\n
Passed after ignoring Presentation Error
Test Case 2
3
***\n
**\n
*
***\n
**\n
*\n
Passed after ignoring Presentation Error




Week-06 Program-01

Due on 2025-03-06, 23:59 IST
Write a C Program to find Largest Element of an Integer Array.
Here the number of elements in the array ‘n’ and the elements of the array is read from the test data.

Use the printf statement given below to print the largest element. printf("Largest element = %d", largest);
Select the Language for this assignment. 
1
#include <stdio.h>
2
 
3
int main()
4
{
5
    int i, n, largest;
6
    int arr[100];
7
 
8
    scanf("%d", &n); /*Accepts total number of elements from the test data */
9
 
10
 for(i = 0; i < n; ++i)
11
    {
12
       scanf("%d", &arr[i]); /* Accepts the array element from test data */
13
    }
14
largest = arr[0];
15
for(i = 1; i < n; ++i)
16
    {
17
           if(largest < arr[i])
18
           largest = arr[i];
19
    }
20
    printf("Largest element = %d", largest);
21
 
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
5
10
50
40
30
20
Largest element = 50
Largest element = 50
Passed
Test Case 2
7
100
50
60
70
90
30
40
Largest element = 100
Largest element = 100
Passed



Week-06 Program-02

Due on 2025-03-06, 23:59 IST
Write a C Program to print the array elements in reverse order (Not reverse sorted order. Just the last element will become first element, second last element will become second element and so on)
Here the size of the array, ‘n’ and the array elements is accepted from the test case data. The last part i.e. printing the array is also written.
 You have to complete the program so that it prints in the reverse order.
Select the Language for this assignment. 
1
#include<stdio.h>
2
 
3
int main() {
4
   int arr[20], i, n;
5
 
6
   scanf("%d", &n); /* Accepts the number of elements in the array */
7
 
8
  for (i = 0; i < n; i++) 
9
     scanf("%d", &arr[i]); /*Accepts the elements of the array */
10
int j, temp;  
11
j = i - 1;   // last Element of the array
12
i = 0;       // first element of the array
13
 
14
   while (i < j) {
15
      temp = arr[i];
16
      arr[i] = arr[j];
17
      arr[j] = temp;
18
      i++;             
19
      j--;        
20
   }
0
for (i = 0; i < n; i++) {
1
      printf("%d\n", arr[i]); // For printing the array elements 
2
   }
3
 
4
   return (0);
5
}
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
1
2
3
4
5
5\n
4\n
3\n
2\n
1
5\n
4\n
3\n
2\n
1\n
Passed after ignoring Presentation Error
Test Case 2
4
45
65
35
25
25\n
35\n
65\n
45
25\n
35\n
65\n
45\n
Passed after ignoring Presentation Error



Week-06 Program-03

Due on 2025-03-06, 23:59 IST
Write a C program to read Two One Dimensional Arrays of same data type (integer type) and merge them into another One Dimensional Array of same type. 
Select the Language for this assignment. 
1
#include<stdio.h>
2
int main() 
3
{
4
   int arr1[20], arr2[20], array_new[40], n1, n2, size, i;
5
 /*n1 size of first array (i.e. arr1[]), n2 size of second array(i.e. arr2[]), 
6
   size is the total size of the new array (array_new[]) */
7
 
8
   scanf("%d", &n1); //Get the size of first array from test data and store it in n1.
9
   
10
   for (i = 0; i < n1; i++)
11
      scanf("%d", &arr1[i]); //Accepts the values for first array 
12
 
13
   scanf("%d", &n2); //Get the size of second array from test data and store it in n2.
14
   
15
   for (i = 0; i < n2; i++)
16
      scanf("%d", &arr2[i]); //Accepts the values for second array
17
//Marge two arrays
18
int j;
19
for (i=0;i<n1;++i)
20
array_new[i]=arr1[i];
21
 
22
size =  n1 + n2;
23
 
24
for(i=0, j=n1; j<size && i<n2; ++i, ++j)
25
array_new[j] = arr2[i];
0
//Printing after merging
1
 
2
for (i = 0; i < size; i++) {
3
      printf("%d\n", array_new[i]);
4
   }
5
 
6
}
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
10
20
30
4
40
50
60
70
10\n
20\n
30\n
40\n
50\n
60\n
70
10\n
20\n
30\n
40\n
50\n
60\n
70\n
Passed after ignoring Presentation Error
Test Case 2
4
9
7
6
5
2
30
50
9\n
7\n
6\n
5\n
30\n
50
9\n
7\n
6\n
5\n
30\n
50\n
Passed after ignoring Presentation Error



Week-06 Program-04

Due on 2025-03-06, 23:59 IST
Write a C Program to delete duplicate elements from an array of integers.
Select the Language for this assignment. 
1
#include<stdio.h>
2
 
3
int main() 
4
{
5
   int array[50], i, size;
6
 
7
   scanf("%d", &size); /*Accepts the size of array from test case data */
8
 
9
   for (i = 0; i < size; i++)
10
   scanf("%d", &array[i]); /* Read the array elements from the test case data */
11
int j, k;
12
   for (i = 0; i < size; i++) {
13
      for (j = i + 1; j < size;) {
14
         if (array[j] == array[i]) {
15
            for (k = j; k < size; k++) {
16
               array[k] = array[k + 1];
17
            }
18
            size--;
19
         } else
20
            j++;
21
      }
22
   }
0
for (i = 0; i < size; i++) {
1
      printf("%d\n", array[i]);
2
   }
3
 
4
}
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
50
60
30
20
30
50\n
60\n
30\n
20
50\n
60\n
30\n
20\n
Passed after ignoring Presentation Error
Test Case 2
6
40
20
50
30
20
10
40\n
20\n
50\n
30\n
10
40\n
20\n
50\n
30\n
10\n
Passed after ignoring Presentation Error





Week-07 Program-01

Due on 2025-03-13, 23:59 IST
Write a C Program to Count Number of Uppercase and Lowercase Letters in a given string. The given string may be a word or a sentence.
Select the Language for this assignment. 
1
#include<stdio.h>
2
int main() {
3
   int upper = 0, lower = 0;
4
   char ch[100];
5
   scanf(" %[^\n]s", ch);  /*A word or a sentence is accepted from test case data */
6
 
7
/* Complete the remaining part of the code to store number of uppercase letters
8
in the variable upper and lowercase letters in variable lower.
9
The print part of already written. You can declare any variable if necessary */
10
int i = 0;
11
   while (ch[i] != '\0') {
12
      if (ch[i] >= 'A' && ch[i] <= 'Z')
13
         upper++;
14
      if (ch[i] >= 'a' && ch[i] <= 'z')
15
         lower++;
16
      i++;
17
   }
0
printf("Uppercase Letters : %d\n", upper); /*prints number of uppercase letters */
1
   printf("Lowercase Letters : %d", lower); /*prints number of lowercase letters */
2
 
3
   return (0);
4
}
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
Online NPTEL Course.
Uppercase Letters : 7\n
Lowercase Letters : 10
Uppercase Letters : 7\n
Lowercase Letters : 10
Passed
Test Case 2
National Programme on Technology Enhanced Learning is a joint initiative of the IITs and IISc.
Uppercase Letters : 11\n
Lowercase Letters : 68
Uppercase Letters : 11\n
Lowercase Letters : 68
Passed



Week-07 Program-02

Due on 2025-03-13, 23:59 IST
Write a C program to find the sum of all elements of each row of a matrix.
Example: For a matrix 4 5 6
                                    6 7 3
                                    1 2 3
 The output will be
15
 16
 6
Select the Language for this assignment. 
1
#include <stdio.h>
2
int main()
3
{
4
    int matrix[20][20];
5
    int i,j,r,c;
6
 
7
    scanf("%d",&r); //Accepts number of rows
8
    scanf("%d",&c); //Accepts number of columns 
9
 
10
    for(i=0;i< r;i++) //Accepts the matrix elements from the test case data
11
    {
12
        for(j=0;j< c;j++)
13
        {
14
            scanf("%d",&matrix[i][j]); 
15
        }
16
    }
17
/*Complete the code to print the sum of each rows. Use the printf() statement as
18
 printf("%d\n",sum); Where sum is the sum of a row. 
19
*/
20
int sum;
21
    for(i=0;i< r;i++)
22
    {
23
        sum=0;      
24
        for(j=0;j< c;j++)
25
        {
26
          //  printf("%d\t",matrix[i][j]);   
27
            sum     +=  matrix[i][j];
28
        }
29
        printf("%d\n",sum);
30
    }
31
}
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
3
1
1
1
2
2
2
3
3
3
3\n
6\n
9
3\n
6\n
9\n
Passed after ignoring Presentation Error
Test Case 2
2
3
1
2
3
4
5
6
6\n
15
6\n
15\n
Passed after ignoring Presentation Error



Week-07 Program-03

Due on 2025-03-13, 23:59 IST

Write a C program to find subtraction of two matrices i.e. matrix_A - matrix_B=matrix_C.

 If the given martix are 

 2 3 5     and  1 5 2    Then the output will be  1 -2 3

 4 5 6             2 3 4                                           2 2 2

 6 5 7             3 3 4                                           3 2 3

 The elements of the output matrix are separated by one blank space 
Select the Language for this assignment. 
1
#include <stdio.h>
2
int main()
3
{
4
    int matrix_A[20][20], matrix_B[20][20], matrix_C[20][20];
5
    int i,j,row,col;
6
    scanf("%d",&row); //Accepts number of rows
7
    scanf("%d",&col); //Accepts number of columns 
8
 
9
    /* Elements of first matrix are accepted from test data */
10
    for(i=0; i<row; i++)
11
    {
12
        for(j=0; j<col; j++)
13
        {
14
            scanf("%d", &matrix_A[i][j]);
15
        }
16
    }
17
 
18
     /* Elements of second matrix are accepted from test data */
19
    
20
    for(i=0; i<row; i++)
21
    {
22
        for(j=0; j<col; j++)
23
        {
24
            scanf("%d", &matrix_B[i][j]);
25
        }
26
    }
27
 
28
/* Complete the program to get the desired output. Use printf() statement as below
29
    printf("%d ", matrix_C[i][j]); You can declare your own variables if required. 
30
*/
31
for(i=0; i<row; i++)
32
    {
33
        for(j=0; j<col; j++)
34
        {
35
            matrix_C[i][j] = matrix_A[i][j] - matrix_B[i][j];
36
        }
37
    }
38
     for(i=0; i<row; i++)
39
    {
40
        for(j=0; j<col; j++)
41
        {
42
            printf("%d ", matrix_C[i][j]);
43
        }
44
        printf("\n");
45
    }
46
    return 0;
47
}
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
3
2
3
5
4
5
6
6
5
7
1
5
2
2
3
4
3
3
4
1 -2 3 \n
2 2 2 \n
3 2 3
1 -2 3 \n
2 2 2 \n
3 2 3 \n
Passed after ignoring Presentation Error



Week-07 Program-04

Due on 2025-03-13, 23:59 IST

Write a C program to print lower triangle of a square matrix.

For example the output of a given matrix 

2 3 4     will be       2 0 0

5 6 7                      5 6 0

4 5 6                      4 5 6

Select the Language for this assignment. 
1
#include <stdio.h>
2
int main()
3
{
4
int matrix[20][20];
5
int i,j,r;
6
   
7
scanf("%d", &r); //Accepts number of rows or columns
8
 
9
    for(i=0;i< r;i++) //Accepts the matrix elements from the test case data
10
    {
11
        for(j=0;j<r; j++)
12
        {
13
            scanf("%d",&matrix[i][j]);
14
        }
15
    }
16
 
17
/* Complete the program to get the desired output. Use the printf() statement as
18
printf("%d ", matrix[i][j]); */
19
for(i=0; i<r; i++) {
20
        for(j=0; j<r; j++) {
21
            if(i>=j)
22
                printf("%d ", matrix[i][j]);
23
            else
24
                printf("%d ", 0);
25
        }
26
        printf("\n");
27
    }
28
return 0;
29
}
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
1
2
3
1
2
3
1 0 0 \n
1 2 0 \n
1 2 3
1 0 0 \n
1 2 0 \n
1 2 3 \n
Passed after ignoring Presentation Error



Week 8: Programming Assignment 1

Due on 2025-03-20, 23:59 IST
Write a C Program to find HCF of 4 given numbers using recursive function
Select the Language for this assignment. 
1
#include<stdio.h>
2
int HCF(int, int); //You have to write this function which calculates the HCF. 
3
     
4
int main()
5
{
6
   int a, b, c, d, result;
7
   scanf("%d %d %d %d", &a, &b, &c, &d); /* Takes 4 number as input from the test data */
8
   result = HCF(HCF(a, b), HCF(c,d));
9
   printf("The HCF is %d", result);
10
}
11
 
12
//Complete the rest of the program to calculate HCF
13
int HCF(int x, int y)
14
{
15
   while (x != y)
16
   {
17
      if (x > y)
18
       {
19
           return HCF(x - y, y);
20
       }
21
       else
22
       {
23
       return HCF(x, y - x);
24
       }
25
    }
26
    return x;
27
}
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
100
30
50
70
The HCF is 10
The HCF is 10
Passed
Test Case 2
49
56
147
21
The HCF is 7
The HCF is 7
Passed



Week 8: Programming Assignment 2

Due on 2025-03-20, 23:59 IST
Write a C Program to find power of a given number using recursion. The number and the power to be calculated is taken from test case
Select the Language for this assignment. 
1
#include <stdio.h>  
2
long power(int, int);
3
int main()
4
{
5
int pow, num;
6
long result;
7
 
8
scanf("%d", &num); //The number taken as input from test case data 
9
 
10
scanf("%d", &pow); //The power is taken from the test case 
11
result = power(num, pow);
12
printf("%d^%d is %ld", num, pow, result);
13
return 0;
14
}
15
long power(int num, int pow)
16
{
17
if (pow)
18
{
19
return (num * power(num, pow - 1));
20
}
21
return 1;
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
5
2
5^2 is 25
5^2 is 25
Passed
Test Case 2
10
4
10^4 is 10000
10^4 is 10000
Passed



Week 8: Programming Assignment 3

Due on 2025-03-20, 23:59 IST
Write a C Program to print Binary Equivalent of an Integer using Recursion
Select the Language for this assignment. 
1
#include <stdio.h>
2
int binary_conversion(int); //function to convert binary to decimal number
3
int main()
4
  {
5
  int num, bin;  //num is the decimal number and bin is the binary equivalent for the number
6
 
7
  scanf("%d", &num); //The decimal number is taken from the test case data
8
  bin = binary_conversion(num); //binary number is stored in variable bin
9
  printf("The binary equivalent of %d is %d\n", num, bin);
10
  return 0;
11
  }
12
int binary_conversion(int num)
13
  { 
14
     if (num == 0)
15
        {
16
            return 0;
17
        }
18
        else
19
        {
20
            return (num % 2) + 10 * binary_conversion(num / 2);
21
        }
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
10
The binary equivalent of 10 is 1010
The binary equivalent of 10 is 1010\n
Passed after ignoring Presentation Error
Test Case 2
257
The binary equivalent of 257 is 100000001
The binary equivalent of 257 is 100000001\n
Passed after ignoring Presentation Error



Week 8: Programming Assignment 4

Due on 2025-03-20, 23:59 IST
Write a C Program to reverse a given word using function. e.g. INDIA should be printed as AIDNI
Select the Language for this assignment. 
1
#include<stdio.h>
2
#include<string.h>
3
 
4
void reverse(char[], int, int);
5
    int main()
6
    {
7
        char str1[20];
8
        scanf("%s", str1); //The string is taken as input form the test data. 
9
     
10
/* Complete the program to print the string in reverse order using the function
11
void reverse(char[], int, int);
12
Use the printf statement as
13
printf("The string after reversing is: %s\n", str1); 
14
You can use different variable also.
15
*/
16
int size;
17
    size = strlen(str1);
18
        reverse(str1, 0, size - 1);
19
        printf("The string after reversing is: %s", str1);  
20
        return 0;
21
    }
22
 
23
    void reverse(char str1[], int index, int size)
24
    {
25
        char temp;
26
        temp = str1[index];
27
        str1[index] = str1[size - index];
28
        str1[size - index] = temp;
29
        if (index == size / 2)
30
        {
31
            return;
32
        }
33
        reverse(str1, index + 1, size);
34
    }
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
computer
The string after reversing is: retupmoc
The string after reversing is: retupmoc
Passed
Test Case 2
NPTEL
The string after reversing is: LETPN
The string after reversing is: LETPN
Passed



Week-09 Program-01

Due on 2025-03-27, 23:59 IST
Write a program to print all the locations at which a particular element (taken as input) is found in a list and also print the total number of times it occurs in the list. The location starts from 1.
For example if there are 4 elements in the array
5
6
5
7
If the element to search is 5 then the output will be
5 is present at location 1
5 is present at location 3
5 is present 2 times in the array.
Select the Language for this assignment. 
1
#include <stdio.h>
2
int main()
3
{
4
   int array[100], search, n, count = 0;
5
   //"search" is the key element to search and 'n' is the total number of element of the array
6
   // "count" is to store total number of elements
7
 
8
 scanf("%d", &n); //Number of elements is taken from test case
9
 
10
 int c;
11
   for (c = 0; c < n; c++)
12
      scanf("%d", &array[c]);
13
 
14
   scanf("%d", &search); // The element to search is taken from test case
15
 
16
/* Use the printf statements as below:
17
"%d is present at location %d.\n"  for each locations
18
"%d is not present in the array.\n" if the element is not found in the list
19
"%d is present %d times in the array.\n"
20
*/
21
for (c = 0; c < n; c++)
22
    {
23
      if (array[c] == search)
24
      {
25
         printf("%d is present at location %d.\n", search, c+1);
26
         count++;
27
      }
28
    }
29
   if (count == 0)
30
      printf("%d is not present in the array.", search);
31
   else
32
      printf("%d is present %d times in the array.", search, count);
33
 
34
   return 0;
35
}
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
5
6
5
7
5
5 is present at location 1.\n
5 is present at location 3.\n
5 is present 2 times in the array.
5 is present at location 1.\n
5 is present at location 3.\n
5 is present 2 times in the array.
Passed
Test Case 2
5
67
80
45
97
100
50
50 is not present in the array.
50 is not present in the array.
Passed



Week-09 Program-02

Due on 2025-03-27, 23:59 IST
Write a C program to search a given element from a 1D array and display the position at which it is found by using linear search function. The index location starts from 1.
Select the Language for this assignment. 
1
#include <stdio.h>
2
int linear_search(int[], int, int);
3
int main()
4
{
5
   int array[100], search, c, n, position;
6
   /* search - element to search, c - counter, n - number of elements in array,
7
   position - The position in which the element is first found in the list. */
8
 
9
    scanf("%d", &n); // Number of elements in the array is read from the test case data
10
 
11
    for (c = 0; c < n; c++)
12
    scanf("%d", &array[c]); //Elements of array is read from the test data
13
 
14
    scanf("%d", &search);  //Element to search is read from the test case data
15
 
16
   /* Use the following in the printf statement to print the output
17
   printf("%d is not present in the array.\n", search);
18
   printf("%d is present at location %d.\n", search, position+1); //As array[0] has the position 1
19
   */
20
position = linear_search(array, n, search);
21
 
22
   if (position == -1)
23
      printf("%d is not present in the array.", search);
24
   else
25
      printf("%d is present at location %d.", search, position+1);
26
   return 0;
27
}
28
 
29
int linear_search(int a[], int n, int find) {
30
   int c;
31
   for (c = 0 ;c < n ; c++ )
32
    {
33
      if (a[c] == find)
34
         return c;
35
    }
36
   return -1;
37
}
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
78
90
34
54
98
90
90 is present at location 2.
90 is present at location 2.
Passed
Test Case 2
6
30
40
50
20
90
60
90
90 is present at location 5.
90 is present at location 5.
Passed



Week-09 Program-03

Due on 2025-03-27, 23:59 IST
Write a C program to search a given number from a sorted 1D array and display the position at which it is found using binary search algorithm. The index location starts from 1.
Select the Language for this assignment. 
1
#include <stdio.h>
2
int main()
3
{
4
 int c, n, search,
5
 array[100];
6
 scanf("%d",&n); //number of elements in the array
7
 
8
 for (c = 0; c < n; c++)
9
 scanf("%d",&array[c]);
10
 
11
 scanf("%d", &search); //The element to search is read from test case.
12
 
13
/* Use the printf statements as below:
14
 printf("%d found at location %d.\n", search, variable_name);
15
 printf("Not found! %d isn't present in the list.\n", search);
16
*/
17
int first, last, middle;
18
   first = 0;
19
   last = n - 1;
20
   middle = (first+last)/2;
21
 
22
   while (first <= last) {
23
      if (array[middle] < search)
24
         first = middle + 1;
25
      else if (array[middle] == search) {
26
         printf("%d found at location %d.", search, middle+1);
27
         break;
28
      }
29
      else
30
         last = middle - 1;
31
 
32
      middle = (first + last)/2;
33
      }
34
   if (first > last)
35
      printf("Not found! %d isn't present in the list.", search);
36
 
37
     return 0;
38
      }
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
10
20
30
40
50
40
40 found at location 4.
40 found at location 4.
Passed
Test Case 2
4
44
55
66
77
10
Not found! 10 isn't present in the list.
Not found! 10 isn't present in the list.
Passed



Week-09 Program-04

Due on 2025-03-27, 23:59 IST
Write a C program to reverse an array by swapping the elements and without using any new array.
Select the Language for this assignment. 
1
#include <stdio.h>
2
int main() {
3
  int array[100], n, c;
4
  scanf("%d", &n); // n is number of elements in the array.
5
  for (c = 0; c < n; c++) {
6
    scanf("%d", &array[c]);
7
  }
8
int temp, end;
9
   end = n - 1;
10
   for (c = 0; c < n/2; c++) {
11
    temp       = array[c];
12
    array[c]   = array[end];
13
    array[end] = temp;
14
 
15
    end--;
16
  }
0
printf("Reversed array elements are:\n");
1
 
2
  for (c = 0; c < n; c++) {
3
    printf("%d\n", array[c]);
4
  }
5
  return 0;
6
}
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
10
20
30
40
Reversed array elements are:\n
40\n
30\n
20\n
10
Reversed array elements are:\n
40\n
30\n
20\n
10\n
Passed after ignoring Presentation Error
Test Case 2
5
50
60
40
30
20
Reversed array elements are:\n
20\n
30\n
40\n
60\n
50
Reversed array elements are:\n
20\n
30\n
40\n
60\n
50\n
Passed after ignoring Presentation Error



Week-10 Program-01

Due on 2025-04-03, 23:59 IST
Select the Language for this assignment. 
1
#include<stdio.h>
2
float fun (float x); //Function fun returns the function value of f(x)
3
void bisection (float *x, float a, float b, int *itr); // This function computes the root of f(x) using bisection method
4
 
5
int main ()
6
{
7
    int itr = 0, maxmitr=10;
8
    float x, a=1.0, b=2.0, allerr, x1; // x is the value of root in each iteration, x1 is the final value of the root 
9
   // a and b are the initial range for calculating the root using bisection method
10
      
11
scanf("%f", &allerr);  // allerr is the allowable error taken from test case data 
12
    bisection (&x, a, b, &itr);
13
 
14
/* Use the printf statement as given below to print the root
15
printf("Root = %1.4f\n", x1); */
16
do
17
    {
18
        if (fun(a)*fun(x) < 0)
19
            b=x;
20
        else
21
            a=x;
22
        bisection (&x1, a, b, &itr);
23
        if (((x1-x)<0 && -(x1-x)< allerr) || ((x1-x)>0 && (x1-x)< allerr))
24
        {
25
            printf("Root = %1.4f", x1);
26
            return 0;
27
        }
28
        x=x1;
29
    }
30
    while (itr < maxmitr);
31
    return 1;
32
}
33
float fun (float x)
34
{
35
    return (2*x*x*x - 3*x - 5);
36
}
37
void bisection (float *x, float a, float b, int *itr)
38
/* this function performs and prints the result of one iteration */
39
{
40
    *x=(a+b)/2;
41
    ++(*itr);
42
}
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
0.001
Root = 1.7197
Root = 1.7197
Passed




Week-10 Program-02

Due on 2025-04-03, 23:59 IST
Select the Language for this assignment. 
1
#include<stdio.h>
2
float f(float x);
3
float df (float x);
4
 
5
int main()
6
{
7
    int itr, maxmitr; // itr is the iteration number and maxitr is the maximum allowable iteration 
8
    float x0=1.0, x1; // x0 is the initial value and x1 is result 
9
    scanf("%d", &maxmitr); // Taken from the test cases 
10
 
11
// use the printf statement as printf("Root = %8.6f\n", x1);
12
float h;
13
    for (itr=1; itr<=maxmitr; itr++)
14
    {
15
        h=f(x0)/df(x0);
16
        x1=x0-h;
17
        x0=x1;
18
    }
19
    printf("Root = %8.6f", x1);
20
    return 0;
21
}
22
float f(float x)
23
{
24
    return x*x*x - 2*x  - 3;
25
}
26
float df (float x)
27
{
28
    return 3*x*x-2;
29
}
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
Root = 3.465754
Root = 3.465754
Passed
Test Case 2
5
Root = 1.908690
Root = 1.908690
Passed




Week-10 Program-03

Due on 2025-04-03, 23:59 IST
Write a C program to sort a given 1D array using pointer in ascending order.
Select the Language for this assignment. 
1
#include <stdio.h>
2
int main()
3
{
4
    int a[100],i, n;
5
    scanf("%d",&n);  //Number of elements of the array is taken from the test case data 
6
   
7
   for (i=0; i<n; i++)
8
    {
9
        scanf("%d",a+i); // Input the array elements
10
    }
11
int j,t;
12
for (i=0; i<(n-1); i++) 
13
    {
14
        for (j=i+1; j<n; j++)
15
        {
16
            if (*(a+i)>*(a+j))
17
            {
18
                t=*(a+i);
19
                *(a+i)=*(a+j);
20
                *(a+j)=t;
21
            }
22
        }
23
    }
0
//   Printing sorted array in ascending order 
1
    for (i=0; i<n; i++)
2
    {
3
        printf("%d\n",*(a+i));
4
    }
5
    return 0;
6
   }
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
20
40
50
30
10
10\n
20\n
30\n
40\n
50
10\n
20\n
30\n
40\n
50\n
Passed after ignoring Presentation Error



Week-10 Program-04

Due on 2025-04-03, 23:59 IST
Write a C program to sort a 1D array using pointer by applying Bubble sort technique.
Select the Language for this assignment. 
1
#include<stdio.h>
2
void sort(int *a, int n);
3
int main()
4
{
5
    int a[20];
6
    int n,i; 
7
    scanf("%d",&n); // Enter number of elements to sort is taken from test case data
8
   
9
    for(i=0;i<n;i++)
10
    {
11
        scanf("%d",&a[i]); // The elements of the array is taken from the test data
12
    }
13
 
14
sort(a, n); // Calling the sorting function
15
 
16
    //Printing the sorted array 
17
    for(i=0;i<n;i++)
18
    {
19
        printf("%d\n",a[i]);
20
    }
21
   return 0;
22
}
23
void sort(int *a, int n)
24
{
25
    int i,temp,j;
26
    for(i=1;i<n;i++)
27
    {
28
        for(j=0;j<n-i;j++)
29
        {
30
           if(*(a+j)>=*(a+j+1))
31
        {
32
            temp = *(a+j);
33
            *(a+j)= *(a+j+1);
34
            *(a+j+1)= temp;
35
        }
36
        }
37
    }
38
}
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
-10
90
30
20
-100
50
-100\n
-10\n
20\n
30\n
50\n
90
-100\n
-10\n
20\n
30\n
50\n
90\n
Passed after ignoring Presentation Error






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.