Showing posts with label square and cube of a number in java. Show all posts
Showing posts with label square and cube of a number in java. Show all posts

Sunday, July 26, 2015

Square and Cube Number Program in Java

This simple program  ask the user to enter a number and then our program will generate the corresponding square and cube value of the given number using for loop statement in Java. The code is very basic for beginners that are new in Java programming.

If you have some questions about programming, about my work please send mu an email at jakerpomperada@gmail.comand jakerpomperada@yahoo.com. People here in the Philippines can contact me at  my mobile number 09173084360.

Thank you very much and Happy Programming.




Sample Prorgam Output

Program Listing

/*
square_array.java
Programmer By: Mr. Jake R. Pomperada, MAED-IT
Date : July 23, 2015
Tools: NetBeans 8.0.2, Java SE 8.0

Problem No. 6:
  
Write a program that will ask the user to give five numbers and then
it will display the square and cube value of the given number by the
user.
*/
              

import java.util.Scanner;

class square_array{
    
public static void main(String[] args) {
      
     Scanner input = new Scanner(System.in);
     char ch;
        
  do { 
      int [] values; 
      values  = new int[5];  
      int x=0;
      System.out.print("SQUARE AND CUBE NUMBER PROGRAM");
      System.out.println();
      for (int a=0; a<5; a++)
      {
         x=1;
         x+=a;
         System.out.print("Enter value in item no. " + x + " : ");
         values[a] = input.nextInt();
        }       
         System.out.println("\n\n"); 
         System.out.print("\tNUMBER     SQUARE      CUBE ");
         for (int a=0; a<5; a++)
          {
             x=1;
             x+=a;
             int square = (values[a] * values[a]);
             int cube  = (values[a] * values[a] * values[a]);
             System.out.println(); 
             System.out.println("\t  "+x+"\t      " + square + " \t" +cube);
          }
     System.out.println(); 
     System.out.print("\nDo you want to continue (Type y or n) : ");
     ch = input.next().charAt(0);                        
     } while (ch == 'Y'|| ch == 'y');  
     System.out.println();
     System.out.print("\t THANK YOU FOR USING THIS PROGRAM");
     System.out.println("\n\n");
  }
} // End of Code