-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathSquareSum.java
36 lines (31 loc) · 1.11 KB
/
SquareSum.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
* Write a program to define a static method sum_Squares( ) to find the sum of the
* squares of the first 'n' natural numbers and a non-static method square_Sum( ) to
* find the square of the sum of those 'n' natural numbers. Invoke these methods from
* main( ) method to evaluate the difference between the values returned by them.
*/
import java.util.*;
class SumDigits {
public static int sum_Squares(int n) {
int i, sum = 0;
for (i = 1; i <= n; i++)
sum += i * i;
return sum;
}
public int square_Sum(int n) {
int i, sum = 0;
for (i = 1; i <= n; i++)
sum += i;
return sum * sum;
}
}
public class SquareSum {
public static void main(String[] args) {
System.out.print("Enter number: ");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
System.out.println("Sum of squares of " + num + " using static method is " + SumDigits.sum_Squares(num));
System.out
.println("Square of sum of " + num + " using non-static method is " + new SumDigits().square_Sum(num));
}
}