Showing posts with label java codes. Show all posts
Showing posts with label java codes. Show all posts

Friday, 25 July 2014

Examples of loop with output in java code

class Loops
{
                public static void main(String args[])
                {
                                int i;
                                for(i=0; i<10; i += 2)
                                System.out.print(" " + i);
                                System.out.println();

                                for(i=100; i>=0; i -= 7)
                                System.out.print(" " + i);
                                System.out.println();

                                for(i=1; i<=10; i += 1)
                                System.out.print("*" + ++i);
                                System.out.println();

                                for(i=2; i<100; i *= 2)
                                System.out.print(" " + i);
                }
}
Output:
0 2 4 6 8
100 93 86 79 72 65 58 51 44 37 30 23 16 9 2
*2*4*6*8*10

2 4 8 16 32 64

Thursday, 24 July 2014

Calculate square of odd, cube of even , quotient for multiple of 7 with java code.

import javax.swing.*;
class Cal
{
                public static void main(String args[])
                {
                                int n;
                                n = Integer.parseInt(JOptionPane.showInputDialog("enter a value: "));
                                if (n%2==0)
                                {
                                System.out.println("cube of even value = " + n*n*n);
                                JOptionPane.showMessageDialog(null, "cube of even value = " + (n*n*n));
                                }
                                else if (n%2!=0)
                                {
                                System.out.println("square of odd value = " + n*n);
                                JOptionPane.showMessageDialog(null, "square of odd value = " + (n*n));
                                }
                                if (n%7 == 0)
                                {
                                System.out.println("quotient for 7 is " + n/7);
                                JOptionPane.showMessageDialog(null,"quotient for 7 is " + (n/7));
                                }
                                System.exit(0);
                }

}

Wednesday, 23 July 2014

Show first 10 Fibonacci numbers in message dialog box with java code.

import javax.swing.*;
class Fibo
{
                public static void main(String[] args){
                int n1 = 0,n2 = 1,n3;
                String st;
                st = n1 + "\n" + n2 + "\n";
                for(int i = 1; i<10; i++){
                                n3 = n1 + n2;
                                st += ( n3 + "\n");
                                n1 = n2;
                                n2 = n3;
                }
                JOptionPane.showMessageDialog(null, st);
                System.exit(0);
                }                             

}