Skip to content

Commit a78f3d5

Browse files
authored
Add files via upload
1 parent d2c7538 commit a78f3d5

File tree

2 files changed

+192
-0
lines changed

2 files changed

+192
-0
lines changed

Assignment-8/Number1.java

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
* PROGRAM : Create a package named "com.myjava.pac" that contains a class "Number" with instance vairable x, constructor(s)
3+
and methods isZero(), isPositive(), isNegative(), isEven(), isOdd(), isPrime(), isArmstrong() those returning a boolean type value
4+
and getSquare(), getSqrt(), getCube(), those returns double type value and printFactors(), printPrimeFactors() those returns nothing.
5+
WAP in java to implement it in the program.
6+
* FILE : Number.java
7+
* CREATED BY : Santosh Hembram
8+
* DATE : 09-10-20
9+
*/
10+
11+
package com.myjava.pac;
12+
import java.lang.Math;
13+
14+
public class Number1 {
15+
16+
int x;
17+
18+
public Number1(int x) {
19+
20+
this.x = x;
21+
System.out.println("------------- The Number is stored through Parameterized constructor --------------");
22+
}
23+
24+
public boolean isZero() {
25+
26+
if (x==0)
27+
return true;
28+
else
29+
return false;
30+
}
31+
32+
public boolean isPositive() {
33+
34+
if (x>0)
35+
return true;
36+
else
37+
return false;
38+
}
39+
40+
public boolean isNegative() {
41+
if (x<0)
42+
return true;
43+
else
44+
return false;
45+
}
46+
47+
public boolean isEven() {
48+
if (x%2==0) {
49+
return true;
50+
}
51+
else
52+
return false;
53+
}
54+
public boolean isOdd(){
55+
if (x%2==1) {
56+
return true;
57+
}
58+
else
59+
return false;
60+
}
61+
public boolean isPrime(){
62+
int i,p=0,temp = x ;
63+
for(i=1;i<=temp;i++)
64+
if(temp%i==0)
65+
p++;
66+
if(p==2)
67+
return true;
68+
else
69+
return false;
70+
}
71+
public boolean isArmstrong(){
72+
int temp,sum=0,digit;
73+
74+
temp=x;
75+
while(temp!=0)
76+
{
77+
digit=temp%10;
78+
sum=(digit*digit*digit)+sum;
79+
temp/=10;
80+
}
81+
if(x==sum)
82+
return true;
83+
else
84+
return false;
85+
}
86+
public double getSquare() {
87+
return Math.pow(x,2) ;
88+
}
89+
public double getCube() {
90+
return Math.pow(x,3) ;
91+
}
92+
public double getSqrt() {
93+
return Math.sqrt(x) ;
94+
}
95+
96+
public void printFactors() {
97+
98+
System.out.print("Factors of " + x + " are: ");
99+
100+
int copyX = x;
101+
102+
for (int i = 1; i <= copyX; ++i) {
103+
104+
if (copyX % i == 0) {
105+
System.out.print(i + " ");
106+
}
107+
}
108+
}
109+
110+
public void printPrimeFactors() {
111+
112+
System.out.println("\nPrime Factors of "+x+" are:");
113+
for(int i = 2; i< x; i++) {
114+
while(x%i == 0) {
115+
System.out.println(i+" ");
116+
x = x/i;
117+
}
118+
}
119+
if(x >2) {
120+
System.out.println(x);
121+
}
122+
}
123+
124+
125+
126+
127+
128+
129+
}

Assignment-8/ProgramTest.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import com.myjava.pac.*;
2+
import java.util.Scanner;
3+
class ProgramTest {
4+
5+
public static void main(String[] args) {
6+
7+
int num;
8+
System.out.print("Enter a number: ");
9+
Scanner sc = new Scanner(System.in);
10+
num = sc.nextInt();
11+
12+
Number1 obj = new Number1(num);
13+
14+
if (obj.isZero() == true)
15+
System.out.println("The number is zero");
16+
else
17+
System.out.println("The number is not zero");
18+
19+
if (obj.isPositive() == true)
20+
System.out.println(num+" is a positive number");
21+
else
22+
System.out.println(num+" is not a positive number");
23+
24+
if (obj.isNegative() == true)
25+
System.out.println(num+" is a negative number");
26+
else
27+
System.out.println(num+" is not a negative number");
28+
29+
if (obj.isEven() == true)
30+
System.out.println(num+" is an even number");
31+
else
32+
System.out.println(num+" is not an even number");
33+
34+
if (obj.isOdd() == true)
35+
System.out.println(num+" is an odd number");
36+
else
37+
System.out.println(num+" is not an odd number");
38+
39+
if (obj.isPrime() == true)
40+
System.out.println(num+" is a prime number");
41+
else
42+
System.out.println(num+" is not an prime number");
43+
44+
if (obj.isArmstrong() == true)
45+
System.out.println(num+" is an Armstrong number");
46+
else
47+
System.out.println(num+" is not an Armstrong number");
48+
49+
double sq = obj.getSquare();
50+
double cube = obj.getCube();
51+
double sqRoot = obj.getSqrt();
52+
53+
System.out.println("The Square of "+num+" is: "+sq);
54+
System.out.println("The cube of "+num+" is: "+cube);
55+
System.out.println("The Square root of "+num+" is: "+sqRoot);
56+
57+
obj.printFactors();
58+
59+
obj.printPrimeFactors();
60+
61+
}
62+
63+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy