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 | July-2026 | Week 9 to 12

 

  Please scroll down for latest Programs   ðŸ‘‡ 


Code compiled and tested successfully!



Problem Solving through Programming in C - Week 9 to 12

NPTEL





Week-09 Program-01

Due date on 2026-09-24, 23:59 IST

Your last recorded submission was on 2026-09-16, 13:14 IST.

Q.

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.
 */

Complete Program

C
#include <stdio.h>
int main()
{
   int array[100], search, n, count = 0;
   //"search" is the key element to search and 'n' is the total number of element of the array
   // "count" is to store total number of elements
 scanf("%d", &n); //Number of elements is taken from test case
 int c;
   for (c = 0; c < n; c++)
      scanf("%d", &array[c]);
   scanf("%d", &search); // The element to search is taken from test case
/* Use the printf statements as below:
"%d is present at location %d.\n"  for each locations
"%d is not present in the array.\n" if the element is not found in the list
"%d is present %d times in the array.\n"
*/

   // --- START OF SOLUTION CODE ---
   for (c = 0; c < n; c++)
   {
      if (array[c] == search)
      {
         printf("%d is present at location %d.\n", search, c + 1);
         count++;
      }
   }
   if (count == 0)
      printf("%d is not present in the array.\n", search);
   else
      printf("%d is present %d times in the array.\n", search, count);

   return 0;
}
// --- END OF SOLUTION CODE ---

Code Snippet to Paste in the Editor

C
   // --- START OF SOLUTION CODE ---
   for (c = 0; c < n; c++)
   {
      if (array[c] == search)
      {
         printf("%d is present at location %d.\n", search, c + 1);
         count++;
      }
   }
   if (count == 0)
      printf("%d is not present in the array.\n", search);
   else
      printf("%d is present %d times in the array.\n", search, count);

   return 0;
}
// --- END OF SOLUTION CODE ---
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.

Evaluation Results

CompilationSuccessful|Public Test Cases: 2/2 Passed

Note: These tests may not be considered while scoring.

Public Test Cases

Test Case
Input
Expected Output
Output
Status
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.\n
Presentation Error
Test Case 2

5 67 80 45 97 100 50

50 is not present in the array.
50 is not present in the array.\n
Presentation Error


Week-09 Program-02

Due date on 2026-09-24, 23:59 IST

Your last recorded submission was on 2026-09-16, 13:17 IST.

Q.

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.

Complete Program

C
#include <stdio.h>
int linear_search(int[], int, int);
int main()
{
   int array[100], search, c, n, position;
   /* search - element to search, c - counter, n - number of elements in array,
   position - The position in which the element is first found in the list. */
    scanf("%d", &n); // Number of elements in the array is read from the test case data
    for (c = 0; c < n; c++)
    scanf("%d", &array[c]); //Elements of array is read from the test data
    scanf("%d", &search);  //Element to search is read from the test case data
   /* Use the following in the printf statement to print the output
   printf("%d is not present in the array.\n", search);
   printf("%d is present at location %d.\n", search, position+1); //As array[0] has the position 1
   */

   // --- START OF SOLUTION CODE ---
   position = linear_search(array, n, search);
   if (position == -1)
      printf("%d is not present in the array.\n", search);
   else
      printf("%d is present at location %d.\n", search, position + 1);
   return 0;
}

int linear_search(int a[], int n, int find)
{
   int c;
   for (c = 0; c < n; c++)
   {
      if (a[c] == find)
         return c;
   }
   return -1;
}
// --- END OF SOLUTION CODE ---

Code Snippet to Paste in the Editor

C
   // --- START OF SOLUTION CODE ---
   position = linear_search(array, n, search);
   if (position == -1)
      printf("%d is not present in the array.\n", search);
   else
      printf("%d is present at location %d.\n", search, position + 1);
   return 0;
}

int linear_search(int a[], int n, int find)
{
   int c;
   for (c = 0; c < n; c++)
   {
      if (a[c] == find)
         return c;
   }
   return -1;
}
// --- END OF SOLUTION CODE ---
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.

Evaluation Results

CompilationSuccessful|Public Test Cases: 2/2 Passed

Note: These tests may not be considered while scoring.

Public Test Cases

Test Case
Input
Expected Output
Output
Status
Test Case 1

5 78 90 34 54 98 90

90 is present at location 2.
90 is present at location 2.\n
Presentation Error
Test Case 2

6 30 40 50 20 90 60 90

90 is present at location 5.
90 is present at location 5.\n
Presentation Error



Week-09 Program-03

Due date on 2026-09-24, 23:59 IST

Your last recorded submission was on 2026-09-16, 13:20 IST.

Q.

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.

Complete Program

C
#include <stdio.h>
int main()
{
 int c, n, search,
 array[100];
 scanf("%d",&n); //number of elements in the array
 for (c = 0; c < n; c++)
 scanf("%d",&array[c]);
 scanf("%d", &search); //The element to search is read from test case.
/* Use the printf statements as below:
 printf("%d found at location %d.", search, variable_name);
 printf("Not found! %d isn't present in the list.", search);
*/

   // --- START OF SOLUTION CODE ---
   int first, last, middle;
   first = 0;
   last = n - 1;
   middle = (first + last) / 2;

   while (first <= last) {
      if (array[middle] < search)
         first = middle + 1;
      else if (array[middle] == search) {
         printf("%d found at location %d.", search, middle + 1);
         break;
      }
      else
         last = middle - 1;

      middle = (first + last) / 2;
   }
   if (first > last)
      printf("Not found! %d isn't present in the list.", search);

   return 0;
}
// --- END OF SOLUTION CODE ---

Code Snippet to Paste in the Editor

C
   // --- START OF SOLUTION CODE ---
   int first, last, middle;
   first = 0;
   last = n - 1;
   middle = (first + last) / 2;

   while (first <= last) {
      if (array[middle] < search)
         first = middle + 1;
      else if (array[middle] == search) {
         printf("%d found at location %d.", search, middle + 1);
         break;
      }
      else
         last = middle - 1;

      middle = (first + last) / 2;
   }
   if (first > last)
      printf("Not found! %d isn't present in the list.", search);

   return 0;
}
// --- END OF SOLUTION CODE ---
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.

Evaluation Results

CompilationSuccessful|Public Test Cases: 2/2 Passed

Note: These tests may not be considered while scoring.

Public Test Cases

Test Case
Input
Expected Output
Output
Status
Test Case 1

5 10 20 30 40 50 40

40 found at location 4.
40 found at location 4.\n
Presentation Error
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.\n
Presentation Error


Week-09 Program-04

Due date on 2026-09-24, 23:59 IST

Your last recorded submission was on 2026-09-16, 13:23 IST.

Q.

Write a C program to reverse an array by swapping the elements and without using any new array.

Complete Program

C
#include <stdio.h>
int main() {
  int array[100], n, c;
  scanf("%d", &n); // n is number of elements in the array.
  for (c = 0; c < n; c++) {
    scanf("%d", &array[c]);
  }

  // --- START OF SOLUTION CODE ---
  int temp, end;
  end = n - 1;
  for (c = 0; c < n / 2; c++) {
    temp = array[c];
    array[c] = array[end];
    array[end] = temp;

    end--;
  }
  // --- END OF SOLUTION CODE ---

  printf("Reversed array elements are:\n");
  for (c = 0; c < n; c++) {
    printf("%d\n", array[c]);
  }
  return 0;
}

Code Snippet to Paste in the Editor

C
  // --- START OF SOLUTION CODE ---
  int temp, end;
  end = n - 1;
  for (c = 0; c < n / 2; c++) {
    temp = array[c];
    array[c] = array[end];
    array[end] = temp;

    end--;
  }
  // --- END OF SOLUTION CODE ---
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.

Evaluation Results

CompilationSuccessful|Public Test Cases: 2/2 Passed

Note: These tests may not be considered while scoring.

Public Test Cases

Test Case
Input
Expected Output
Output
Status
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
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
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.