Search This Blog

Sunday, 23 March 2014

item insertion in an array at any location in java


import java.util.*;

public class array {
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int []ar=new int[20];
        int i,j,n;
        System.out.println("How many element u want ? ");
        n=sc.nextInt();
        System.out.println("Enter elements : ");
        for(i=0;i<n;i++){
            ar[i]=sc.nextInt();
        }
        System.out.print("Elements are : \n");
        for(i=0;i<n;i++){
            System.out.print(ar[i]+"  ");
        }
        int val,pos;
        System.out.println("\nEnter element to insert : ");
        val=sc.nextInt();
        System.out.println("Enter position to insert : ");
        pos=sc.nextInt();
        if(pos>=0 && pos<=n+1){
            for(i=n;i>pos-1;i--){
                ar[i]=ar[i-1];
            }
          ar[i]=val;
          n++;
          System.out.println("Updated arrray is : ");
          for(i=0;i<n;i++){
            System.out.print(ar[i]+"  ");
        }
        }
        else
            System.out.println("Invalid position");
        }

}

Output


Saturday, 22 March 2014

overloading = operator in C++


#include<iostream.h>
#include<conio.h>

class OpOver
{
  public :
       int len,bre;
       int area()
       {
         return len*bre;
       }
       void operator =(OpOver &r)
       {
         len=r.len;
         bre=r.bre;
       }
};

void main()
{
  OpOver a,b,c;

  a.len=3,a.bre=6;
  b.len=2,b.bre=2;
  cout<<"\nArea of first obj "<<" and "<<" is "<<a.area();//18
  cout<<"\nArea of first obj "<<" and "<<" is "<<b.area();    //4
  c=a; // c=a;
  cout<<"\n area of 3rd obj is :"<<c.area(); //18
  getch();
}

overloading + operator in C++


#include<iostream.h>
#include<conio.h>

class Op
{
  public :
          int a,b;
          int add()
          {
            return a+b;
          }
          Op operator +(Op m)      // operator overloading format
          {
            Op temp;
            temp.a=a+m.a;
            temp.b=b+m.b;
            return temp;
          }
};

void main()
{
  Op op1,op2,op3;
  op1.a=2,op1.b=3;
  cout<<"Sum is :"<<op1.add();  //5
  op2.a=2,op2.b=8;
  cout<<"\nSum is :"<<op2.add(); //10
  op3=op1+op2;     //passing object as parameter
  cout<<"\nSum is :"<<op3.add(); //15
  getch();

}

Thursday, 20 March 2014

diamond shape of star in C++


#include<iostream.h>
#include<conio.h>
void main()
{
  int i,j,k,n,temp;
  cout<<"Enter height (must be >2) : ";
  cin>>temp;
  n=(temp/2)+1;
  if(temp>2)
  {
     for(i=1,j=n-1;i<n,j>=0;i++,j--)
     {
     for(k=0;k<=j;k++)
     cout<<" ";
     for(k=1;k<=2*i-1;k++)
     cout<<"*";
     cout<<endl;
     }
    for(i=n-1,j=0;i>=0,j<n;i--,j++)
    {
     cout<<" ";
     for(k=0;k<=j;k++)
     cout<<" ";
     for(k=1;k<=2*i-1;k++)
     cout<<"*";
     cout<<endl;
    }
  }
  else
  cout<<"Minimum should be 3";
  getch();
}

Output


finding ASCII value of any character

/*  ASCII  means American Standard Code for Information Interchange*/


#include<iostream.h>
#include<conio.h>

void main()
{
  char ch;
  cout<<"Enter any character :";
  cin>>ch;
  cout<<"Its ASCII value is : "<<(int)ch;
  getch();
}

Output


Tuesday, 18 March 2014

finding nCr in C++

/*
  Here nCr stands for combination of all r in n.
  nCr = n! / r! (n-r)!
*/


#include<iostream.h>
#include<conio.h>

int fact(int a)
{
  if(a>1)
  return a*fact(a-1);
  else
  return a;
}

void main()
{
  int n,r;
  float comb;
  cout<<"Enter n and r for nCr : ";
  cin>>n>>r;
  comb=(fact(n)/(fact(r)*fact(n-r)));
  cout<<"Combination of ur values is : "<<comb;
  getch();
}

Output



use of goto in C++


// print triangular star using "goto" statement
#include<iostream.h>
#include<conio.h>
void main()
{
int j,i;
i=1;
x:
j=1;
y:
cout<<"*";
j++;
if(j<=i)
goto y;
cout<<endl;
i++;
if(i<=5)
goto x;
getch();
}

Output

Sunday, 16 March 2014

armstrong number between range


#include<iostream.h>
#include<conio.h>

void main()
{
  int i,temp,sum,k;
  for(i=101;i<=500;i++)
  {
    sum=0;
    temp=i;
    while(temp!=0)
    {
       k=temp%10;
       sum+=k*k*k;
       temp=temp/10;
    }
    if(i==sum)
    cout<<sum<<" ";
  }
  getch();
}

Output


delete consonant from any string


/*
   Suppose we have a string as
    ANIMAL  then this program will do and present the result as  A _ I _ A _
 */


#include<iostream.h>
#include<conio.h>

void main()
{
  char str[20];
  int len=0,i;
  cout<<"Enter any string :";
  cin.get(str,20);
  for(i=0;str[i]!='\0';i++)
    len++;
  for(i=0;i<len;i++)
  {
    if(str[i]=='A'||str[i]=='a'||str[i]=='e'||str[i]=='E'||str[i]=='i'||
    str[i]=='I'|str[i]=='o'||str[i]=='O'||str[i]=='u'||str[i]=='U')
      cout<<str[i];
    else if(str[i]==' ')
      cout<<"  ";
    else
       cout<<"_";
  }
  getch();
}

Output


Thursday, 13 March 2014

finding two smallest number in an array using C++

/*
  let we have 2,5,1,7,9
  1st small item is 1
  2nd small item is 2.
*/


#include<iostream.h>
#include<conio.h>

void main()
{
      int ar[5],i;
      cout<<"Enter array elements:";
      for(i=0;i<5;i++)
      {
         cin>>ar[i];
      }
      cout<<"Elements are:\n";
      for(i=0;i<5;i++)
      {
         cout<<ar[i]<<"\t";
      }
      int min=ar[0];
      for(i=0;i<5;i++)
      {
         if(ar[i]<ar[0])
         min=ar[i];
      }
      cout<<"\nFirst minimum number is:"<<min;

      for(i=0;i<5;i++)
      {
         if(ar[i]==min)
         {
            ar[i]=32767;
         }
      }
      int min1=ar[0];
      for(i=0;i<5;i++)
      {
         if(ar[i]<ar[0])
         {
            min1=ar[i];
         }
      }
      cout<<"\nsecond minimum number is:"<<min1;
      getch();
}

Output


Abstract Class and Abstract Method


abstract class Base
{
    private int roll;
    private String name;
    private String branch;
    
    Base(int roll,String name,String branch)
    {
        this.roll=roll;
        this.name=name;
        this.branch=branch;
    }
    int getRoll()
    {
        return roll;
    }
    String getName()
    {
        return name;
    }
    String getBranch()
    {
        return branch;
    }
    abstract void display();   // abstract Method
}
class Child extends Base
{
    Child(int roll,String name,String branch)
    {
        super(roll,name,branch);   
    }
    void display()
    {
      System.out.println("Name is : "+getName());
      System.out.println("Roll number is : "+getRoll());
      System.out.println("Branch is : "+getBranch());
    }
}

class MainClass
{
    public static void main(String[] args)
    {
        Base b=new Child(21,"sanjay","cse");  //upcasting
        b.display();
    }
}

Output


Tuesday, 11 March 2014

function with #define in C++


#include<iostream.h>
#include<conio.h>
#define Min(a,b)(a<b?a:b)
void main()
{
  int i=20,j=25;
  cout<<"Minimum value is : "<<Min(i,j);
  getch();
}

Output


Monday, 10 March 2014

String Methods in java


package stringfunctions;

import java.lang.String;
class Strings
{
    public static void main(String[] args)
    {
        String str="sanjay kumar singh";
        String str2="Sanjay Kumar Singh";
        String str3=" sanjay  kumar   singh  ";
        String str4="sanjay";
        String str5=" kumar singh";
       
        System.out.println(str.charAt(5));
        System.out.println(str2.compareTo(str));
        System.out.println(str.compareToIgnoreCase(str2));
        System.out.println(str.endsWith("singh"));
        System.out.println(str4.concat(str5));
        System.out.println(str.length());
        System.out.println(str3.trim());
        System.out.println(str2.equals(str));
        System.out.println(str2.equalsIgnoreCase(str));
        System.out.println(str.indexOf('k'));
        System.out.println(str.startsWith("singh"));
        System.out.println(str.indexOf("kumar"));
        System.out.println(str.substring(6));
        System.out.println(str.substring(6, 12));
        System.out.println(str4.replaceAll("sanjay","neeraj"));
    }
}

Output


Sunday, 9 March 2014

finding product of two numbers without using multiplication operator


#include<iostream.h>
#include<conio.h>

void main()
{
  int multiplicand,multiplier,i,sum;
  cout<<"Enter multiplier and multiplicand :";
  cin>>multiplier>>multiplicand;
  sum=0;
  //determining the multiplication
       int min=multiplier<=multiplicand?multiplier:multiplicand;
       int max=multiplier>multiplicand?multiplier:multiplicand;
       for(i=0;i<min;i++)
         sum+=max;
     cout<<"\nAnswer of (multiplier * multiplicand) :"<<sum;
  //ends here
  getch();
}

Output


finding power of a number without using power function


#include<iostream.h>
#include<conio.h>

void main()
{
  int base,expo,i,sum=1;
  cout<<"Enter base and exponent of the expression :";
  cin>>base>>expo;
  // determining the power function
     for(i=0;i<expo;i++)
       sum*=base;
   cout<<"\nAnswer of (Base ^ exponent) :"<<sum;
  //ends here
  getch();
}

Output


Composition in C++


/*
Composition means using the object of one class in another without inheriting.
Example:
class Student
{
  //Codes
}
class Address
{
 Student s;
 ...// Codes
}
*/



#include<iostream.h>
#include<conio.h>

class Book
{
    private : int bCode;
              char *bName;
    public :
    void SetbCode(int n)
    {
      bCode=n;
    }
    void SetbName(char name[])
    {
        bName=name;
    }
    int GetbCode()
    {
        return bCode;
    }
    char* GetbName()
    {
        return bName;
    }
};
class Student
{
    public :
    int roll;
    char *name;
    Book bk;
    Student(int roll,char nick[],Book k)
    {
        this->roll=roll;
        name=nick;
        bk=k;
    }
    void showDetails()
    {
        cout<<"\nStudent name is : "<<name<<"  "<<roll;
        cout<<"\nBook details are : "<<bk.GetbName()<<"  "<<bk.GetbCode();
    }
};

void main()
{
       Book bt;
       bt.SetbCode(1007);
       bt.SetbName("MicroProcessor");
       Student s(21,"sanjay kumar singh",bt);
       s.showDetails();
       getch();
}

Output


Aggregation in Java

/*
Aggregation means using the object of one class in another without inheriting.
Example:
class Student
{
  //Codes
}
class Address
{
 Student s;
 ...// Codes
}
*/



class Book
{
    private int bCode;
    private String bName;
    public void SetbCode(int n)
    {
      bCode=n;  
    }
    public void SetbName(String name)
    {
        bName=name;
    }
    public int GetbCode()
    {
        return bCode;
    }
    public String GetbName()
    {
        return bName;
    }
}
class Student
{
    int roll;
    String name;
    Book bk;
    Student(int roll,String name,Book k)
    {
        this.roll=roll;
        this.name=name;
        bk=k;
    }
    void showDetails()
    {
        System.out.println("Student name is : "+name+"  "+roll);
        System.out.println("Book details are : "+bk.GetbName()+"  "+bk.GetbCode());
    }
}


public class Aggregation
{
   public static void main(String[] args)
   {
       Book bt=new Book();
       bt.SetbCode(1007);
       bt.SetbName("MicroProcessor");
       Student s=new Student(21,"sanjay kumar singh",bt);
       s.showDetails();
   }
}

Output


Saturday, 8 March 2014

stack in C++ using linked list

/*
 Stack performs following operation
  1 . Insertion (push).
  2 . Deletion (pop) 
*/


#include<iostream.h>
#include<conio.h>
#include<process.h>


struct node
{
        int data;
        node *next;
};

class Stack
{
  public :
          int val;
          node *start,*temp,*cur;
          Stack()
          {
           start=NULL;
          }
          void push()
          {
            cout<<"\nEnter value of element :";
            cin>>val;
            temp=new node[1];//temp=new node;
            temp->data=val;
            temp->next=NULL;
            if(start==NULL)
            {
                  start=temp;
            }
            else
            {
              temp->next=start;
              start=temp;
            }
          }

          void pop()
          {
            if(start==NULL)
              cout<<"\nStack is empty\n";
            else
            {
              temp=start;
              start=start->next;
              delete temp;
              cout<<"\nElement deleted successfully\n";
            }
          }
          void display()
          {
             if(start==NULL)
             cout<<"\nStack is empty\n";
             else
             {
               temp=start;
               while(temp!=NULL)
               {
                 cout<<temp->data<<",";
                 temp=temp->next;
               }
             }
          }
};

void main()
{

  Stack s;
  int n;
  char ch;
  do{
  cout<<"\nMain Menu\n";
  cout<<"1.Push\t2.Pop\t3.Display\t4.Exit\nEnter yuor choice :";
  n=getch();
  switch(n)
  {
     case '1':s.push();
              cout<<"\nElement inserted successfully\n";
              break;
     case '2':s.pop();
              break;
     case '3':s.display();
              break;
     case '4':exit(0);
  }
  cout<<"\nDo u want to continue(y/n) : ";
  cin>>ch;
  }while(ch=='y'||ch=='Y');
  getch();
}

Output




queue in C++ using linked list

/*
  In queue there are following operations are performed such as
  1 . Insertion from 'rear' side.
  2 . Deletion from 'front' side. 
*/


#include<iostream.h>
#include<conio.h>
#include<process.h>

struct node
{
  int data;
  node *next;
};

class Queue
{
  public :
         node *rear,*front,*temp;
         int val;
         Queue()
         {
           front=NULL;
           rear=NULL;
         }
         void Qinsert()
         {
           cout<<"\nEnter element to insert:";
           cin>>val;
           temp=new node[1];
           temp->data=val;
           temp->next=NULL;
           if(rear==NULL && front==NULL)
           {
             rear=temp;
             front=temp;
           }
           else
           {
             rear->next=temp;
             rear=temp;
             cout<<"\nInserted Successfully\n";
           }
         }
         void Qdelete()
         {
           if(front==NULL)
           {
             cout<<"Queue is empty";
           }
           else
           {
             temp=front;
             front=front->next;
             delete(temp);
             cout<<"\nDeleted successfully\n";
           }
         }
         void Qdisplay()
         {
           if(front==NULL)
           cout<<"\nQueue is empty\n";
           else
           {
             for(temp=front;temp!=NULL;temp=temp->next)
             {
               cout<<temp->data<<",";
             }
           }
         }
};

void main()
{
  Queue q;
  char ch;
  int n;
  do{
  cout<<"\n1.Qinsert\t2.Qdelete\t3.Qdisplay\t4.Exit\nEnter your choice :";
  n=getch();
  switch(n)
  {
    case '1':q.Qinsert();
              break;
    case '2':q.Qdelete();
              break;
    case '3':q.Qdisplay();
              break;
    case '4':exit(0);
  }
  cout<<"\nDo u want to continue (y/n):";
  cin>>ch;
  }while(ch=='y'||ch=='Y');
  getch();
}

Output


friend Class in C++

/*
 friend class means that a class can use all the private variables of the Base Class .
 Syntax :
 friend class <class_name>;
*/


#include<iostream.h>
#include<conio.h>

class Student
{
         int roll,a,b,c;
         char *name;
       public :
         Student(int roll,char ch[],int a,int b,int c )
         {
           this->roll=roll;
           name=ch;
           this->a=a;
           this->b=b;
           this->c=c;
         }
         void output()
        {
           cout<<"\nRoll number is :"<<roll;
           cout<<"\nName is :"<<name;

        }
         friend class Marks;
};
class Marks
{
  int marks,avg;
  public :
           void Marking(Student s)
           {
             marks=s.a+s.b+s.c;
             cout<<"\nTotal marks is : "<<marks;
             avg=(marks)/3;
           }
};
void main()

{
  Student s(21 , "sanjay kumar singh",78,89,90);
  s.output();
  Marks m;
  m.Marking(s);
  getch();
}

Output


new keyword in C++

/*
 new keyword is used to allocate dynamic memory for the variable.
 Syntax 
 <data_type> pointer_variable
 pointer_variable=new <data_type>[size]

 deallocation of memory is done by delete keyword.
*/


#include<iostream.h>
#include<conio.h>
void main()
{
  int *n,i;
  n=new int[5];
  cout<<"Enter element :\n";
  for(i=0;i<5;i++)
  cin>>n[i];
  cout<<"\nElements are :\n";
  for(i=0;i<5;i++)
  cout<<n[i]<<" ";
 delete(n);
  getch();
}

Output


call by value and call by reference


// program to illustrate call by value as well as call by reference

#include<iostream>
#include<conio.h>

void passbyvalue(int x);
void passbyreference(int *x);
void main()
{
   int a=13;
   int b=13;

   passbyvalue(a);  /// call by value

   passbyreference(&b);   call by address
   cout<<a<<endl;
   cout<<b<<endl;

   getch();
}
void passbyvalue(int x)
{
x=33;
}
void passbyreference(int *x)
{
*x=99;
}

Output


Saturday, 1 March 2014

typedef in C++

/*
typedef changes the name of the  variable such that no confusion is created .
Syntax :  typedef int Integer;
              Integer a,b;
              // here a and b are two integer values .
*/


#include<iostream.h>
#include<conio.h>
 void main()
 {
    typedef int var;
    var a,b,c;
    cout<<"Values for a and b :";
    cin>>a>>b;
    c=a+b;
    cout<<"Sum is:"<<c;
    getch();
 }

Output


string functions in C++

/*
strlen(str) returns length of the string str.
strcpy(str1,str2) copy the string from the str2 to str1.
strcat(str1,str2) catenate str1 to str2.
*/


#include <iostream>
#include<conio>
#include<cstring>
void main ()
{
   char str1[10] = "Hello";
   char str2[10] = "World";
   char str3[10];
   int  len ;

   // copy str1 into str3
   strcpy( str3, str1);
   cout << "strcpy( str3, str1) : " << str3 << endl;

   // concatenates str1 and str2
   cout << "strcat( str1, str2): " <<   strcat( str1, str2)<< endl;

   // total lenghth of str1 after concatenation
   len = strlen(str1);
   cout << "strlen(str1) : " << len << endl;

   getch();
}

Output


Popular Posts