NPTEL » Problem solving through Programming In C Assignment 2021

problem solving in c

NPTEL Problem solving through Programming In C With the growth of Information and Communication Technology, there is a need to develop large and complex software.

Problem-solving through Programming In C is a MOOC based course that is 12 weeks in duration and can fulfill the criteria of 4 credits in a year. You can visit the NPTEL SWAYAM platform and register yourself for the course. The course is developed by Prof. Anupam Basu is a Professor in the Dept. of Computer Science & Engineering, IIT Kharagpur, and has been an active researcher in the areas of Cognitive and Intelligent Systems, Embedded Systems, and Language Processing, Presently he is acting as the Chairman and Head of the Center for Educational Technology, IIT Kharagpur.

Problem Solving Through Programming in C 2021 Details:-

Contents

  1. Who Can Join: This is an elective course. Intended for senior UG/PG students. BE/ME/MS/PhD
  2. Requirements/Prerequisites: We will assume that the students know to program for some of the assignments. If the students have done introductory courses on probability theory and linear algebra it would be helpful. We will review some of the basic topics in the first two weeks as well.
  3. INDUSTRY SUPPORT: All IT Industries

NPTEL Problem Solving Through Programming in C Online Programming Test Answers:-

Q1. Write a C code to print hollow left arrow using star pattern. The number of coloums (N) is taken from test case. For example for N = 6 the output will be

Code:-

QUIZXP TELEGRAM

Q2. Complete the C program which takes size of the array and array elements as input and puts the prime and composite elements of the array in two separate arrays (according to their occurrence in the input array) .

Code:-

Problem Solving In C Programming Assignment Week 12 Answers:-

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

Code:-

JULY 2021 SESSION ANSWERS ARE HERE :- PROBLEM SOLVING THROUGH PROGRAMMING IN C ANSWER

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:-


JULY 2021 SESSION ANSWERS ARE HERE :- PROBLEM SOLVING THROUGH PROGRAMMING IN C ANSWER

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

Code:-


JULY 2021 SESSION ANSWERS ARE HERE :- PROBLEM SOLVING THROUGH PROGRAMMING IN C ANSWER

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

Code:-

Q5. 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.
In the Test case the input is given as follows

Code:-

Problem Solving In C Programming Assignment Week 11 Answers:-

Q1.

Code:-


JULY 2021 SESSION ANSWERS ARE HERE :- PROBLEM SOLVING THROUGH PROGRAMMING IN C ANSWER

Q2.

Code:-


JULY 2021 SESSION ANSWERS ARE HERE :- PROBLEM SOLVING THROUGH PROGRAMMING IN C ANSWER

Q3.

Code:-

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:-

Q5. Write a C program to reverse a word using Recursion. Input to the program is a string that is to be taken from the user and output is reverse of the input word. Note that you have to use recursion.

Code:-

char* reverse(char str[])
{
    static int i= 0;
    static char rev[MAX];
    if (*str)
    {
        reverse(str+1);
        rev[i++]= *str;
    }
    return rev;
}

Problem Solving In C Programming 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:-


JULY 2021 SESSION ANSWERS ARE HERE :- PROBLEM SOLVING THROUGH PROGRAMMING IN C ANSWER

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:-


JULY 2021 SESSION ANSWERS ARE HERE :- PROBLEM SOLVING THROUGH PROGRAMMING IN C ANSWER

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

Code:-


JULY 2021 SESSION ANSWERS ARE HERE :- PROBLEM SOLVING THROUGH PROGRAMMING IN C ANSWER

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

Code:-

Q5. Write a C code to check if a 3 x 3 matrix is invertible. A matrix is not invertible if its determinant is 0.

Code:-

determinant = a[0][0] * ((a[1][1]*a[2][2]) - (a[2][1]*a[1][2])) -a[0][1] * (a[1][0]
   * a[2][2] - a[2][0] * a[1][2]) + a[0][2] * (a[1][0] * a[2][1] - a[2][0] * a[1][1]);
   if ( determinant == 0)
       printf("The given matrix is not invertible");
   else
       printf("The given matrix is invertible");
   return 0;
}

Problem Solving In C Programming Assignment Week 9 Answers:-

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

Code:-

for(int i=0;i<n;i++)
{
  if (array[i]==search)
  {
    printf("%d is present at location %d.\n",search ,i+1);
    count++;
    
  }
  
}
  if (count>0)
    printf("%d is present %d times in the array.\n",search ,count );
  else
    printf("%d is not present in the array.\n",search );
return 0;
}

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

int count=0;
for(int i=0;i<n;i++)
{
  if (array[i]==search)
  { 
    position=i;
    count++;
    printf("%d is present at location %d.\n", search, position+1);
  }
}

if (count==0)
     printf("%d is not present in the array.\n", search);
return 0;
}
QUIZXP TELEGRAM

Q3. 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 first,last,mid,flag;
first=0;
    last=n-1;
 
    while(first<=last)
    {
        mid=(first+last)/2;
 
        if(search ==array[mid]){
            flag=1;
            break;
        }
        else
            if(search >array[mid])
                first=mid+1;
            else
                last=mid-1;
    }
 
    if(flag==1)
       printf("%d found at location %d.\n", search, mid+1);
    else
       printf("Not found! %d isn't present in the list.\n", search);
    return 0;
}
QUIZXP TELEGRAM

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

Code:-

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

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

Code:-

void merge(int a[], int m, int b[], int n, int sorted[]) {
  int i, j, k;

  j = k = 0;

  for (i = 0; i < m + n;) {
    if (j < m && k < n) {
      if (a[j] < b[k]) {
        sorted[i] = a[j];
        j++;
      }
      else {
        sorted[i] = b[k];
        k++;
      }
      i++;
    }
    else if (j == m) {
      for (; i < m + n;) {
        sorted[i] = b[k];
        k++;
        i++;
      }
    }
    else {
      for (; i < m + n;) {
        sorted[i] = a[j];
        j++;
        i++;
      }
    }
  }
}

Problem Solving In C Programming Assignment Week 8 Answers:-

Week-08 Program-01:- Write a C Program to find HCF of 4 given numbers using recursive function

Code:-

int HCF(int a, int b)
{
    while (a != b)
    {
        if (a > b)
        {
            return HCF(a - b, b);
        }
        else
        {
            return HCF(a, b - a);
        }
    }
    return a;
}

Week-08 Program-02:- Write a C Program to find power of a given number using recursion. The number and the power to be calculated is taken from test case

Code:-

long power(int base, int a) {
    if (a != 0)
        return (base * power(base, a - 1));
    else
        return 1;
}

Week-08 Program-03:- Write a C Program to print Binary Equivalent of an Integer using Recursion

Code:-

int binary_conversion(int num)
{
    if (num == 0)
    {
        return 0;
    }
    else
    {
        return (num % 2) + 10 * binary_conversion(num / 2);
    }
}
quizxp telegram

QUIZ SOLUTION Problem Solving In C Quiz Assignment Week 8 Answers

QUIZ SOLUTION Problem Solving In C Quiz Assignment Week 7 Answers

Week-08 Program-04:- Write a C Program to reverse a given word using function. e.g. INDIA should be printed as AIDNI

Code:-

int size = strlen(str1);
    reverse(str1, 0, size - 1);
    printf("The string after reversing is: %s", str1);
    return 0;
}
 
void reverse(char str1[], int index, int size)
{
    char temp;
    temp = str1[index];
    str1[index] = str1[size - index];
    str1[size - index] = temp;
    if (index == size / 2)
    {
        return;
    }
    reverse(str1, index + 1, size);
}
quizxp telegram

Week-08 Program-05:-Write a C program to print a triangle of prime numbers upto given number of lines of the triangle.
e.g If number of lines is 3 the triangle will be
2 3 5 7 11 13  

Code:-

int i, j;
int counter = 2;
for (i = 1; i <= lines; i++) {
      for (j = 1; j <= i; j++) {
       
        while(!prime(counter)){
            counter++;
 }
        printf("%d\t", counter);
        counter++;
      }
      printf("\n");
   }
   return(0);
}


int prime(int num) {
   int i, isPrime = 1;
   for (i = 2; i <= (num/2); i++) {
      if (num % i == 0){
         isPrime = 0;
         break;
      }
   }
   if (isPrime==1 || num==2)
      return 1;
   else
      return 0;
}

Problem Solving In C Programming Assignment Week 7 Answers:-

Week-07 Program-01:- Write a C Program to Count Number of Uppercase and Lowercase Letters in a given string. The given string may be a word or a sentence.

Code:-

for(int counter=0;ch[counter]!='\0';counter++){
 
        if(ch[counter]>='A' && ch[counter]<='Z')
            upper++;
        else if(ch[counter]>='a' && ch[counter]<='z')
            lower++;   
    }   

Week-07 Program-02:- Write a C program to find the sum of all elements of each row of a matrix.

Code:-

for(i=0;i< r;i++) //Accepts the matrix elements from the test case data
    {
        int sum=0;
        for(j=0;j< c;j++)
        {
         sum=sum+matrix[i][j] ; 
        }
     printf("%d\n",sum);
    }
return 0;
}
quizxp telegram

Week-07 Program-03:- Write a C program to find subtraction of two matrices i.e. matrix_A – matrix_B=matrix_C.
If the given martix are
2 3 5 and 1 5 2 Then the output will be 1 -2 3
4 5 6 2 3 4 2 2 2
6 5 7 3 3 4 3 2 3
The elements of the output matrix are separated by one blank space

Code:-

for(i=0; i<row; i++)
    {
        for(j=0; j<col; j++)
        {
            printf("%d ", matrix_A[i][j]-matrix_B[i][j]);
        }
  printf("\n");
    }
return 0;
}

Week-07 Program-04:- Write a C program to print lower triangle of a square matrix.
For example the output of a given matrix
 2 3 4 will be 2 0 0
5 6 7             5 6 0
4 5 6             4 5 6

Code:-

for(i=0;i< r;i++) 
    {
        for(j=0;j<i+1; j++)
        {
            printf("%d ", matrix[i][j]);

        }
        for(j=1+i;j<r;j++)
        {
        	printf("%d ",0);
		}
        printf("\n");
    }
return 0;
}

Week-07 Program-05:- Write a C program to print Largest and Smallest Word from a given sentence. If there are two or more words of same length, then the first one is considered. A single letter in the sentence is also consider as a word.

Code:-

int k,max,min,i,j,a,maxIndex,minIndex;
while(str[k]!='\0')
    {
        j=0;
        while(str[k]!=' '&&str[k]!='\0'&&str[k]!='.')
        {
            substr[i][j]=str[k];
            k++;
            j++;
        }
        substr[i][j]='\0';
        i++;
        if(str[k]!='\0')
        {
            k++;
        }        
    }
    int len=i;
    max=strlen(substr[0]);
    min=strlen(substr[0]);
    
    for(i=0;i<len;i++)
    {
       a=strlen(substr[i]);
       if(a>max)
        {
            max=a;
            maxIndex=i;
        }
        if(a<min)
        {
            min=a;
            minIndex=i;
        }
    }    
 
  printf("Largest Word is: %s\nSmallest word is: %s\n",substr[maxIndex],substr[minIndex]);
    
}

Problem Solving In C Programming Assignment Week 6 Answers:-

Week-06 Program-01:- Write a C Program to find Largest Element of an Integer Array.
 Here the number of elements in the array ‘n’ and the elements of the array is read from the test data.
 Use the printf statement given below to print the largest element. printf(“Largest element = %d”, largest);

Code:-

largest=arr[0];
for(i=0;i<n;i++)
{
  if(largest<arr[i])
    largest=arr[i];
}
printf("Largest element = %d", largest);

return 0;
}

Week-06 Program-02:- Write a C Program to print the array elements in reverse order (Not reverse sorted order. Just the last element will become first element, second last element will become second element and so on)
Here the size of the array, ‘n’ and the array elements is accepted from the test case data.
The last part i.e. printing the array is also written. You have to complete the program so that it prints in the reverse order.

Code:-

int array[20];
int y=0;
for(i=n-1;i>=0;i--)
{
  array[y]=arr[i];
  y++;
}
for(i=0;i<n;i++)
  arr[i]=array[i];
quizxp telegram

Week-06 Program-03:- Write a C program to read Two One Dimensional Arrays of same data type (integer type) and merge them into another One Dimensional Array of same type.

Code:-

size=n1+n2;
for (i = 0; i < n1; i++)
  array_new[i]=arr1[i];
int t=n1;
for (i = 0; i < n2; i++)
{ array_new[t]=arr2[i];
  t=t+1;
  
}

Week-06 Program-04:- Write a C Program to delete duplicate elements from an array of integers.

Code:-

int temp[50],count=0;
for (int i = 0; i < size; i++)
  {
    int j;
    for (j = 0; j < count; j++)
    {
      if (array[i] == temp[j])
        break;
    }
    if (j == count)
    {
      temp[count] = array[i];
      count++;
    }
  }
size=count;
for(i=0;i<size;i++)
  array[i]=temp[i];

Week-06 Program-05:- C Program to delete an element from a specified location of an Array starting from array[0] as the 1st position, array[1] as second position and so on.

Code:-

int c;
for (c = pos - 1; c < num - 1; c++)
         array[c] = array[c+1];

num=num-1;

Problem Solving In C Programming Assignment Week 5 Answers:-

Week-05 Program-01:- Write a C program to check whether a given number (N) is a perfect number or not?
[Perfect Number – A perfect number is a positive integer number which is equals to the sum of its proper positive divisors.
For example 6 is a perfect number because its proper divisors are 1, 2, 3 and it’s sum is equals to 6.]

Code:-

int sum=0;
int i;
for(i=1;i<N;i++)
{
  if(N%i==0)
    sum=sum+i;

}
if (sum==N)
  printf("%d is a perfect number.\n",N);
else
  printf("%d is not a perfect number.\n",N);
return 0;
}

Week-05 Program-02:- Write a C program to count total number of digits of an Integer number (N).

Code:-

int count=0;
int n;
n=N;
while(N>0)
{N=N/10;
 count=count+1;
}
printf("The number %d contains %d digits.",n,count);
return 0;
}

Problem Solving In C Quiz Assignment Week 5 Answers

Week-05 Program-03:- Write a C program to check whether the given number(N) can be expressed as Power of Two (2) or not. For example, 8 can be expressed as 2^3.

Code:-

int power,i,j;
power =2;
for(i=1;i<N/2;i++)
{power=power*2;
  if (power==N)
    break;
}
if (power==N)
  printf("%d is a number that can be expressed as power of 2.",N);
else
  printf("%d cannot be expressed as power of 2.",N);
  
  
return 0;
}

Week-05 Program-04:- Write a C program to find sum of following series where the value of N is taken as input
1 + 1/2 + 1/3 + 1/4 + 1/5 + .. 1/N

Code:-

int i;
for(i=1;i<=N;i++)
sum=sum+(1.0/i);
printf("Sum of the series is: %.2f\n",sum);

return 0;
}

Week-05 Program-05:- Write a c program to print the following Pyramid pattern upto Nth row where n is taken as input.

Code:-

int i,j;
for(i=N;i>=1;i--)
{
  for(j=1;j<=i;j++)
    printf("*");
  printf("\n");
}
  
return 0;
}

Problem Solving In C Programming Assignment Week 4 Answers:-

Week-04 Program-01:- Write a C Program to Find the Smallest Number among Three Numbers (integer values) using Nested IF-Else statement.

Code:-

	if(n1 < n3)
	{
		if(n1 < n2)
		{
			printf("%d is the smallest number.", n1);
		}
		else
		{
			printf("%d is the smallest number.", n2);
		}
	}
	else if(n2 < n3)
		printf("%d is the smallest nummber.", n2);
	else
		printf("%d is the smallest number.", n3);
}

Week-04 Program-02 – 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:-

if(a<(b+c)&&b<(a+c)&&c<(a+b))
    {
        if(a==b&&a==c&&b==c)
        printf("Equilateral Triangle");
        else if(a==b||a==c||b==c)
        printf("Isosceles Triangle");
        else if((a*a)==(b*b)+(c*c)||(b*b)==(a*a)+(c*c)||(c*c)==(a*a)+(b*b))
        printf("Right-angle Triangle");
        else if(a!=b&&a!=c&&b!=c)
        printf("Scalene Triangle");
    }
    else
    printf("Triangle is not possible");
    return 0;
}

Week-04 Program-03 – Write a program to find the factorial of a given number using while loop.

Code:-

int i=1;
fact = 1;
while(i<=n)
    {
        fact*=i;
        i++;
    }
    printf("The Factorial of %d is : %ld ",n,fact);
}

Week-04 Program-04 – Write a C program to calculate the Sum of First and the Last Digit of a given Number. For example if the number is 1234 the result is 1+4 = 5.

Code:-

    Last_digit = N % 10;
N = N / 10;
    while(N >= 10)
    {
       N = N / 10; 
    }
    First_digit = N;

Week-04 Program-05 – Write a program to find whether a given character is a Vowel or consonant. A character is taken as input. The character may be in Upper Case or in Lower Case.

if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch =='o' || ch=='O' || ch == 'u' || ch == 'U')
    printf("%c is a vowel.", ch);
  else
    printf("%c is a consonant.", ch);
  return 0;
}

Problem Solving In C Programming Assignment Week 3 Answers:-

Week-03 Program-01 – 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;

Week-03 Program-02 – Write a C program to check if a given Number is zero or Positive or Negative Using if…else statement.

Code:-

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

Week-03 Program-03 – 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);

return 0;
}

Week-03 Program-04 – 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((n1<n2) && (n2>n3))
  printf("%d is the largest number.", n2);
  else
  printf("%d is the largest number.", n3);
  
  return 0;
}
quizxp telegram

Find Other Quiz Here:

NPTEL » Programming in C++ Assignment week 01 2021

Amazon Fashion Quiz Answers & Win ₹1000

Amazon Great Indian Festival Quiz Answers: Win Rs. 50,000

NPTEL » Programming In Java Assignment week 6 Sep-2020

NPTEL » Programming In Java Assignment week 7 Sep-2020

NOTE: These codes are based on our knowledge. Answers might be incorrect, we suggest you not copy-paste answers blindly.

[foobar id=”1315″]