File tree Expand file tree Collapse file tree 10 files changed +166
-0
lines changed Expand file tree Collapse file tree 10 files changed +166
-0
lines changed Original file line number Diff line number Diff line change 1+ import java .util .Scanner ;
2+
3+ public class Assignment {
4+
5+ static int fact (int n ) {
6+ if (n == 0 ) { return 1 ; }
7+ return n * fact (n -1 );
8+ }
9+
10+ static void table (int n ) {
11+ for (int i =1 ; i <=10 ; i ++) {
12+ System .out .println (n +" * " +i +" = " +(n *i ));
13+ }
14+ }
15+
16+ public static void main (String [] args ) {
17+ Scanner sc = new Scanner (System .in );
18+
19+ // Question 1
20+ for (int i =0 ; i <5 ; i ++) {
21+ System .out .println ("Hello" );
22+ i += 2 ; // 0, 3 5<5 false
23+ }
24+
25+ // Question 2
26+ // Prints the sum of the even and odd integers
27+ int num = 5 ;
28+ int even = 0 , odd = 0 ;
29+ for (int i =1 ; i <=num ; i ++) {
30+ if (i %2 == 0 ) {
31+ even += i ;
32+ } else {
33+ odd += i ;
34+ }
35+ }
36+ System .out .println (even +" " +odd );
37+
38+ // Question 3
39+ // Factorial using function to reduce the complexity
40+ System .out .println (fact (num ));
41+
42+ // Question 4
43+ // table of N number
44+ table (num );
45+ }
46+ }
Original file line number Diff line number Diff line change 1+ public class ContinueKeyword {
2+ public static void main (String [] args ) {
3+ // dont print 10 ever
4+ int num = 19 ;
5+ for (int i =1 ; i <=num ; i ++) {
6+ if (i == 10 ) {
7+ continue ;
8+ }
9+ System .out .println (i );
10+ }
11+ }
12+ }
Original file line number Diff line number Diff line change 1+ import java .util .Scanner ;
2+
3+ public class MultiplicationOf10 {
4+ public static void main (String [] args ) {
5+ do {
6+ Scanner sc = new Scanner (System .in );
7+ int num = sc .nextInt ();
8+
9+ if (num %10 == 0 ) {
10+ break ;
11+ }
12+ System .out .println (num );
13+
14+ } while (true );
15+ }
16+ }
Original file line number Diff line number Diff line change 1+ import java .util .Scanner ;
2+
3+ public class Prime {
4+ public static void main (String [] args ) {
5+ Scanner sc = new Scanner (System .in );
6+
7+ int num = sc .nextInt ();
8+ int count = 0 ;
9+
10+ for (int i =1 ; i <num ; i ++) {
11+ if (num %i == 0 ) {
12+ count ++;
13+ }
14+ }
15+
16+ if (count == 1 ) {
17+ System .out .println ("Prime" );
18+ } else {
19+ System .out .println ("Not Prime" );
20+ }
21+ }
22+ }
Original file line number Diff line number Diff line change 1+ public class PrintNo1to10 {
2+ public static void main (String [] args ) {
3+ int i =1 ;
4+ do {
5+ System .out .println (i );
6+ i ++;
7+ } while (i <= 10 );
8+ }
9+ }
Original file line number Diff line number Diff line change 1+ import java .util .Scanner ;
2+
3+ public class PrintNo1toN {
4+ public static void main (String [] args ) {
5+ Scanner sc = new Scanner (System .in );
6+
7+ int num = sc .nextInt (), i =1 ;
8+ while (i <= num ) {
9+ System .out .println (i );
10+ i ++;
11+ }
12+ }
13+ }
Original file line number Diff line number Diff line change 1+ public class PrintSquarePattern {
2+ public static void main (String [] args ) {
3+ int n = 4 , i =1 ;
4+ while (i <= n ) {
5+ int j =1 ;
6+ while (j <= n ) {
7+ System .out .print ("*" );
8+ j ++;
9+ }
10+ i ++;
11+ System .out .println ();
12+ }
13+ }
14+ }
Original file line number Diff line number Diff line change 1+ public class ReverseGivenNumber {
2+ public static void main (String [] args ) {
3+ int x = 2345 , reverse =0 ;
4+ while (x >0 ) {
5+ int lastDigit = x %10 ;
6+ reverse = (reverse * 10 ) + lastDigit ;
7+ x /= 10 ;
8+ }
9+ System .out .println (reverse );
10+ }
11+ }
Original file line number Diff line number Diff line change 1+ public class ReverseOfNumber {
2+ public static void main (String [] args ) {
3+ int n = 3578 ;
4+ while (n > 0 ) {
5+ int reminder = n %10 ;
6+ System .out .print (reminder );
7+ n = n /10 ;
8+ }
9+ }
10+ }
Original file line number Diff line number Diff line change 1+ import java .util .Scanner ;
2+
3+ public class SumOfFirstNNaturalNum {
4+ public static void main (String [] args ) {
5+ Scanner sc = new Scanner (System .in );
6+
7+ int x = sc .nextInt (), sum =0 ;
8+ for (int i =1 ; i <=x ; i ++) {
9+ sum += i ;
10+ }
11+ System .out .println (sum );
12+ }
13+ }
You can’t perform that action at this time.
0 commit comments