[go: up one dir, main page]

0% found this document useful (0 votes)
20 views5 pages

Java Practice Book Week1 Loops

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views5 pages

Java Practice Book Week1 Loops

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

■ Java Practice Book – Week 1

Topic: Loops
Loops are used to execute a block of code multiple times.

■ Types of Loops in Java:


- for loop: when number of iterations is known.
- while loop: when condition is checked before execution.
- do-while loop: executes at least once, condition checked after.

■ Syntax:
for(initialization; condition; update){ ... }
while(condition){ ... }
do { ... } while(condition);
■ Practice Questions and Solutions (Loops)
Q1. Print numbers from 1 to 10.
class Loop1 {
public static void main(String[] args) {
for(int i=1;i<=10;i++) System.out.print(i+" ");
}
}

Q2. Print sum of first N natural numbers.


class SumN {
public static void main(String[] args) {
int n=10,sum=0;
for(int i=1;i<=n;i++) sum+=i;
System.out.println("Sum="+sum);
}
}

Q3. Print multiplication table of a given number.


class Table {
public static void main(String[] args) {
int n=5;
for(int i=1;i<=10;i++) System.out.println(n+"*"+i+"="+(n*i));
}
}

Q4. Print all even numbers between 1 and 100.


class EvenNums {
public static void main(String[] args) {
for(int i=2;i<=100;i+=2) System.out.print(i+" ");
}
}

Q5. Print all odd numbers between 1 and 100.


class OddNums {
public static void main(String[] args) {
for(int i=1;i<=100;i+=2) System.out.print(i+" ");
}
}

Q6. Find factorial of a number.


class Factorial {
public static void main(String[] args) {
int n=5,fact=1;
for(int i=1;i<=n;i++) fact*=i;
System.out.println("Factorial="+fact);
}
}

Q7. Check whether a number is prime or not.


class PrimeCheck {
public static void main(String[] args) {
int n=29,flag=1;
for(int i=2;i<=n/2;i++) if(n%i==0){flag=0; break;}
if(flag==1) System.out.println("Prime"); else System.out.println("Not Prime");
}
}

Q8. Find sum of digits of a number.


class DigitSum {
public static void main(String[] args) {
int n=1234,sum=0;
while(n>0){sum+=n%10; n/=10;}
System.out.println("Sum="+sum);
}
}

Q9. Reverse a number.


class ReverseNum {
public static void main(String[] args) {
int n=1234,rev=0;
while(n>0){rev=rev*10+n%10; n/=10;}
System.out.println("Reverse="+rev);
}
}

Q10. Check whether a number is palindrome.


class Palindrome {
public static void main(String[] args) {
int n=121,temp=n,rev=0;
while(n>0){rev=rev*10+n%10; n/=10;}
if(temp==rev) System.out.println("Palindrome"); else System.out.println("Not Palindrome");
}
}

Q11. Print Fibonacci series up to N terms.


class Fibonacci {
public static void main(String[] args) {
int n=10,a=0,b=1;
for(int i=1;i<=n;i++){System.out.print(a+" "); int c=a+b;a=b;b=c;}
}
}

Q12. Find GCD of two numbers.


class GCD {
public static void main(String[] args) {
int a=36,b=60;
while(a!=b){if(a>b)a-=b;else b-=a;}
System.out.println("GCD="+a);
}
}

Q13. Find LCM of two numbers.


class LCM {
public static void main(String[] args) {
int a=12,b=15;
int lcm=(a*b)/gcd(a,b);
System.out.println("LCM="+lcm);
}
static int gcd(int x,int y){
while(y!=0){int t=y;y=x%y;x=t;} return x;
}
}

Q14. Check whether a number is Armstrong number.


class Armstrong {
public static void main(String[] args) {
int n=153,sum=0,temp=n;
while(n>0){int d=n%10; sum+=d*d*d; n/=10;}
if(sum==temp) System.out.println("Armstrong"); else System.out.println("Not Armstrong");
}
}

Q15. Check whether a number is perfect number.


class PerfectNum {
public static void main(String[] args) {
int n=28,sum=0;
for(int i=1;i<n;i++) if(n%i==0) sum+=i;
if(sum==n) System.out.println("Perfect Number"); else System.out.println("Not Perfect");
}
}

Q16. Print all factors of a number.


class Factors {
public static void main(String[] args) {
int n=20;
for(int i=1;i<=n;i++) if(n%i==0) System.out.print(i+" ");
}
}

Q17. Print numbers in reverse order from N to 1.


class ReverseOrder {
public static void main(String[] args) {
int n=10;
for(int i=n;i>=1;i--) System.out.print(i+" ");
}
}

Q18. Calculate power of a number (a^b).


class Power {
public static void main(String[] args) {
int a=2,b=5,res=1;
for(int i=1;i<=b;i++) res*=a;
System.out.println("Result="+res);
}
}

Q19. Print pattern of stars (triangle).


class StarPattern {
public static void main(String[] args) {
for(int i=1;i<=5;i++){for(int j=1;j<=i;j++) System.out.print("*"); System.out.println();}
}
}

Q20. Print pattern of numbers (pyramid).


class NumberPyramid {
public static void main(String[] args) {
for(int i=1;i<=5;i++){for(int j=1;j<=i;j++) System.out.print(j+" "); System.out.println();}
}
}
■ Mini Project: Pattern Generator
Description: Write a program that takes an integer input and prints different star patterns (pyramid,
inverted pyramid, diamond).
import java.util.Scanner;
class PatternGenerator {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
// Pyramid
for(int i=1;i<=n;i++){
for(int j=i;j<n;j++) System.out.print(" ");
for(int j=1;j<=2*i-1;j++) System.out.print("*");
System.out.println();
}
// Inverted Pyramid
for(int i=n;i>=1;i--){
for(int j=i;j<n;j++) System.out.print(" ");
for(int j=1;j<=2*i-1;j++) System.out.print("*");
System.out.println();
}
}
}

You might also like