Saturday, 23 August 2014

Odd or Even

package com.tutorial4u;

import java.util.Scanner;

public class OddorEven {
       public static void main(String[] args) {
             System.out.println("Enter an Integer to check if it is odd or even.");
             Scanner sc = new Scanner(System.in);
             int num = sc.nextInt();
             if(num%2==0){
                    System.out.println("Entered number is even. ");
             }else{
                    System.out.println("Entered number is odd. ");
             }
       }

}


Output : 

Enter an Integer to check if it is odd or even.
24
Entered number is even.

Monday, 18 August 2014

Addition of Two Number

package com.tutorial4u;

import java.util.Scanner;

public class Addition {
     public static void main(String[] args) {
           int a, b, c;
           Scanner sc = new Scanner(System.in);
           System.out.print("Ener value of a : ");
           a = sc.nextInt();
           System.out.print("Enter value of b : ");
           b = sc.nextInt();
           c = a + b;
           System.out.println("Added Value of a and b is : " + c);
     }

}


Output :

Ener value of a : 13
Enter value of b : 34
Added Value of a and b is : 47

Print Multiplication of a Table

package com.tutorial4u;

import java.util.Scanner;

public class MutiplicationsOfTable {
     public static void main(String[] args) {
           int num;
           Scanner sc = new Scanner(System.in);
           System.out.print("Enter a Number to prints its table : ");
           num = sc.nextInt();
           System.out.println("Multiplication of table " + num + " is : ");
           for (int i = 1; i <= 10; i++) {
                System.out.println(num + "*" + i + "=" + (num * i));
           }

     }
}


Output :

Enter a Number to prints its table : 5
Multiplication of table 5 is :
5*1=5
5*2=10
5*3=15
5*4=20
5*5=25
5*6=30
5*7=35
5*8=40
5*9=45
5*10=50




HelloWord

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

Monday, 11 August 2014

Reverse of string

Reverse of string

/******************************************************
                  Reverse of string   
*******************************************************/
#include< stdio.h>
#include< conio.h>
#include< string.h>
void main()
{
 char str[100],temp;
 int i,j=0;
 clrscr();
 printf("Enter any the string :");
 gets(str);  //  gets function for input string
 i=0;
 j=strlen(str)-1;
  while(i < j)
   {
   temp=str[i];
   str[i]=str[j];
   str[j]=temp;
   i++;
   j--;
   }
 printf("Reverse string is :%s",str);
 getch();
}
Output
Enter any the string  : hitesh
Reverse string is : hsetih
Explanation Of Program :
Firstly find the length of the string using library function strlen().
j = strlen(str)-1;
Suppose we accepted String "hitesh" then
j = strlen(str)-1;
  = strlen("hitesh") - 1
  = 7 - 1
  = 6
As we know String is charracter array and Character array have character range between 0 to length-1. Thus we have position of last character in variable 'j'.Current Values of 'i' and 'j' are :
i = 0;
j = 6;
'i' positioned on first character and 'j' positioned on last character. Now we are swapping characters at position 'i' and 'j'. After interchanging characters we are incrementing value of 'i' and decrementing value of 'j'.
while(i < j)
     {
     temp   = str[i];
     str[i] = str[j];
     str[j] = temp;
     i++;
     j--;
     }
If i crosses j then process of interchanging character is stopped.

Java Development Kit (JDK) and Java Runtime Environment (JRE)

                  Java Development Kit (JDK) and Java Runtime Environment (JRE)  To download and install the Java Development Kit (  JDK ) ...