**NPTEL Problem solving through Programming In C **the course aims to provide students to have basic knowledge of programming and problem solving by programming language C.

**Problem solving through Programming In C** is a MOOC course offered by IIT Madras on the NPTEL platform. This course helps the students to achieve knowledge of python programming language as well as data science which are a few of the most demanding and emerging technologies in the world. The course is developed by **Prof.Rengaswamy **is a professor of Chemical Engineering.

Final Year Undergraduates**INTENDED AUDIENCE:****Requirements/Prerequisites:**Knowledge of basic data science algorithms**INDUSTRY SUPPORT:**All industry/companies/organizations will recognize and value this course and recommend this for their employees and trainee programs.

**CRITERIA TO GET A CERTIFICATE**

Average assignment score = 25% of the average of the best 3 assignments out of the total 4 assignments given in the course.

Exam score = 75% of the proctored certification exam score out of 100

Final score = Average assignment score + Exam score

Students will be eligible for CERTIFICATE ONLY IF AVERAGE ASSIGNMENT SCORE >=10/25 AND EXAM SCORE >= 30/75. If any of the 2 criteria are not met, the student will not get the certificate even if the Final score >= 40/100.

## NPTEL Problem solving through Programming In C ONLINE PROGRAMMING TEST ANSWERS:-

Contents

- 1 NPTEL Problem solving through Programming In C ONLINE PROGRAMMING TEST ANSWERS:-
- 2 NPTEL Problem solving through Programming In C ONLINE PROGRAMMING TEST ANSWERS:-
- 3 NPTEL Problem solving through Programming In C ASSIGNMENT WEEK 12 ANSWERS:-
- 4 NPTEL Problem solving through Programming In C ASSIGNMENT WEEK 11 ANSWERS:-
- 5 NPTEL Problem solving through Programming In C ASSIGNMENT WEEK 10 ANSWERS:-
- 6 NPTEL Problem solving through Programming In C ASSIGNMENT WEEK 9 ANSWERS:-
- 7 NPTEL Problem solving through Programming In C ASSIGNMENT WEEK 4 ANSWERS:-
- 8 NPTEL Problem solving through Programming In C ASSIGNMENT WEEK 3 ANSWERS:-
- 9 NPTEL Problem solving through Programming In C ASSIGNMENT WEEK 1 ANSWERS:-
- 10 NPTEL Problem solving through Programming In C ASSIGNMENT WEEK 1 ANSWERS:-

**Q1.** Write a C program to find the sum of two 1D integer arrays ‘A’ and ‘B’ of same size and store the result in another array ‘C’, where the size of the array and the elements of the array are taken as input. Use dynamic memory allocation.

**Code:-**

```
A=(int*)malloc(n*sizeof(int));
for(int i=0;i<n;i++)
scanf("%d",A+i);
B=(int*)malloc(n*sizeof(int));
C=(int*)malloc(n*sizeof(int));
for(int i=0;i<n;i++)
scanf("%d",B+i);
for(int i=0;i<n;i++)
*(C+i)=*(A+i)+*(B+i);
```

**Q2.** Write a program which accepts number of rows and columns and prints a hollow rectangle using asterisk (*) symbol as given in the example.

**Code:-**

```
void print_rectangle(int n, int m)
{
int i, j;
for (i = 1; i <= n; i++)
{
for (j = 1; j <= m; j++)
{
if (i==1 || i==n || j==1 || j==m)
printf("*");
else
printf(" ");
}
printf("\n");
}
}
```

## NPTEL Problem solving through Programming In C ONLINE PROGRAMMING TEST ANSWERS:-

**Q1.** Write a C Program to find the sum of both the diagonals of a square matrix. The middle element should not be counted twice.

**Code:-**

```
for(i=0;i< n;i++)
{
for(j=0;j< n;j++)
{
if(i==j || i+j==n-1)
{
sum = sum + a[i][j];
}
}
}
```

**Q2.** Write a C Program to delete an element from a specified location of a 1D Array starting from array[0] as the 1st position, array[1] as second position and so on.

**Code:-**

```
for(i=pos-1;i<num-1;i++){
array[i]=array[i+1];
}
num--;
```

## NPTEL Problem solving through Programming In C ASSIGNMENT WEEK 12 ANSWERS:-

**Q1.** Write a program in C to find the factorial of a given number using pointers.

**Code:-**

**If you want help with the 17 Oct Online Programming test and help in the final exam take our membership for a better score in the exam read more here:- Final Exam Membership **

```
void findFact(int n, long int *f)
{
int i;
*f =1;
for(i=1;i<=n;i++)
*f=*f*i;
}
```

**Q2.** Write a C program to print the Record of the Student Merit wise. Here a structure variable is defined which contains student rollno, name and score.

**Code:-**

```
struct student temp;
int j;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1;j++)
{
if(s[j].score<s[j+1].score)
{
temp=s[j];
s[j]=s[j+1];
s[j+1]=temp;
}
}
}
```

**Q3.** Write a C program to store n elements using Dynamic Memory Allocation – calloc() and find the Largest element

**Code:-**

```
element = (float*) calloc(n, sizeof(float));
if(element == NULL)
{
printf("Error!!! memory not allocated.");
exit(0);
}
// Stores the number entered by the user.
int i;
for(i = 0; i < n; ++i)
{
scanf("%f", element + i);
}
// find the largest
for(i = 1; i < n; ++i)
{
if(*element < *(element + i))
*element = *(element + i);
}
printf("Largest element = %.2f\n", *element);
return 0;
}
```

**Q4.** Write a C program to add two distance given as input in feet and inches.

**Code:-**

```
sum.feet=d1.feet+d2.feet;
sum.inch=d1.inch+d2.inch;
/* If inch is greater than 12, converting it to feet. */
if (sum.inch>=12)
{
sum.inch=sum.inch-12;
++sum.feet;
}
```

## NPTEL Problem solving through Programming In C ASSIGNMENT WEEK 11 ANSWERS:-

**Q1.** The velocity of car at different time instance is give as:-

**Code:-**

**If you want help with the 17 Oct Online Programming test and help in the final exam take our membership for a better score in the exam read more here:- Final Exam Membership **

```
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]);
}
```

**Q2.** write a c program to find using trapezodial rule with 10 segments

**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;
}
```

**Q3.** write a c program to solve the following differential equation using runge-kutta method

**Code:-**

```
while(x0<xn)
{
m1=func(x0,y0);
m2=func((x0+h/2.0),(y0+m1*h/2));
m3=func((x0+h/2.0),(y0+m2*h/2));
m4=func((x0+h),(y0+m3*h));
m=((m1+2*m2+2*m3+m4)/6);
y0=y0+m*h;
x0=x0+h;
}
printf("y=%.4f",y0); // Final output
return 0;
}
float func(float x,float y)
{
float m;
m=(x*(x+1)-3*y*y*y)/10;
return m;
}
```

**Q4.** Write a C program to check whether the given input number is Prime number or not using recursion. So, the input is an integer and output should print whether the integer is prime or not. Note that you have to use recursion.

**Code:-**

**If you want help with the 17 Oct Online Programming test and help in the final exam take our membership for a better score in the exam read more here:- Final Exam Membership **

```
int checkPrime(int num, int i)
{
if (i == 1)
{
return 1;
}
else
{
if (num % i == 0)
{
return 0;
}
else
{
return checkPrime(num, i - 1);
}
}
}
```

## NPTEL Problem solving through Programming In C ASSIGNMENT WEEK 10 ANSWERS:-

**Q1.** Write a C program to find the root of the equation using bisection method for different values of allowable error of the root.

**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\n", 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);
}
```

**Q2.** Write a C program to find the root of the equation using Newton Raphson method, the maximum number of steps are taken as input.

**Code:-**

```
float h;
for (itr=1; itr<=maxmitr; itr++)
{
h=f(x0)/df(x0);
x1=x0-h;
x0=x1;
}
printf("Root = %8.6f\n", x1);
return 0;
}
float f(float x)
{
return x*x*x - 2*x - 3;
}
float df (float x)
{
return 3*x*x-2;
}
```

**Q3.** Write a C program to sort a given 1D array using pointer in ascending order.

**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;
}
}
}
```

**Q4.** Write a C program to sort a 1D array using pointer by applying Bubble sort technique.

**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;
}
}
}
}
```

## NPTEL Problem solving through Programming In C ASSIGNMENT WEEK 9 ANSWERS:-

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

**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;
}
```

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

**Code:-**

```
int low, high, mid;
low=0;
high=n-1;
mid=(low+high)/2;
while(low<=high)
{
if(array[mid]==search) {
printf("%d found at location %d.\n", search, mid+1);
return 0;
}
else if(array[mid]<search)
low=mid+1;
else
high=mid-1;
mid=(low+high)/2;
}
printf("Not found! %d isn't present in the list.\n", search);
return 0;
}
```

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

**Code:-**

```
int temp;
for(c=0;c<n/2;c++)
{
temp=array[c];
array[c]=array[n-c-1];
array[n-c-1]=temp;
}
```

**Q4.** Write a C program to marge two given sorted arrays (sorted in ascending order).

The code for input and output is already written. You have to write the merge function so that the merged array is displayed in ascending order. */

**Code:-**

```
void merge(int a[], int m, int b[], int n, int sorted [])
{
int i, j, k;
i=0; j=0; k=0;
while(i<m && j<n)
{
if(a[i]<=b[j])
{
sorted[k]=a[i];
i++;
}
else
{
sorted[k]=b[j];
j++;
}
k++;
}
while(i<m)
{
sorted[k]=a[i];
i++;
k++;
}
while(j<n)
{
sorted[k]=b[j];
j++;
k++;
}
}
```

## NPTEL Problem solving through Programming In C ASSIGNMENT WEEK 4 ANSWERS:-

**Q1.** Write a C Program to Find the Smallest Number among Three Numbers (integer values) using Nested IF-Else statement.

**Code:-**

**Q2.** 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).

**Code:-**

**Q3.** 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]

**Code:-**

**Q4.** Write a C program to find power of a number using while loops. The base number (>0) and exponent (>=0) is taken from the test cases.

**Code:-**

## NPTEL Problem solving through Programming In C ASSIGNMENT WEEK 3 ANSWERS:-

**Q1.** Write a C Program to calculates the area (floating point number with two decimal places) of a Circle given it’s radius (integer value). The value of Pi is 3.14.

**Code:-**

`area=PI*radius*radius;`

**Q2.** Write a C program to check if a given Number is zero or Positive or Negative Using if…else statement.

**Code:-**

```
if(number==0)
printf("The number is 0.");
else if(number<0)
printf("Negative number.");
else
printf("Positive number.");
}
```

**Q3.** Write a C program to check whether a given number (integer) is Even or Odd.

**Code:-**

```
if(number%2==0)
printf("%d is even.", number);
else
printf("%d is odd.", number);
}
```

**Q4.** Write a C Program to find the Largest Number (integer) among Three Numbers (integers) using IF and Logical && operator.

**Code:-**

if((n1>n2)&&(n1>n3))

printf(“%d is the largest number.”, n1);

else if(n2>n3)

printf(“%d is the largest number.”, n2);

else

printf(“%d is the largest number.”, n3);

}

## NPTEL Problem solving through Programming In C ASSIGNMENT WEEK 1 ANSWERS:-

**Q1.** Which of the following statements is correct?

**Answer:- ** **b) Application software is dependent on system software**

**Q2.** The execution of any C program is

**Answer:-** **a) Sequential**

**Q3.** A function is

**Answer:-** **d) All of the above**

**Q4.**Which of the following statements is correct?

**Answer:-** **d) All of the above**

**Q5.** We use the concept of ‘function’ for the following reason

**Answer:- d) All of the above**

**Q6.** If an integer needs two bytes of storage, then the maximum value of a signed integer in C would be

**Answer:- C**

**Q7.** What is the output?

**Answer:- c) 10**

**Q8.** What is the output of the following?

**Answer:- d) The output is 17.00**

**Q9.** What is the output of the following?

**Answer:- d) Compilation error**

**Q10.** What is the output of the following?

**Answer:- b) a=a%b; b=a+b; a=a/b;**

## NPTEL Problem solving through Programming In C ASSIGNMENT WEEK 1 ANSWERS:-

**Q1.** The intermediate medium for the communication between human and computer is called

**Answer:- C – **Compiler

**Q2.** Which one of the following statement is the most appropriate?

**Answer:- ** **B- ** Flowchart is basically a diagrammatic representation of the algorithm. Whereas in pseudo code normal English language is translated into the programming languages to be worked on.

**Q3.** A Pseudo-code is

**Answer:- ** **A –** An equivalent of a flow chart

**Q4.** Compiler helps in the translation from

**Answer:- ** **C –** High-level language to machine level language

**Q5.** Computer memory which is used to store programs and data currently being processed by CPU is

**Answer:- B – **RAM

**Q6.** X is an integer (X=1234). The print value of Y of the algorithm below is (note: ‘%’ is the modulo operator, which calculates the reminder and ‘/’ gives the quotient of a division operation)

**Answer:- C –** 4321

**Q7.** The flow chart calculates the HCF of two numbers a and b (where a is greater than or equal to b). Which of the following conditions need to be put inside the blanks 1 and 2 to calculate the HCF? (Hint: Use Euclidian Algorithm for finding HCF of two numbers)

**Answer:- D – **r=0 a=b and b=r

**Q8.** The input N from the user is 6. The output of the following algorithm is

**Answer:- ** **B – **720

**Q9.** What is algorithm?

**Answer:- ** **A –** A process or set of rules to be followed in calculations or other problem-solving operations, especially by a computer.

**Q10.** The ALU unit of computer

**Answer:- C – **Can perform both arithmetic and logical operations

**Also check :- Internship oppurtinites**

**Note:- We do not claim 100% surety of answers, these answers are based on our sole knowledge and by posting these answers we are just trying to help students, so we urge do you assignment own your own.**