Wednesday, September 30, 2020

NPTEL INTRODUCTION TO PROGRAMMING IN C WEEK 3 ANSWERS (JUL-DEC 2020)

 Question 1:

#include<stdio.h>


int find_odd(int k)

{

  int num;

  int counter = 0;

  int check = 0;

  

  while(num != -1)

  {

    scanf("%d",&num);

    if(num%2!=0)

    {

      counter++;

      if(counter == k)

      {

        printf("%d",num);

        check = 1;

      }

    }

    

       

  }

   if((counter != k) && (check == 0))

      printf("-1");


  return 0;

}



int main()

{

  int k;

  scanf("%d",&k);

  find_odd(k);

  return 0;

}

Question 2:

#include<stdio.h>


int main()

{

  float num1,num2;

  float m_avg;

  

  scanf("%f",&num1);

  

  while(num1 != -1)

  {

    num2 = num1;

    scanf("%f",&num1);

      if(num1!=-1)

        { m_avg = (num1+num2)/2;

    printf("%0.1f ",m_avg);

        }

    

  }

  return 0;

}

    

Question 3:


#include<stdio.h>


int main()

{

  int n;

  scanf("%d",&n);

  int fac = 1;

  

  for(int i = 1; i<n; i++)

  {

    fac = fac*i; 

    if(fac <= n)

     {

        printf("%d ",fac);

      }

    else 

      break;

  }

  return 0;

  

}


Thursday, September 24, 2020

NPTEL PROGRAMMING IN JAVA WEEK 2 ANSWERS (JUL-DEC 2020)

 Question 1:

// Create an object of class Student


// Call 'print()' method of class Student 


// Create an object of class School


// Call 'print()' method of class School


Student obj = new Student();


obj.print();


School obj_1 = new School();


obj_1.print();


 Question 2:

// Create an object of class Printer


// Call 'print()' methods for desired output


Printer obj = new Printer();


obj.print("Hi! I am class STUDENT");


obj.print();


 Question 3:

// Define a method named 'studentMethod()' in class Question


// Call the method named 'print()' in class Question


void studentMethod(){

  

  Question23 obj = new Question23();

  print(obj);

}



 Question 4:

class Answer{

Answer(){

System.out.println("You got nothing.");

}

Answer(int marks, String type){

      this();

System.out.print("You got "+marks+" for an "+type);

}

}


 Question 5:

//Declare variable with name 'nptel', 'space' and 'java' and proper datatype.


//Initialize the variables with proper input


String nptel = "NPTEL", space = " ", java = "JAVA";


Wednesday, September 23, 2020

NPTEL INTRODUCTION TO PROGRAMMING IN C WEEK 2 ANSWERS (JUL-DEC 2020)

Question 1:

#include<stdio.h>

int main()
{
  int arr[50][50];
  int n;
  int upper = 1, lower = 1;
  scanf("%d",&n);
  
  for(int i=0; i<n; i++)
  {
    for(int j=0; j<n; j++)
      scanf("%d",&arr[i][j]);
  }
  
  for(int i=0; i<n; i++)
  {
    for(int j=0; j<n; j++)
    {
      if(i>j&&arr[i][j]!=0)
      {
        upper = 0;
      }
      
      else if(j>i&&arr[i][j]!=0)
      {
        lower = 0;
      }
     } 
  }
  
 if(upper == 1 && lower == 0)
   printf("1");
 else if(upper == 0 && lower == 1)
   printf("-1");
 else if (upper == 1 && lower == 1)
   printf("2");
 else 
   printf("0");
  
  return 0;
}


Question 2:


#include<stdio.h>

int main()
{
  int n;
  int arr[100];
  int i=0;
  
  
  while(n!=-1)
  {
    scanf("%d",&n);
    arr[i]=n;
    i++;
  }
  
  for(int j=0; j<i; j++)
    for(int k=j; k<i; k++)
    if(arr[j]<arr[k])
  {
    int temp;
    temp = arr[j];
    arr[j] = arr[k];
    arr[k] = temp;
  }
  
  int distinct=0;
  
  for(int j=0; j<i-1; j++)
  {
    if(arr[j]!=arr[j+1])
      distinct++;
  }
  
  if(distinct>=3)
    printf("1");
  else 
    printf("0");
}


Question 3:


#include<stdio.h>

int main()
{
  int n;
  int arr[100];
  int i=0;
  
  
  while(n!=-1)
  {
    scanf("%d",&n);
    arr[i]=n;
    i++;
  }
  
  for(int j=0; j<i; j++)
    for(int k=j; k<i; k++)
    if(arr[j]<arr[k])
  {
    int temp;
    temp = arr[j];
    arr[j] = arr[k];
    arr[k] = temp;
  }
  
  int distinct=0;
  
  for(int j=0; j<i-1; j++)
  {
    if(arr[j]!=arr[j+1]&&arr[j+1]!=-1)
    {
      distinct=arr[j+1];
      printf("%d",distinct);
      break;
    }
  }
  
  if(distinct==0)
    printf("0");
    
}

NPTEL PROGRAMMING IN C++ ASSIGNMENT WEEK 2 ANSWERS 2020(JUL-DEC)

 Question 1:

Complex operator*(Complex &p1, Complex &p2) { // LINE-1
    struct Complex p3 = { 0, 0 };
    p3.x = (p1.x)*(p2.x) - (p1.y)*(p2.y); // LINE-2
    p3.y = (p1.x)*(p2.y) + (p1.y)*(p2.x); // LINE-3
    return p3;
}


Question 2:

#include <iostream>
#include <string>
using namespace std;
void print(string a, string b = "Anyone") { // LINE-1


Question 3:

#include <iostream>
using namespace std;
int Double(int a) { // LINE-1
    return a*2;   // LINE-2
}


Qu
estion 4:

int main() {
    int *p;
    int arr[10];
  p =  arr;// LINE-1
    
    process(p);
    
        
    return 0;
}


Monday, September 14, 2020

Programming in Java WEEK 1 NPTEL(JULY - DEC 2020) ASSIGNMNET SOL.

Question 1: 

 
if(radius>0)
       {
perimeter = 2*Math.PI*radius;
    area = (Math.PI)*radius*radius;
          System.out.println(perimeter);
          System.out.print(area);
       }

else 
      System.out.print("please enter non zero positive number");


Question 2:


//Use if...else ladder to find the largest among 3 numbers and store the largest number in a variable called result.

if(x>y)
        {
          if(x>z)
            System.out.print(x);
          else 
            System.out.print(z);
        }

else 
        {
          if(y>z)
            System.out.print(y);
          else
            System.out.print(z);
        }

Question 3:


//Use for or while loop do the operation

  for(int i = 0, j = 0; i<n; i++, j=j+2)
      {
        if(j%3==0)
          sum = sum + j;
      }
  System.out.print(sum);
        

Question 4:


//Use while loop check the number is Armstrong or not.
//store the output(1 or 0) in result variable.
int sum = 0;
int n1 = n;
int counter = 0;
int[] arr = new int[100];

while(n>0)
        {
          int rem = n%10;
          arr[counter] = rem;
          n = n/10;
          counter++;
        }

for(int i=0; i<counter; i++)
        {
          int l = 1;
          for(int j=1; j<=counter; j++)
          {
            l=l*arr[i];
          }
          sum = sum + l;
        }
            

if(sum == n1)
          result = 1;
else 
          result = 0;

System.out.print(result);

          

Question 5:


//Initialize maximum element as first element of the array.  
   //Traverse array elements to get the current max.
   //Store the highest mark in the variable result.
   //Store average mark in avgMarks.
int largest;
int sum = 0;
float avg;
for(i=0;i<arr.length;i++)
  {
        for(int j = i; j<arr.length; j++)
                  if(arr[i]<arr[j])
                {
                  int temp = arr[i];
                  arr[i] = arr[j];
                  arr[j] = temp;
                }
    } 

largest = arr[0];

for(i=0;i<arr.length;i++)
  {
        sum = sum + arr[i];
  } 
avg = sum/arr.length;
System.out.println(largest);
System.out.print(avg);

Programming in C++ WEEK 1 NPTEL(JULY-DEC 2020) ASSIGNMNET SOL.

 Question 1 :

stack<char> s;    // LINE-1    



    for (int i = 0; i < strlen(str); i++)


        s.push(str[i]);    // LINE-2


Question 2 :

bool StrCmp(string s1, string s2) {


    if (s1.length()>s2.length() || s1.length()<s2.length())    // LINE-1

    

        return 1;    // LINE-2

    else

        return 0;    // LINE-3

}


Question 3 :

typedef void (*Fun_Ptr)(int,int); // LINE-1


Fun_Ptr fp = &add; // LINE-2

Sunday, September 13, 2020

Introduction to Programming in C week 1 NPTEL(JULY - DEC 2020) assignmnet solution

 Question 1 :

#include<stdio.h>


void  main()

{

  int arr[4];

  for(int i=0; i<3; i++)

    scanf("%d",&arr[i]);    //To take input.

  

  int sum = arr[0] + arr[1];

  

  if(sum>arr[2]) //if statement to check wether third number is smaller than the sum of

    printf("1"); //first two.

  else 

    printf("0");

}



Question 2 :

#include<stdio.h>


void main()

{

  int m,n;

  scanf("%d",&m);

  scanf("%d",&n);

  

  int rem = m%n;  //% is used to find the remainder.

  

  if(rem==1)

    printf("1");

  else 

    printf("0");

  

}


Question 3:

#include<stdio.h>


void main()

{

  float arr[4];

  

  for(int i=0; i<3; i++)

    scanf("%f",&arr[i]);

  

  //To check wether the triplet is strictly increasing or decreasing or none.

  

  if(arr[0]>arr[1]&&arr[1]>arr[2])

    printf("1");

  else if(arr[2]>arr[1]&&arr[1]>arr[0])

    printf("1");

  else

    printf("0");

}