Calculates the odd and even numbers within 100 with a while loop and
public class test{
public static void Main (string[] args) {
int sum=0;
int num=0;
for (int i=0;i<100;i++) {
if (i%2==0) {
Sum+=i;
}else{
Num+=i;
}
}
System.out.println ("The number of even-numbered and:"+sum);
System.out.println ("The number of odd-numbered and:"+num);
}
}
All the numbers divisible by 5 in the loop output 1000, and output up to 3
public void test{
int i=0;
int j=0;
while (i<1000) {//condition
if (i%5==0) {//Can be divisible by 5
System.out.println (i+ "\ t");
j + +;
if (j%3==0) {//three X
System.out.println ("\ n");
j=0;
}
}
i++;
}
}
99 Multiplication Table
public void Test () {
for (int i=1;i<10;i++) {
for (int j=1;j<=i;j++) {
SYSTEM.OUT.PRINTLN (j+ "*" +i+ "=" + (I*J<10))
}
}
System.out.println ();
}
Factorial:
public void num () {
int sum=0;
for (int i=1;i<=100;i++) {
int temp=0;
for (int j=1;j<=i,j++) {
Temp+=j;
}
Sum+=temp;
}
SYSTEM.OUT.PRINTLN (sum);
}
/**
* Programming: 1! +2! + ... +15!
*/
void Addfactorial () {
Long result = 0;
for (int i=1;i<=15;i++) {
int temp = 1;
for (int j=1;j<=i;j++) {
Temp *=j;
}
Result +=temp;
}
SYSTEM.OUT.PRINTLN (result);
}
}
Enter a string that counts the number of uppercase and lowercase letters;
public static void Main (string[] args) {
String str= "SADASWQRTGDSGDFHGDF";
int lowe = 0;
int upper = 0;
for (int i=0;i<str.lenth (); i++) {
Char c =str.charat (i);
if (c>= ' a ' &&c<= ' Z ') {
lowe++;
}
if (c>= ' A ' &&c<= ' Z ') {
upper++;
}
}
System.out.printf (" Number of capital letters:%d", upper);
System.out.printf (" number of lowercase letters:%d", Lowe);
}
Java Loop Example