Tuesday, July 28, 2020

Solving your first problem in C++ on CodeChef

Problem Name : Life, the Universe, and Everything

How to solve this question?


In this question they telling us to take the input of a number and display that number, repeat this proedure till we encounter number 42. After that we need to stop.

Solution :


#include <iostream>
using namespace std;

int main() {
// your code goes here
int number;
cin>>number;
while(number!=42)
{
    cout<<number<<endl;
    cin>>number;
}
return 0;
}




Sunday, July 26, 2020

Introduction to Programming in C week 0 assignment(jul-dec 2020)

Hello everyone! solution of Introduction to Programming in C week 0 assignment are given below.

Question 1 : 


#include<stdio.h>

int main()
{
  printf("Hello C");
  return 0;
}

Question 2 :


#include<stdio.h>

int main()
{
  int a,b;
  scanf("%d%d",&a,&b);
  int avg = (a+b)/2;
  printf("%d",avg);
  return 0;
}



Tuesday, April 7, 2020

PROGRAMMING IN JAVA WEEK 10

QUESTION 1:

import java.*;
import java.sql.*;

QUESTION 2:

// Open a connection and check connection status
conn = DriverManager.getConnection(DB_URL);
if(conn.isClosed())
  System.out.print("false");
else
  System.out.print("true");

QUESTION 3:

import java.sql.*;
import java.util.Scanner;
import java.lang.*;

public class Question103 {
    public static void main(String args[]) {
        try {
              Connection conn = null;
              Statement stmt = null;
              String DB_URL = "jdbc:sqlite:/tempfs/db";
              System.setProperty("org.sqlite.tmpdir", "/tempfs");
            conn = DriverManager.getConnection(DB_URL);
              conn.close();
              System.out.print(conn.isClosed());
        }
       catch(Exception e){ System.out.println(e);}
    }
}


QUESTION 4:

// The statement containing SQL command to create table "players"
String sql = "CREATE TABLE PLAYERS "+
  "(UID INT, "+
  "First_Name VARCHAR(45), "+
  "Last_Name VARCHAR(45), "+
  "Age INT, "+
  " PRIMARY KEY(UID))";

// Execute the statement containing SQL command below this comment
stmt.executeUpdate(sql);


QUESTION 5:

// Write the SQL command to rename a table
String str = "alter table PLAYERS rename to SPORTS";

// Execute the SQL command
stmt.executeUpdate(str);


Monday, March 23, 2020

WEEK 8

QUESTION 1:


// Declare a user-defined exception
class NegativeValException : public exception {     // LINE-1
public:
    virtual const char* what() const throw() {
        return "Negative value";
    }
};

// Declare a user-defined exception
class ZeroValException : public exception {         // LINE-2
public:
    virtual const char* what() const throw() {
        return "Zero";
    }
};

int main() {
    int i;
    cin >> i;
   
    try {
        if (i < 0)
            // Throw the exception object
            throw NegativeValException();                               // LINE-3

        else if (i == 0)
            // Throw the exception object
            throw ZeroValException();                               // LINE-4

QUESTION 2:


template <class T, int n>                    // LINE-1
class container {
    T arr[n];                // LINE-2

    int i;
public:
    container() : i(-1) { }

    void operator=(char data) {// LINE-3

QUESTION 3:


template <class T, int n>  // LINE-1

void mysequence<T,n>::setVal(int i, T value) {  // LINE-2

    items[i] = value;
}

template <class T, int n>   // LINE-3

T mysequence<T,n>::getVal(int i) {  // LINE-4

    return items[i];
}

Wednesday, March 18, 2020

WEEK 7 ASSIGNMENT SOLUTION

GUYS I KNOW I WAS SUPER INCONSISTENT AND LATE IN UPLOADING THE SOLUTIONS BUT I WAS HAVING A BUSY TIME IN PAST FEW WEEKS(COLLEGE WORK ) , I AM NOT GIVING ANY EXCUSES BUT JUST LETTING YOU ALL GUYS KNOW WHAT WAS THE REASON DUE TO WHICH I AM UPLOADING VIDEOS SO LATE. AND THANKS FOR WATCHING MY STUFF. 

QUESTION 1:

import java.util.*;
public class Question1{
        public static void main (String[] args){

//Write the appropriate code to read the 3 integer values and find their sum.
 int a,b=0,sum=0;
          Scanner i = new Scanner(System.in);
          for(a=0; a<3; a++)
          {
            b=i.nextInt();
            sum=sum+b;
          }

QUESTION 2:


try{InputStreamReader r=new InputStreamReader(System.in); 
   BufferedReader br=new BufferedReader(r); 
   String number=br.readLine(); 
   int x = Integer.parseInt(number);
   System.out.print(x*x);
   }
catch(Exception e){
  System.out.print("Please enter valid data");
}

QUESTION 3:


// Complete the code to get specific indexed byte value and its corresponding char value.
String str=new String(barr,n,1);
System.out.println(barr[n]);
System.out.print(str);
}

QUESTION 4:


//Write your code here to count the number of vowels in the string "s1"
for(int i=0;i<s1.length();i++){
  char s=s1.charAt(i);
  if(s=='a'||s=='A'||s=='e'||s=='E'||s=='i'||s=='I'||s=='o'||s=='O'||s=='u'|| s=='U')
  {
    c=c+1;
  }
}

QUESTION 5:


//Replace the char in string "s1" with the char 'a' at index "n"  and print the modified string
byte[] barr=s1.getBytes();
byte b1=(byte) c;
barr[n]=b1;
System.out.print(new String(barr));
}

Monday, March 16, 2020

WEEK 7 ASSIGNMENT SOLUTION

QUESTION 1:


Container& operator =(int val) {      // LINE-1

        this->arr[++i] = val;
        return *this;
    }

    operator int()  {      // LINE-2

        return arr[i--];
    }
};


QUESTION 2:


    virtual void print(int i)=0;          // LINE-1
};

class derived1 : public base{ // LINE-2
public:
    void print(int i) { cout << i * 1 << " "; }
};

class derived2 : public base { // LINE-3
public:
    void print(int i) { cout << i * 2 << " "; }
};

class derived3 : public base{ // LINE-4

QUESTION 3:


void updateSem(int new_sem) const{             // LINE-1

        student* ptr=const_cast<student*> (this);
      ptr->sem = new_sem;             // LINE-2
    }

    void show() const { 


QUESTION 4:


class derived1 : virtual public base {    // LINE-1
public:
    derived1();
    derived1(int _pr);
};

class derived2 : virtual public base {    // LINE-2
public:
    derived2();
    derived2(int _pr);
};

class dd : public derived2, public derived1 {          // LINE-3

Wednesday, March 11, 2020

WEEK 6 ASSIGNMENT SOLUTION

QUESTION 1:

// Write the appropriate code to extend the Thread class to complete the class Question61.
public class Question61 extends Thread{

public void run()
{
  System.out.print("Thread is Running.");
}

QUESTION 2:

// Create main class Question62 with main() method and appropriate statements in it
public class Question62{
  public static void main(String[] args){
    ThreadRun a = new ThreadRun();
    Thread b = new Thread(a);
    b.start();
  }
}


QUESTION 3:

// Create a class named MyThread and extend/implement the required class/interface

// Define a method in MyThread class to print the output

class MyThread extends B
{
  public void run()
  {
    System.out.print("NPTEL Java");
  }
}

QUESTION 4:

synchronized void print(int n){
   for(int i=1;i<=5;i++){ 
     System.out.println(n*i); 
     try{ 
      Thread.sleep(400); 
     }catch(Exception e){
        System.out.println(e);
     } 
   }
 } 


QUESTION 5:

// Write the necessary code below...
t.setPriority(Thread.MAX_PRIORITY);
t.start();
t.setName("NPTEL");