Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Friday, October 18, 2013

Java Program to generate a random number with output

 Code:-


class Test
{

    public static void main(String args[])
    {
       
        double a = Math.random();
        System.out.println(a);
    }
}

Output:-


Java Program to test whether a given year is a leap year or not


Code:-

import java.util.Scanner;
class Leap{
public static void main(String args[]){
System.out.println("Enter year");
Scanner s=new Scanner(System.in);
int a=s.nextInt();
if((a%100)==0){
if((a%400)==0)
System.out.println(a+" is a century year and a leap year too");
else
System.out.println(a+" is a century year");
}
else{
if((a%4)==0)
System.out.println(a+" is leap year ");
else
System.out.println(a+" is not a leap year");
}
}
}

Output:-


Monday, October 14, 2013

Java Program to read text using readline function

import java.io.*;
class Read{
public static void main(String args[]) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str;
System.out.println("Enter text \n stop to terminate" );
do{
str= br.readLine();
System.out.println(str);
}
while(str.equals("Stop"));
}
}

Saturday, September 21, 2013

Java program to create a frame using Swing

import javax.swing.*;
class P1{
public static void main(String args[]){
JFrame j=new JFrame();
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j.setSize(300,300);
j.setVisible(true);
}
}

Output:-


Sunday, August 11, 2013

C++ and Java Program to add two numbers

Using C++:-

#include<iostream> 
using namespace std;
int main(){
int a,b,c;
cout<<"Enter First Number";
cin>>a;
cout<<"Enter Second Number";
cin>>b;
c=a+b;
cout<<"The sum is"<<" "<<c;
return 0;
}


 

Output:-

 Using java:-

Using Scanner class(to take input from the user):- 

import java.util.Scanner;
class Add{
public static void main(String args[]){
Scanner s=new Scanner(System.in);
int a,b,c;
System.out.println("Enter First Number");
a=s.nextInt();
System.out.println("Enter Second Number");
b=s.nextInt();
c=a+b;
System.out.println("The sum of a and b is "+c);
}
}
 

Output:-

 

Using predefined values:-

class Add{
public static void main(String args[]){
int a=10,b=20;
int c=a+b;
System.out.println("First Number="+a+"\nSecond Number="+b);
System.out.println("Sum is "+c);
}
}

Output:-

 


 

 

 

A simple hello world program

Using c++:-


#include<iostream>
using namespace std;
int main(){
cout<<"Hello world";
return 0;
}



Using Java

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

Ads Inside Post