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.
Due date on 2026-10-01, 23:59 IST
Complete Program
#include <stdio.h>
float fun(float x);
void bisection(float *x, float a, float b, int *itr);
int main()
{
int itr = 0, maxmitr = 10;
float x, a = 1.0, b = 2.0, allerr, x1;
scanf("%f", &allerr);
bisection(&x, a, b, &itr);
// --- START OF SOLUTION CODE ---
do
{
if (fun(a) * fun(x) < 0)
b = x;
else
a = x;
bisection(&x1, a, b, &itr);
if (((x1 - x) < 0 && -(x1 - x) < allerr) || ((x1 - x) > 0 && (x1 - x) < allerr))
{
printf("Root = %1.4f", x1);
return 0;
}
x = x1;
} while (itr < maxmitr);
return 1;
}
float fun(float x)
{
return (2 * x * x * x - 3 * x - 5);
}
void bisection(float *x, float a, float b, int *itr)
/* this function performs and prints the result of one iteration */
{
*x = (a + b) / 2;
++(*itr);
}
// --- END OF SOLUTION CODE ---
Code Snippet to Paste in the Editor
// --- START OF SOLUTION CODE ---
do
{
if (fun(a) * fun(x) < 0)
b = x;
else
a = x;
bisection(&x1, a, b, &itr);
if (((x1 - x) < 0 && -(x1 - x) < allerr) || ((x1 - x) > 0 && (x1 - x) < allerr))
{
printf("Root = %1.4f", x1);
return 0;
}
x = x1;
} while (itr < maxmitr);
return 1;
}
float fun(float x)
{
return (2 * x * x * x - 3 * x - 5);
}
void bisection(float *x, float a, float b, int *itr)
/* this function performs and prints the result of one iteration */
{
*x = (a + b) / 2;
++(*itr);
}
// --- 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-10-01, 23:59 IST
Complete Program
#include <stdio.h>
float f(float x);
float df(float x);
int main()
{
int itr, maxmitr;
float x0, x1;
scanf("%f %d", &x0, &maxmitr);
// --- START OF SOLUTION CODE ---
float h;
for (itr = 1; itr <= maxmitr; itr++)
{
h = f(x0) / df(x0);
x1 = x0 - h;
x0 = x1;
}
printf("Root = %8.6f", x1);
return 0;
}
float f(float x)
{
return x * x * x - 2 * x - 3;
}
float df(float x)
{
return 3 * x * x - 2;
}
// --- END OF SOLUTION CODE ---
Code Snippet to Paste in the Editor
// --- START OF SOLUTION CODE ---
float h;
for (itr = 1; itr <= maxmitr; itr++)
{
h = f(x0) / df(x0);
x1 = x0 - h;
x0 = x1;
}
printf("Root = %8.6f", x1);
return 0;
}
float f(float x)
{
return x * x * x - 2 * x - 3;
}
float df(float x)
{
return 3 * x * x - 2;
}
// --- 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-10-01, 23:59 IST
Complete Program
#include <stdio.h>
int main()
{
int a[100], n, i;
scanf("%d", &n);
for (i = 0; i < n; i++)
{
scanf("%d", a + i);
}
// --- START OF SOLUTION CODE ---
int j, t;
for (i = 0; i < (n - 1); i++)
{
for (j = i + 1; j < n; j++)
{
if (*(a + i) > *(a + j))
{
t = *(a + i);
*(a + i) = *(a + j);
*(a + j) = t;
}
}
}
// --- END OF SOLUTION CODE ---
for (i = 0; i < n; i++)
{
printf("%d\n", *(a + i));
}
return 0;
}
Code Snippet to Paste in the Editor
// --- START OF SOLUTION CODE ---
int j, t;
for (i = 0; i < (n - 1); i++)
{
for (j = i + 1; j < n; j++)
{
if (*(a + i) > *(a + j))
{
t = *(a + i);
*(a + i) = *(a + j);
*(a + j) = t;
}
}
}
// --- 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-10-01, 23:59 IST
Complete Program
#include<stdio.h>
void sort(int *a, int n);
int main()
{
int a[20];
int n,i;
scanf("%d",&n); // Enter number of elements to sort is taken from test case data
for(i=0;i<n;i++)
{
scanf("%d",&a[i]); // The elements of the array is taken from the test data
}
sort(a, n); // Calling the sorting function
//Printing the sorted array
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
return 0;
}
// --- START OF SOLUTION CODE ---
void sort(int *a, int n)
{
int i, temp, j;
for (i = 1; i < n; i++)
{
for (j = 0; j < n - i; j++)
{
if (*(a + j) > *(a + j + 1))
{
temp = *(a + j);
*(a + j) = *(a + j + 1);
*(a + j + 1) = temp;
}
}
}
}
// --- END OF SOLUTION CODE ---
Code Snippet to Paste in the Editor
// --- START OF SOLUTION CODE ---
void sort(int *a, int n)
{
int i, temp, j;
for (i = 1; i < n; i++)
{
for (j = 0; j < n - i; j++)
{
if (*(a + j) > *(a + j + 1))
{
temp = *(a + j);
*(a + j) = *(a + j + 1);
*(a + j + 1) = temp;
}
}
}
}
// --- 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-10-08, 23:59 IST
Complete Program
#include <stdio.h>
#define MAX 100
char *reverse(char[]);
int main()
{
char str[MAX], *rev;
//printf("Enter a String: ");
scanf("%s", str);
rev = reverse(str); //You have to write this function
printf("The reversed string is : %s\n", rev);
return 0;
}
// --- START OF SOLUTION CODE ---
char* reverse(char str[])
{
static int i= 0;
static char rev[MAX];
if (*str)
{
reverse(str+1);
rev[i++]= *str;
}
return rev;
}
// --- END OF SOLUTION CODE ---
Code Snippet to Paste in the Editor
// --- START OF SOLUTION CODE ---
char* reverse(char str[])
{
static int i= 0;
static char rev[MAX];
if (*str)
{
reverse(str+1);
rev[i++]= *str;
}
return rev;
}
// --- 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-10-08, 23:59 IST
Q.

Complete Program
#include<stdio.h>
int main()
{
float t[100]={10,15,18,22,30}, v[100]={22,26,35,48,68};
float a; //Value of the t to find the respective value of v(t)
scanf("%f", &a); // This will be taken from test cases
// --- START OF SOLUTION CODE ---
int i,j;
float b, c, k =0;
for(i=0; i<5; i++)
{
b=1;
c=1;
for(j=0; j<5; j++)
{
if(j!=i)
{
b=b*(a-t[j]);
c=c*(t[i]-t[j]);
}
}
k=k+((b/c)*v[i]);
}
// --- END OF SOLUTION CODE ---
printf("The respective value of the variable v is: %.2f", k);
return 0;
}
Code Snippet to Paste in the Editor
// --- START OF SOLUTION CODE ---
int i,j;
float b, c, k =0;
for(i=0; i<5; i++)
{
b=1;
c=1;
for(j=0; j<5; j++)
{
if(j!=i)
{
b=b*(a-t[j]);
c=c*(t[i]-t[j]);
}
}
k=k+((b/c)*v[i]);
}
// --- 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-10-08, 23:59 IST
Complete Program
#include<stdio.h>
float func(float x);
int main()
{
int n=10; //Taking n=10 sub intervals
float a,b,integral; //integral is the integration result
scanf("%f",&a); // initial limit taken from test case
scanf("%f",&b); // Final limit taken from test case
//Use the printf statement as printf("The integral is: %0.6f\n",integral);
// --- START OF SOLUTION CODE ---
int i;
float h,x, sum=0;
if(b>a)
h=(b-a)/n;
else
h=-(b-a)/n;
for(i=1;i<n;i++){
x=a+i*h;
sum=sum+func(x);
}
integral=(h/2)*(func(a)+func(b)+2*sum);
printf("The integral is: %0.6f\n",integral);
return 0;
}
float func(float x)
{
return x*x;
}
// --- END OF SOLUTION CODE ---
Code Snippet to Paste in the Editor
// --- START OF SOLUTION CODE ---
int i;
float h,x, sum=0;
if(b>a)
h=(b-a)/n;
else
h=-(b-a)/n;
for(i=1;i<n;i++){
x=a+i*h;
sum=sum+func(x);
}
integral=(h/2)*(func(a)+func(b)+2*sum);
printf("The integral is: %0.6f\n",integral);
return 0;
}
float func(float x)
{
return x*x;
}
// --- 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-10-08, 23:59 IST
Complete Program
#include <stdio.h>
int checkPrime(int, int); //Function to check prime or not
int main()
{
int num, check;
scanf("%d", &num); //The number is taken from test case data
check = checkPrime(num, num/2);
if (check == 1)
{
printf("%d is a prime number\n", num);
}
else
{
printf("%d is not a prime number\n", num);
}
return 0;
}
// --- START OF SOLUTION CODE ---
int checkPrime(int num, int i)
{
if (i == 1)
{
return 1;
}
else
{
if (num % i == 0)
{
return 0;
}
else
{
return checkPrime(num, i - 1);
}
}
}
// --- END OF SOLUTION CODE ---
Code Snippet to Paste in the Editor
// --- START OF SOLUTION CODE ---
int checkPrime(int num, int i)
{
if (i == 1)
{
return 1;
}
else
{
if (num % i == 0)
{
return 0;
}
else
{
return checkPrime(num, i - 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.

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.