Please scroll down for latest Programs 👇
Code compiled and tested successfully!
Problem Solving through Programming in C - Week 9 to 12
NPTEL
Due date on 2026-09-24, 23:59 IST
Complete Program
#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
// --- 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 ---
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
Note: These tests may not be considered while scoring.
Due date on 2026-09-24, 23:59 IST
Complete Program
#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
// --- 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 ---
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
Note: These tests may not be considered while scoring.
Due date on 2026-09-24, 23:59 IST
Complete Program
#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
// --- 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 ---
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
Note: These tests may not be considered while scoring.
Due date on 2026-09-24, 23:59 IST
Complete Program
#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
// --- 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 ---
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
Note: These tests may not be considered while scoring.