Posts

Showing posts from August, 2014

Print a Floyd’s triangle having n rows.

Image
import java.util.Scanner; class triangle { public static void main(String arg[]) { Scanner input=new Scanner(System.in); System.out.println("Enter number of rows: "); int num; int ab=0; num=input.nextInt();                System.out.println();                System.out.println(); for(int i=1;i<=num;i++) { for(int j=1;j<=i;j++) { if(ab==0) { ab=1; System.out.print(ab); } else { ab=0; System.out.print(ab); } } System.out.println(); }          } }

Program to display binary equivalent of a number n.

Image
import java.lang.Math; import java.util.Scanner; public class bi { public static void main(String a[]){ int number; Scanner input=new Scanner(System.in); int binary[] = new int[64]; int index = 0; System.out.print("Enter number : "); number=input.nextInt(); if(number>0) {       System.out.print("\nBinary = "); while(number > 0) { binary[index++] = number%2; number = number/2; } for(int i = index-1;i >= 0;i--) { System.out.print(binary[i]); } } else { number=-number; while(number > 0) { binary[index++] = number%2; number = number/2; } index++; binary[index]=0; for(int i = index-1;i >= 0;i--) { if(binary[i]==1) { binary[i]=0; } else { binary[i]=1; } } if(binary[0]==0) binary[0]=1; else { for(int i = index-1;i >= 0;i--) { if(binary[i]==1) {

Display the denomination of an input amount.

Image
import java.util.Scanner; class denomination { public static void main(String arg[]) { Scanner input=new Scanner(System.in); int amount,counts=0; System.out.println("Enter Amount: "); amount=input.nextInt(); for(int i=amount;i>=1000;i-=1000) { counts++; amount-=1000; } if(counts!=0) System.out.println("1000 X "+counts); counts=0; for(int i=amount;i>=500;i-=500) { counts++; amount-=500; } if(counts!=0) System.out.println("500  X "+counts); counts=0; for(int i=amount;i>=100;i-=100) { counts++; amount-=100; } if(counts!=0) System.out.println("100  X "+counts); counts=0; for(int i=amount;i>=50;i-=50) { counts++; amount-=50; } if(counts!=0) System.out.println("50   X "+counts); counts=0; for(int i=amount;i>=20;i-=20) { counts++; amount-=20; } if(counts!=0) Sys

Numeric Operators

The operators for numeric data types include the standard arithmetic operators: addition (+), subtraction (–), multiplication (*), division (/), and remainder (%), as shown in Table 2.3. When both operands of a division are integers, the result of the division is an integer. The fractional part is truncated. For example, 5 / 2 yields 2, not 2.5, and –5 / 2 yields -2, not –2.5. To perform regular mathematical division, one of the operands must be a floating-point number. For example, 5.0 / 2 yields 2.5. The % operator yields the remainder after division. The left-hand operand is the dividend and the right-hand operand the divisor. Therefore, 7 % 3 yields 1, 12 % 4 yields 0, 26 % 8 yields 2, and 20 % 13 yields 7. Name         Meaning              Example         Result +                Addition              34 + 1           35 -                Subtraction          34.0–0.1        33.9 *                Multiplication      300 * 30        9000 /                 Division    

Named Constant

Image
public class finalconstant { public static void main(String[] args) { final double PI=3.14;           // constant value. can't be change throughout the program. double radius = 20; double area = radius * radius * PI; System.out.println("The area for the circle of radius " + radius + " is " + area); } } Output :-

Compute area of circle(using console Input)

import java.util.Scanner; public class circleareaconsoleinput { public static void main(String arg[]) { Scanner input=new Scanner(System.in); System.out.print("Enter radius for radius: "); double radius = input.nextDouble(); double area=radius*radius*3.14; System.out.print("Area of circle is : "+area); } }

Reading Input from console(Scanner class)

 You can use the Scanner class for console input. Java uses System.out to refer to the standard output device and System.in to the standard input device. By default the output device is the display monitor, and the input device is the keyboard. To perform console output, you simply use the println method to display a primitive value or a string to the console. Console input is not directly supported in Java, but you can use the Scanner class to create an object to read input from System.in, as follows: Scanner input = new Scanner(System.in); The syntax new Scanner(System.in) creates an object of the Scanner type. The syntax Scanner input declares that input is a variable whose type is Scanner. The whole line Scanner input = new Scanner(System.in) creates a Scanner object and assigns its reference to the variable input. An object may invoke its methods. To invoke a method on an object is to ask the object to perform a task. Method                              Description

Compute area of circle

Image
public class circlearea { public static void main(String arg[]) { double radius; double area; radius=20; area=radius*radius*3.14; System.out.println("Area of circle having radius 20 is "+area);   //here + is use to concatenate                                                                                                                        //string and variable. } } Output: - 

Data types and range in Java

Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals, or characters in these variables. There are two data types available in Java: Primitive Data Types Reference/Object Data Types Primitive Data Types: There are eight primitive data types supported by Java. Primitive data types are predefined by the language and named by a keyword. Let us now look into detail about the eight primitive data types. byte: Byte data type is an 8-bit signed two's complement integer. Minimum value is -128 (-2^7) Maximum value is 127 (inclusive)(2^7 -1) Default value is 0 Byte data type is used to save space in large arrays, mainly in place of integers, since a

Message Dialogbox with extra features

import javax.swing.JOptionPane; public class welcomebox { public static void main(String arg[]) { JOptionPane.showMessageDialog(null,"Welcome to smartprogrammeron.blogspot.in","Display Message",JOptionPane.INFORMATION_MESSAGE); } }

Message Dialogbox

Image
import javax.swing.JOptionPane;   //Java’s predefined classes public class welcomebox { public static void main(String arg[]) { JOptionPane.showMessageDialog(null,"Welcome to smartprogrammeron.blogspot.in"); } } output:-

Compute expressions

Image
public class computeexp { public static void main(String arg[]) { System.out.println(((10+4)/2)+1); } } output: -          How? Ans:- 10+4=14/2=7+1=8

How to install jdk and fix javac cmd.

Image

A Simple Java Program

Image
Step 1:- Open notepad. Step 2:- Write this code.             public class hello { public static void main(String arg[]) { System.out.println("Hello this smart programmeron.blogspot.in"); } } Step 3:- Save As-> Hello.java Step 4:- Press windows+R. Step 5:- Type cmd and hit enter to open Command Prompt. Cmds are:=               >  d:     ------  to change from C:\Users\Amit to D:\   drive .               > cd  java          ----- to goto folder java where my hello.java file was saved.               > javac hello.java     ---->> to compile the hello.java file which you I have save in D:\java                         After compile hello.java , You will find another file with extension .class .                         Here I have hello.class because in this code I wrote public class hello{}              > java hello   and hit the enter and your program will  run... Note: - Please run that .class file in which you ha