Write a program to return the properties - isPerfectSquare
, isPrimeNumber
, previousNumber
, nextNumber
, square
and cube
- for the given input number.
Input (Integer) |
Output (NumberProperites) |
25 |
isPerfectSquare = true isPrimeNumber = false previousNumber = 25 - 1 = 24 nextNumber = 25 + 1 = 26 square = 25 * 25 = 625 cube = 25 * 25 * 25 = 15625
|
7 |
isPerfectSquare = false isPrimeNumber = true previousNumber = 7 - 1 = 6 nextNumber = 7 + 1 = 8 square = 7 * 7 = 49 cube = 7 * 7 * 7 = 343
|
10 |
isPerfectSquare = false isPrimeNumber = false previousNumber = 10 - 1 = 6 nextNumber = 10 + 1 = 8 square = 10 * 10 = 100 cube = 10 * 10 * 10 = 1000
|
class FindNumberProperties
{
public static void main(String s[])
{
NumberProperties properties = getProperties(7);
System.out.println("Printing the properties of the number 7");
System.out.println("---------------------------------------");
System.out.println("Perfect Square : " + properties.isPerfectSquare);
System.out.println("Prime Number : " + properties.isPrimeNumber);
System.out.println("Previous Number : " + properties.previousNumber);
System.out.println("Next Number : " + properties.nextNumber);
System.out.println("Square : " + properties.square);
System.out.println("Cube : " + properties.cube);
}
public static NumberProperties getProperties(int input)
{
}
}
class NumberProperties
{
boolean isPerfectSquare;
boolean isPrimeNumber;
int previousNumber;
int nextNumber;
int square;
int cube;
}
Topic:
Is Java Pass by Reference or Pass by Value
If you need explanation Read this topic
If you need Answer Take test on this topic
User comments below. All of them might not be correct.
-Here Program is to find properties of a given number.
--Here two classes are used :
1)FindNumberProperties--> with main method ang getproperties method
2)NumberProperties--> Having different attributes defined for a given number.
We have to state 6 properties of number:
we have create object of NumberProperties class as follows:
----->NumberProperties obj=new NumberProperties();
1) isperfectsquare:
perfectsquare numbers are 1 4 9 16 25....
--To find it we have to use the "FOR LOOP" which runs from 1 to number as follows:
--> for(int i=1;i<=input;i++)
--Inside this loop we have to check if (i*i)==no then we have to break as follows.
--> if((i*i)==input)
{
obj.isperfectsquare=true;
break;
}
2)isprime:
To check whether no is prime or not we have to use "FOR LOOP" which runs from 2 to no-1 as any number will get divided with itself and 1.Inside which we have to check if
(no%i==0) then we have to set isprime=true as follows:
----->isprime=true;
for(int i=2;i<input;i++)
{
if(input%i==0)
{
obj.isprime=false;//if number gets divided then it is not prime.
}
}
3)To find previous no we have to subtract 1 from no
---> obj.previousNumber=input-1
4)To find next number we have to add 1 to number
---> obj.NextNumber=input+1
5)To find square of no we have to multipy no by itself
---> obj.square=input*input
6)To find cube of no we have to multipy no by itself two times.
---> obj.square=input*input*input
At last we have to return "obj" object
---> return obj
Posted by Mânïshå Mùlchåndânï 2015-01-23 13:34:56
here we have to find some properties for the given num:
first create instance for the NumberProperties class..assume we have object with the name numprop;
1.isPerfectSquare:
Ex: 4,9,16
2*2=4,3*3=9..
to find perfect num we need to travel using fro loop i=1;i<=num/2;i++) in side check if (i*i==num) set numprop.isPerfectSquare=true and break th loop; coming out of the set numprop.isPerfectSquare= false;
here we are traveling till num/2 Bcz after crossing the half of the num we will get more than num so no need to travel till the num..
2.isPrime:
Ex:1,2,3,5,7
if a num has only 2 factor(1 and num it self) than we can say that num is prime
e: 5 (1,5)
so find factors using the loop and count the factors for a num..if factorscount==2 than set numprop.isPrime=true else numprop.isPrime=false;
3.previousNum;
subtract 1 from the give num
numprop.previousNum=num-1;
4.nextNum;
Add 1 to the give num
numprop.nextNum=num+1;
5.square;
numpropsquare=num*num;
6.
numprop.cube=num*num*num;
finaly return the numprop;
Posted by Uday Kumar 2015-01-26 04:39:35
This dose is now closed and the winners are Mânïshå Mùlchåndânï, for 'First Correct Comment', Uday Kumar, for 'Second Correct Comment'. The 'lucky liker' is Mânïshå Mùlchåndânï. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.
Posted by Merit Campus 2015-01-26 05:25:49