-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloops.java
More file actions
170 lines (160 loc) · 4.77 KB
/
loops.java
File metadata and controls
170 lines (160 loc) · 4.77 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import java.util.Scanner;
public class loops {
public static void main(String args[]){
// form a calculator using switch case
Scanner input = new Scanner(System.in); {
System.out.println("this is calculator");
System.out.println("enter number1");
int number1 = input.nextInt();
System.out.println("enter number2");
int number2 = input.nextInt();
System.out.println("enter operation");
char operation = input.next().charAt(0);
switch(operation){
case ('+'):
System.out.println(number1+number2);
break;
case ('-'):
System.out.println(number1-number2);
break;
case ('*'):
System.out.println(number1*number2);
break;
case ('/'):
System.out.println(number1/number2);
break;
case ('%'):
System.out.println(number1%number2);
break;
default:
System.out.println("you have not put valid input");
}
//Write a Java program to input week number(1-7) and print day of week name using switch case
System.out.println("input the day number");
int day = input.nextInt();
switch(day){
case(1):
System.out.println("Monday");
break;
case(2):
System.out.println("tuesday");
break;
case(3):
System.out.println("wednesday");
break;
case(4):
System.out.println("thursday");
break;
case(5):
System.out.println("friday");
break;
case(6):
System.out.println("saturday");
break;
default:
System.out.println("this is not");
}
/*loops : these are the statements through which we can put our work on repeat
//there are three type of loop 1.> while loop 2.> for loop 3.> do while loop
//while loop
//while(condition){
//code
}*/
//1. print value of 10 times:
System.out.println("print the value ten times");
int value1=input.nextInt();
while(value1<=100){
System.out.println("hello , everyone");
value1++;
}
//print number from 1 to n
System.out.println("print the number from 1 to n :");
int n = input.nextInt();
while(n<=100){
System.out.println(n);
n++;
}
int m = input.nextInt();
for(int i = 1; i<=m; i++){
System.out.println(i);
i++;
}
}
for( int i=1; i<=4; i++){
for( int j=1; j<=5; j++){
System.out.print("* ");
}
System.out.println();
}
// sum of first n natural number
System.out.println("THE sum of n natural number are:");
int number1= input.nextInt();
int sum = 0;
int i=1;
while(i<=number1){
sum+=i;
i++;
}
System.out.println("the sum of n number is: "+sum);
// print the reverse of number
System.out.println("enter the number for the reverse print :");
int rev1 = input.nextInt();
while(rev1>0){
int lastDigit= rev1%10;
System.out.println(lastDigit);
rev1 =rev1/10;
}
System.out.println("rev1:"+rev1);
System.out.println(" this is the star pattern :");
//print star pattern
/**
* *
* * *
* * * *
*/
for(int line =1; line<=5; line++){
for(int star =1; star<=line; star++ ){
System.out.print("*");
}
System.out.println();
}
// print star pattern
// * * * *
// * * *
// * *
// *
for(int line =1; line <=4; line++){
for (int star = 1; star<=(4-line+1); star++){
System.out.print("*");
}
System.out.println();
}
// print the pattern
/*1
1 2
1 2 3
1 2 3 4
*/
for (int line = 1; line<=5; line++){
for(int number =1; number<=line; number++){
System.out.print(number);
}
System.out.println();
}
// print the pattern
/* A
B C
D E F
G H I J
K L M N O
*/
char ch='A';
for(int line = 1; line<=5; line++){
for(int chars = 1; chars<=line; chars++){
System.out.print(ch);
ch++;
}
System.out.println();
}
}
}