forked from kishanrajput23/Java-Projects-Collections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFactorial_prime_avg.java
94 lines (81 loc) · 1.88 KB
/
Factorial_prime_avg.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package calculate;
import java.util.Scanner;
public class lab2 {
public static void Factorial(int n)
{
int fact = 1;
if(n==0)
return;
for(int i= 1; i<=n; i++)
{
fact = fact*i;
}
System.out.println("Factorial of "+n+" is "+fact+" ");
}
public static void Average(int n)
{
int sum = 0;
for(int i=0; i<=n; i++)
{
sum = sum + i ;
}
int avg = sum/n;
System.out.println("Sum: "+sum+" ");
System.out.println("Average: "+avg+" ");
}
public static void Prime()
{
int counter = 0;
int i=2;
for(i=2; i<=50; i++)
{
for(int j=1; j<=i; j++)
{
if(i%j==0)
counter++;
}
if(counter<=2)
{
System.out.print(i+" ");
counter =0;
}
else
counter=0;
}
System.out.println("");
}
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
int opt,n;
boolean flag = true;
while(flag)
{
System.out.println("choose a option from following: ");
System.out.println("1. Factorial");
System.out.println("2. Prime numbers");
System.out.println("3. Average");
System.out.println("4 for exit");
opt = s.nextInt();
switch(opt)
{
case 1:
System.out.print("Enter number to find factorial: ");
n = s.nextInt();
Factorial(n);
break;
case 2:
Prime();
break;
case 3:
System.out.print("Enter number of itiration: ");
n = s.nextInt();
Average(n);
break;
case 4:
flag = false;
break;
}
}
}
}