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 Assignment Jan-2023 Swayam

NPTEL » Introduction to programming in C   

Jan 2023 NPTEL course


Scroll Down for latest Assignments


Support me : Subscribe to the YouTube Channel :  Swayam Solver




Week 1: Assignment 1 - Question 1

Due on 2023-02-09, 23:59 IST
You have a certain number of 100 rupee notes, 10 rupee notes and 1 rupee notes with you.
There is an item you want to buy whose price is given to you.
Write a program to find if the item is affordable, that is the price of the item is less than or equal to the current money you have.


Input
-----
Four non negative integers. 
The first input is an integer representing the number of 100 rupee notes.
The second input is an integer representing the number of 10 rupee notes.
The third input is an integer representing the number of 1 rupee notes.
The fourth input is an integer representing the price of the item.

Output
------
You have to output 1 if the item is affordable.
You have to output 0 if the item is not affordable. 

Your last recorded submission was on 2023-01-30, 08:40 IST
Select the Language for this assignment. 
1
#include<stdio.h>

void main()
{
  int h, t, o, price,current_money;
  scanf("%d %d %d %d", &h, &t, &o, &price);

  current_money = 100*h+10*t+o;
 
  if(price <= current_money)
    printf("1");
  else
    printf("0");
}

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 120
1
1
Passed
Test Case 2
3 17 9 500
0
0
Passed





Private Test cases used for EvaluationStatus
Test Case 1
Passed
Test Case 2
Passed
Test Case 3
Passed
Test Case 4
Passed
Test Case 5
Passed





Week 1: Assignment 1 - Question 2

Due on 2023-02-09, 23:59 IST
You are given two positive integers, say M and N.
Check whether M is an exact multiple of N, without using loops.

Input
-----
Two positive integers, say M and N.

Output
------
You have to output 0 if M is not a multiple of N.
You have to output 1 if M is a multiple of N.

Your last recorded submission was on 2023-01-28, 23:14 IST
Select the Language for this assignment. 
1
#include<stdio.h>

void main()
{
  int m,n;
  scanf("%d %d", &m, &n);


  if(m % n)
    printf("0");
  else
    printf("1");
}

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 5
1
1
Passed
Test Case 2
10 7
0
0
Passed




Private Test cases used for EvaluationStatus
Test Case 1
Passed
Test Case 2
Passed
Test Case 3
Passed
Test Case 4
Passed
Test Case 5
Passed






Week 1: Assignment 1 - Question 3

Due on 2023-02-09, 23:59 IST
Given three integers a b and c, find if they are strictly increasing/decreasing or not.
 
Input
-------
Triplet of three integers (a,b,c)

Output
---------
You have to output 1 if they are either in strictly increasing (a>b>c) or decreasing (a<b<c) order.
Output 0, otherwise.
Your last recorded submission was on 2023-01-28, 23:15 IST
Select the Language for this assignment. 
1
#include <stdio.h>
int main()

{
int a,b,c;
scanf("%d %d %d", &a,&b, &c);
if(((a>b) && (b>c))||((c>b) && (b>a)))
 {
   printf("1");
  }
else
  printf("0");
return 0;
}
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 3 2
1
1
Passed
Test Case 2
1 2 1
0
0
Passed


Private Test cases used for EvaluationStatus
Test Case 1
Passed
Test Case 2
Passed
Test Case 3
Passed
Test Case 4
Passed
Test Case 5
Passed







-----------------------------------------------------------




Week 2: Assignment 2 - Question 1

Due on 2023-02-09, 23:59 IST
You are given a non-negative sequence of numbers, ending with a -1. You can
assume that there are at least two numbers before the ending -1. 

You have to output the second largest element of the sequence.
If there is no second largest element in the sequence then output 0.

Note : -1 is not a part of input. It only signifies that input has ended.
Your last recorded submission was on 2023-02-09, 20:55 IST
Select the Language for this assignment. 
1
#include<stdio.h>

int main()
{
 int a,b,d;
 int c =0;
 scanf("%d %d",&a,&b);
 if(a>b)
  {
      c=b;
     
  }
 if(b>a)
  {
      c=a;
      a=b;
      b=c;
  }

 for(;;)
  {
      scanf("%d",&b);
      if(b==-1)
       {
           printf("%d",c);
           break;
       }
     
      if(b>a)
       { 
         c=a;
         a=b;
}
      if(b>c && b<a)
       {
           c =b;
       }
      
  }


return 0;
}
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 2 4 3 5 -1
4
4
Passed
Test Case 2
5 5 5 -1
0
0
Passed






Private Test cases used for EvaluationStatus
Test Case 1
Passed
Test Case 2
Passed
Test Case 3
Passed
Test Case 4
Passed
Test Case 5
Passed




Week 2: Assignment 2 - Question 2

Due on 2023-02-09, 23:59 IST
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.
Your last recorded submission was on 2023-02-09, 20:56 IST
Select the Language for this assignment. 
1
#include <stdio.h>
void main()
{
int cnt,x0,x1;
 
  cnt=0;
  
    scanf("%d",&x0);
    while(x0!=-1)
    {
            scanf("%d",&x1);
          if(x1!=x0)
                ++cnt;
      x0=x1;
    }
  printf("%d",cnt);
}
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 2 3 -1
3
3
Passed
Test Case 2
1 1 2 -1
2
2
Passed





Private Test cases used for EvaluationStatus
Test Case 1
Passed
Test Case 2
Passed
Test Case 3
Passed
Test Case 4
Passed
Test Case 5
Passed




Week 2: Assignment 2 - Question 3

Due on 2023-02-09, 23:59 IST
In this assignment, you will be given an  × matrix, with >1

You have to determine whether the matrix is an upper triangular matrix.

A matrix is upper triangular if every entry below the diagonal is 0. The following is an example of an upper triangular matrix:

                                                                 1111041100010001  

Note: The diagonal itself, and the entries above the diagonal can be zeroes or non-zero integers.

Input
-------
First, you will be given N, which is the size of the matrix.
Then you will be given N rows of integers, where each row consists of N integers separated by spaces.

Output
---------
If the input matrix is upper triangular, then print 1. Otherwise, print 0.




Your last recorded submission was on 2023-02-09, 20:57 IST
Select the Language for this assignment. 
1
#include <stdio.h>
void main()
{
int i,j,N,v;
  int lflag=1;
  int uflag=1;
  
scanf("%d", &N);
    for(i=0;i<N;i++)
    {
        for(j=0;j<N;j++)
        {
            scanf("%d",&v);
          if(j<i && v!=0)
                lflag=0;
          if(i<j && v!=0)
                uflag=0;    
        }
    }
    if(lflag)
            printf("1");
         else
            printf("0");
 
}
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 1 1 
0 1 1
0 0 1
 
1
1
Passed
Test Case 2
4
1 0 3 4 
5 6 7 0
0 9 3 2
1 1 1 0
0
0
Passed
Test Case 3
3
1 1 1 
0 0 1
0 0 1
1
1
Passed





Private Test cases used for EvaluationStatus
Test Case 1
Passed
Test Case 2
Passed
Test Case 3
Passed
Test Case 4
Passed
Test Case 5
Passed








Week 3: Assignment 3 - Question 1

Due on 2023-02-16, 23:59 IST
Complete the function int find_factorial(int k) to find the factorial of the positive number k.

The factorial of a positive integer , denoted by ! , is the product of all positive integers less than or equal to .

                                          !=×(1)××1.

Input 
---------
The first line of input is a positive integer N.
The next line contains N positive integers  for =1 to ..

Output
---------
For each  given as input, print factorial of .


Note 
---------
The code stub given takes care of reading the input, passing it over to the function find_factorial and printing the value (factorial of k) returned by the function. You have to just complete the function find_factorial, which takes a single positive integer   as input and returns !.

Your last recorded submission was on 2023-02-09, 20:40 IST
Select the Language for this assignment. 
1
#include <stdio.h>

int find_factorial(int k){
  int i,f = 1;
  for(i=1;i<=k;i++)
    f*=i;
  return f;
}

int main(){
    int n,k;
    scanf("%d",&n);

    for(int i=0;i<n;i++){
        scanf("%d",&k);
        printf("%d ", find_factorial(k));
    }
   
    return 0;
}
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 3 6
1 6 720
1 6 720 
Passed after ignoring Presentation Error




Private Test cases used for EvaluationStatus
Test Case 1
Passed
Test Case 2
Passed




Week 3: Assignment 3 - Question 2

Due on 2023-02-16, 23:59 IST
Write a C function to find the kth occurrence of an odd integer in a sequence of non-
negative integers, and then call your function from main.

Your function should be according to the following declaration:
int find_odd(int k);

Input
--------
You are given the input in two lines:

  • The first line contains a positive integer k.

  • In the second line, you will be given a sequence of numbers terminated with a -1.
You have to find the kth occurrence of an odd integer in the sequence.

Note:  The -1 is not part of the sequence.

Output
----------
If there are k odd numbers in the sequence, then output the kth occurrence of an odd number in the sequence, if present. If there are less than k odd numbers in the sequence, output -1.

Sample Input
------------------
2
1 4 3 6 5 2 3 4 1 -1

Sample Output
--------------------
3
Your last recorded submission was on 2023-02-10, 09:49 IST
Select the Language for this assignment. 
1
#include <stdio.h>
int find_odd(int k){
  int x0=1,cnt=0;
  while(x0!=-1)
    {
      scanf("%d",&x0);
      if(x0%2)
         ++cnt;
      if(cnt==k)
      {
        return x0;
        break;
      }
    }
  return -1;
}
int main(){
    int k;
    scanf("%d",&k);
    printf("%d",find_odd(k));
    return 0;
}
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
1 4 3 6 5 4 1 -1
3
3
Passed
Test Case 2
3
2 4 1 9 4 4 -1
-1
-1
Passed





Private Test cases used for EvaluationStatus
Test Case 1
Passed
Test Case 2
Passed
Test Case 3
Passed





Week 3: Assignment 3 - Question 3

Due on 2023-02-16, 23:59 IST
In this question, you have to output the "two moving average" of a sequence of non-negative numbers.

The two moving average is the sequence of averages of the last 2 entries.
For the first number, no average is output.

For example, if the sequence of numbers is 1,2,3,4,5
The 2-moving average is  (1+2)2,(2+3)2,(3+4)2,(4+5)2.

Input
-------
The input is a sequence of non-negative floating point numbers, terminated by a -1.
The -1 is not part of the sequence. There will be at least 3 numbers in the sequence.

Output
----------
You have to output the moving average of the sequence. The output should be printed correct to one digit after the decimal.
Hint : Use the format specifier "%.1f" inside printf.

Sample Input 1
---------------------
1 3 2 4 -1

Sample Output 1
-------------------------
2.0 2.5 3.0
Your last recorded submission was on 2023-02-10, 10:00 IST
Select the Language for this assignment. 
1
#include <stdio.h>
int main()
{
int x0,x1;
    scanf("%d",&x0);
    while(1)
    {
        scanf("%d",&x1);
        if(x1==-1)
          break;
        printf("%.1f ",((x0+x1)/2.0));
       
        x0=x1;
    }
  return 0;
}
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 2 4 -1
2.0 2.5 3.0
2.0 2.5 3.0 
Passed after ignoring Presentation Error





Private Test cases used for EvaluationStatus
Test Case 1
Passed
Test Case 2
Passed
Test Case 3
Passed
Test Case 4
Passed





























#include <stdio.h>

#include <stdlib.h>


struct DoublyLinkedList {

  int data;

  struct DoublyLinkedList * prev;

  struct DoublyLinkedList * next;

};

/*

--------------------------

Create a new node

--------------------------

*/

struct DoublyLinkedList * createNode(int n) {

  struct DoublyLinkedList * newNodeptr;

  newNodeptr = (struct DoublyLinkedList * )

  malloc(sizeof(struct DoublyLinkedList));

  newNodeptr -> data = n;

  newNodeptr -> prev = NULL;

  newNodeptr -> next = NULL;

  return newNodeptr;

}


/*

--------------------------------

add a node at the end of a doubly linked list.

Tailptr is the address of the pointer to the end of the current list.

After adding the node, tail points to the new node inserted.

--------------------------------

*/


void appendNode(struct DoublyLinkedList ** tailptr, int n) {

  struct DoublyLinkedList * newNode;

  newNode = createNode(n);

  newNode -> prev = * tailptr;

  ( * tailptr) -> next = newNode;

  * tailptr = newNode;

}


void initializeList(

  struct DoublyLinkedList ** headptr,

  struct DoublyLinkedList ** tailptr,

  int n) {

  struct DoublyLinkedList * newNode;

  newNode = createNode(n);

  * headptr = newNode;

  * tailptr = newNode;

  return;

}


void printList(

  struct DoublyLinkedList * head,

  struct DoublyLinkedList * tail) {

  struct DoublyLinkedList * curr = head;

  while (curr != NULL) {

    if (curr -> next != NULL)

      printf("%d,", curr -> data);

    else printf("%d", curr -> data);


    curr = curr -> next;

  }

  return;

}


/* additional functions used */


struct DoublyLinkedList * search(struct DoublyLinkedList * list, int id) {

  struct DoublyLinkedList *curr=list;

  while(curr!=NULL && curr->data!=id)

    curr=curr->next;

  return curr;

}


/*

COMPLETE THE FUNCTION GIVEN BELOW


--------------------------------

remove the node contains data with value n.

After removing the first node, we should reset head.

After removing the last node, we should reset tail.

--------------------------------

*/


void removeData(

  struct DoublyLinkedList * headptr,

  struct DoublyLinkedList * tailptr,

  int n) {

  

  struct DoublyLinkedList * e = search(headptr, n);

  if (e != NULL)

  { 

  if(e->prev==headptr)

      headptr=e->next;

      

    if(e->next==tailptr)

      tailptr=e->prev;

    

  e->next->prev=e->prev;

  e->prev->next=e->next;

  }

  free(e);

}


int main() {

  int n;

  int i = 0;

  int m;

  struct DoublyLinkedList * head, * tail;

  scanf("%d", & n);

  if (n <= 0) {

    return 0;

  }

  scanf("%d", & m);

  initializeList( & head, & tail, m);

  for (i = 1; i < n; i++) {

    /* read the remaining elements */

    scanf("%d", & m);

    appendNode( & tail, m);

  }


  scanf("%d", & n);

  removeData( head, tail, n);

  printList(head, tail);

  return 0;

}


















































































2 comments:

Keep your comments reader friendly. Be civil and respectful. No self-promotion or spam. Stick to the topic. Questions welcome.