DEV Community

Pavithra C
Pavithra C

Posted on

Day:36-While loop practice2

EXAMPLE1:

package While;

public class Factorial {
    public static void main(String[] args) {
        int factorial =1;
        int no =6;

        while (no>=1) {
            factorial =factorial*no;
            no=no-1;

        }  
        System.out.println(factorial);
    }

}
Enter fullscreen mode Exit fullscreen mode

Output:
720

EXAMPLE2:

package While;

public class Choco { 

 public static void main(String[] args) {
    int choco =1;
    int part = 5;

    while (part>0) {
        choco =choco *2;
        part =part-1;
        System.out.println(choco);
    } 

}
}
Enter fullscreen mode Exit fullscreen mode

Output:
2
4
8
16
32

EXAMPLE3:

package While;

public class Choco2 {
    public static void main(String[] args) {
        int choco =128;
        int kids_count=0;

        while (choco>1) {
            choco=choco/2;
            kids_count =kids_count+1;

        }
        System.out.println(kids_count);
    }
}
Enter fullscreen mode Exit fullscreen mode

OUTPUT:
7

EXAMPLE4:

package While;

public class Ft {
    public static void main(String[] args) {
        float total_fit=30;
        int up=2;
        float down=1.5f;
        int day=1;

        while(total_fit>1) {
            total_fit=(total_fit-up+down);
            day=day+1;

        }
        System.out.println(day);


    }

}
Enter fullscreen mode Exit fullscreen mode

OUTPUT:
59

Task:

package While;

public class Hour {
public static void main(String[] args) {
    int hour=1;
    int minutes=0;
    while (hour < 12) {
        hour = hour + 1;
        minutes = minutes + 5;
    }

    int result = (int)(hour - (minutes / 60.0));
    System.out.println(result);
}
}
Enter fullscreen mode Exit fullscreen mode

Output:
11

Top comments (0)