-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask1.java
153 lines (132 loc) · 4.31 KB
/
task1.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import java.util.*;
public class task1
{
static int ch, a, b;
static float x;
public static long fact(int n)
{
long res=1;
for(int i=n;i>=1;i--)
res=res*i;
return res;
}
public static float pow(int y)
{
float pro=1;
for(int i=1;i<=y;i++)
pro=pro*x;
return pro;
}
public static int pow1(int z)
{
int pro1=1;
for(int i=1;i<=z;i++)
pro1=pro1*(-1);
return pro1;
}
public static void main(String args[])
{
System.out.println("Welcome to task 1 - Calculator Program.");
System.out.println("Following are the operations that can be performed; Select the operation, type the number and press Enter:");
Scanner sc=new Scanner(System.in);
System.out.println("1. Addition of 2 variables\n2. Difference between 2 variables");
System.out.println("3. Product of 2 variables\n4. Division of one variable by the other ");
System.out.println("5. Exponentiation of a variable with base e (e^x)\n6. x^y\n7. sin(x)\n8. cos(x)\n9. tan(x)\n10. Exit.");
ch=sc.nextInt();
float quo=0.0f, exp=0.0f, sine=0.0f, cosine=0.0f;
while(ch!=10)
{
switch(ch)
{
case 1:
System.out.println("Enter the values of a and b respectively: ");
a=sc.nextInt();
b=sc.nextInt();
System.out.println("a+b = "+(a+b));
System.out.println("Enter your choice:");
ch=sc.nextInt();
break;
case 2:
System.out.println("Enter the values of a and b respectively: ");
a=sc.nextInt();
b=sc.nextInt();
System.out.println("a-b = "+(a-b));
System.out.println("Enter your choice:");
ch=sc.nextInt();
break;
case 3:
System.out.println("Enter the values of a and b respectively: ");
a=sc.nextInt();
b=sc.nextInt();
System.out.println("a x b = "+(a*b));
System.out.println("Enter your choice:");
ch=sc.nextInt();
break;
case 4:
System.out.println("Enter the values of a and b respectively: ");
a=sc.nextInt();
b=sc.nextInt();
quo=a/b;
System.out.println("a/b = "+quo);
System.out.println("Enter your choice:");
ch=sc.nextInt();
break;
case 5:
System.out.println("Enter the value of x:");
x=sc.nextFloat();
for(int i=1;i<=15;i++)
{
exp=exp+((pow(i-1))/(fact(i-1)));
}
System.out.println("e^x = e^"+x+" = "+exp);
System.out.println("Enter your choice:");
ch=sc.nextInt();
break;
case 6:
System.out.println("Enter the values of x and y respectively:");
x=sc.nextFloat();
int y=sc.nextInt();
System.out.println("x^y = "+x+"^"+y+" = "+pow(y));
System.out.println("Enter your choice:");
ch=sc.nextInt();
break;
case 7:
System.out.println("Enter the value of x:");
x=sc.nextFloat();
for(int i=1;i<=15;i++)
sine=sine+((pow1(i+1))*(pow(2*i-1))/fact(2*i-1));
System.out.println("sin (x) = "+sine);
System.out.println("Enter your choice:");
ch=sc.nextInt();
break;
case 8:
System.out.println("Enter the value of x:");
x=sc.nextFloat();
for(int i=1;i<=015;i++)
cosine=cosine+((pow1(i+1))*(pow(2*i-2))/fact(2*i-2));
System.out.println("cos (x) = "+cosine);
System.out.println("Enter your choice:");
ch=sc.nextInt();
break;
case 9:
System.out.println("Enter the value of x:");
x=sc.nextFloat();;
for(int i=1;i<=15;i++)
sine=sine+((pow1(i+1))*(pow(2*i-1))/fact(2*i-1));
for(int i=1;i<=15;i++)
cosine=cosine+((pow1(i+1))*(pow(2*i-2))/fact(2*i-2));
System.out.println("tan (x) = "+(sine/cosine));
System.out.println("Enter your choice:");
ch=sc.nextInt();
break;
default: System.out.println(ch+" is an invalid choice.");
System.out.println("Enter your choice:");
ch=sc.nextInt();
break;
}
}
if(ch==10)
System.out.println("Exiting...");
sc.close();
}
}