Thursday 29 January 2015

C

C Program For Reverse string
C Program for Palindrome
C program to convert string to integer without using atoi function
C program to delete vowels from a string
C Programming To Find a Substring
C program to check subsequence
C program to sort a string in alphabetic order
C Program To Remove Blanks Spaces from a string
C Program To Change string to upper case without strupr
C Program To Change string to Lower case without strlwr
C program to swap two strings
C program to find frequency of characters in a string
C Programming To Find Anagram
C program to read a file
C Program To Copy Files
C Program To Merge Two Files
C Program to List Files in Directory
C Program to Delete a File
C Program to Generate Random Numbers
C Program to Add two Complex Numbers
C Program to Print Date
C Program To Shutdown Or Turn off Computer
C Program to Get IP Address

Wednesday 21 January 2015

Java Programming

Constructor Overloading In Java



class Language {
  String name;

  Language() {
    System.out.println("Constructor method called.");
  }

  Language(String t) {
    name = t;
  }

  public static void main(String[] args) {
    Language cpp  = new Language();
    Language java = new Language("Java");

    cpp.setName("C++");

    java.getName();
    cpp.getName();
  }

  void setName(String t) {
    name = t;
  }

  void getName() {
    System.out.println("Language name: " + name);
  }
}

Tuesday 20 January 2015

Constructor In Java



class Programming {
  //constructor method
  Programming() {
    System.out.println("Constructor method called.");
  }

  public static void main(String[] args) {
    Programming object = new Programming(); //creating object
  }
}

Multiple Classes In Java

class Computer {
  Computer() {
    System.out.println("Constructor of Computer class.");
  }

  void computer_method() {
    System.out.println("Power gone! Shut down your PC soon...");
  }

  public static void main(String[] args) {
    Computer my = new Computer();
    Laptop your = new Laptop();

    my.computer_method();
    your.laptop_method();
  }
}

class Laptop {
  Laptop() {
    System.out.println("Constructor of Laptop class.");
  }

  void laptop_method() {
    System.out.println("99% Battery available.");
  }
}

Java Static Program



class StaticBlock {
  public static void main(String[] args) {
    System.out.println("Main method is executed.");
  }

  static {
    System.out.println("Static block is executed before main method.");
  }
}


class Languages {
  public static void main(String[] args) {
    display();
  }

  static void display() {
    System.out.println("Java is my favorite programming language.");
  }
}

Java String Methods



class StringMethods
{
  public static void main(String args[])
  {
    int n;
    String s = "Java programming", t = "", u = "";

    System.out.println(s);

    // Find length of string

    n = s.length();
    System.out.println("Number of characters = " + n);

    // Replace characters in string

    t = s.replace("Java", "C++");
    System.out.println(s);
    System.out.println(t);

    // Concatenating string with another string

    u = s.concat(" is fun");
    System.out.println(s);
    System.out.println(u);
  }
}

Java Methods



class Methods {

  // Constructor method

  Methods() {
    System.out.println("Constructor method is called when an object of it's class is created");
  }

  // Main method where program execution begins

  public static void main(String[] args) {
    staticMethod();
    Methods object = new Methods();
    object.nonStaticMethod();
  }

  // Static method

  static void staticMethod() {
    System.out.println("Static method can be called without creating object");
  }

  // Non static method

  void nonStaticMethod() {
    System.out.println("Non static method must be called by creating an object");
  }
}

Java Program To Convert Fahrenheit To Celsius



import java.util.*;

class FahrenheitToCelsius {
  public static void main(String[] args) {
    float temperatue;
    Scanner in = new Scanner(System.in);     

    System.out.println("Enter temperatue in Fahrenheit");
    temperatue = in.nextInt();

    temperatue = ((temperatue - 32)*5)/9;

    System.out.println("Temperatue in Celsius = " + temperatue);
  }
}

Java Program To Find Odd Or Even



import java.util.Scanner;

class OddOrEven
{
   public static void main(String args[])
   {
      int x;
      System.out.println("Enter an integer to check if it is odd or even ");
      Scanner in = new Scanner(System.in);
      x = in.nextInt();

      if ( x % 2 == 0 )
         System.out.println("You entered an even number.");
      else
         System.out.println("You entered an odd number.");
   }
}

Java Program To Add Two Numbers



import java.util.Scanner;

class AddNumbers
{
   public static void main(String args[])
   {
      int x, y, z;
      System.out.println("Enter two integers to calculate their sum ");
      Scanner in = new Scanner(System.in);
      x = in.nextInt();
      y = in.nextInt();
      z = x + y;
      System.out.println("Sum of entered integers = "+z);
   }
}
Above code can add only numbers in range of integers(4 bytes), if you wish to add very large numbers then you can use BigInteger class. Code to add very large numbers:




import java.util.Scanner;
import java.math.BigInteger;

class AddingLargeNumbers {
  public static void main(String[] args) {
    String number1, number2;
    Scanner in = new Scanner(System.in);

    System.out.println("Enter first large number");
    number1 = in.nextLine();

    System.out.println("Enter second large number");
    number2 = in.nextLine();

    BigInteger first  = new BigInteger(number1);
    BigInteger second = new BigInteger(number2);
    BigInteger sum;

    sum = first.add(second);

    System.out.println("Result of addition = " + sum);
  }
}

Output of program:

Enter first large number
11111111111111
Enter second large number
99999999999999
Result of addition = 111111111111110

How To Get Input From User In Java



import java.util.Scanner;

class GetInputFromUser
{
   public static void main(String args[])
   {
      int a;
      float b;
      String s;

      Scanner in = new Scanner(System.in);

      System.out.println("Enter a string");
      s = in.nextLine();
      System.out.println("You entered string "+s);

      System.out.println("Enter an integer");
      a = in.nextInt();
      System.out.println("You entered integer "+a);

      System.out.println("Enter a float");
      b = in.nextFloat();
      System.out.println("You entered float "+b);  
   }
}

Java Program To Print Multiplication Table



import java.util.Scanner;

class MultiplicationTable
{
   public static void main(String args[])
   {
      int n, c;
      System.out.println("Enter an integer to print it's multiplication table");
      Scanner in = new Scanner(System.in);
      n = in.nextInt();
      System.out.println("Multiplication table of "+n+" is :-");

      for ( c = 1 ; c <= 10 ; c++ )
         System.out.println(n+"*"+c+" = "+(n*c));
   }
}

Java Program To Print Alphabets



class Alphabets
{
   public static void main(String args[])
   {
      char ch;

      for( ch = 'a' ; ch <= 'z' ; ch++ )
         System.out.println(ch);
   }
}

Java "while" Loop



import java.util.Scanner;

class WhileLoop {
  public static void main(String[] args) {
    int n;

    Scanner input = new Scanner(System.in);
    System.out.println("Input an integer");

    while ((n = input.nextInt()) != 0) {
      System.out.println("You entered " + n);
      System.out.println("Input an integer");
    }

    System.out.println("Out of loop");
  }
}

Java "for" Loop

Simple for loop example in Java

class ForLoop {
  public static void main(String[] args) {
    int c;

    for (c = 1; c <= 10; c++) {
      System.out.println(c);
    }
  }
}





Java for loop example to print stars in console


class Stars {
  public static void main(String[] args) {
    int row, numberOfStars;

    for (row = 1; row <= 10; row++) {
      for(numberOfStars = 1; numberOfStars <= row; numberOfStars++) {
        System.out.print("*");
      }
      System.out.println(); // Go to next line
    }
  }
}

Java if-else Program

import java.util.Scanner;
 
class IfElse {
  public static void main(String[] args) {
    int marksObtained, passingMarks;
 
    passingMarks = 40;
 
    Scanner input = new Scanner(System.in);
 
    System.out.println("Input marks scored by you");
 
    marksObtained = input.nextInt();
 
    if (marksObtained >= passingMarks) {
      System.out.println("You passed the exam.");
    }
    else {
      System.out.println("Unfortunately you failed to pass the exam.");
    }
  }
}
 

Hello World

class HelloWorld
{
   public static void main(String args[])
   {
      System.out.println("Hello World");
   }
}

 

Download Hello world program class file.

 
 

Sunday 18 January 2015

Link List Program

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
//-------------------------------------------------
struct node
{
int data;
struct node *next;
}*start=NULL;
//------------------------------------------------------------

void creat()
{
char ch;
 do
 {
  struct node *new_node,*current;

  new_node=(struct node *)malloc(sizeof(struct node));

  printf("nEnter the data : ");
  scanf("%d",&new_node->data);
  new_node->next=NULL;

  if(start==NULL)
  {
  start=new_node;
  current=new_node;
  }
  else
  {
  current->next=new_node;
  current=new_node;
  }

 printf("nDo you want to creat another : ");
 ch=getche();
 }while(ch!='n');
}
//------------------------------------------------------------------

void display()
{
struct node *new_node;
 printf("The Linked List : n");
 new_node=start;
 while(new_node!=NULL)
   {
   printf("%d--->",new_node->data);
   new_node=new_node->next;
   }
  printf("NULL");
}
//----------------------------------------------------
void main()
{
create();
display();
}
//----------------------------------------------------

Enter the data : 10
Do you want to creat another :  y
Enter the data : 20
Do you want to creat another : y

Enter the data : 30
Do you want to creat another : n

The Linked List :
10--->20--->30--->NULL

Donate