0% found this document useful (0 votes)
3 views

Looping Problems

The document contains Java programs that solve various looping problems, including checking for perfect squares and cubes, dividing and multiplying without using specific operators, identifying triangular numbers, printing the alphabet multiple times, generating Fibonacci series, and printing numbers while skipping multiples of 4. Each program is structured with input handling and relevant logic to achieve the desired output. The examples demonstrate fundamental programming concepts such as loops, conditionals, and arithmetic operations.

Uploaded by

HARISH S ECE
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)
3 views

Looping Problems

The document contains Java programs that solve various looping problems, including checking for perfect squares and cubes, dividing and multiplying without using specific operators, identifying triangular numbers, printing the alphabet multiple times, generating Fibonacci series, and printing numbers while skipping multiples of 4. Each program is structured with input handling and relevant logic to achieve the desired output. The examples demonstrate fundamental programming concepts such as loops, conditionals, and arithmetic operations.

Uploaded by

HARISH S ECE
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

Looping Problems

1. Check whether given input is Perfect Square or not:

import java.util.*;

class PerfectSquare{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
long a = sc.nextLong();
long i= 1;

while(i*i<a){
i++;
}
if(i*i==a){
System.out.println("Perfect Squre");
}
else{
System.out.println("Not Perfect Square");
}

}
}

2. Check whether given input is Perfect Cube or not:

import java.util.Scanner;

public class PerfectSquare{


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
long N = scanner.nextLong();
long root = 1;
while (root * root * root < N) {
root++;
}

if (root * root * root == N)


System.out.println("Perfect Cube");
else
System.out.println("Not a Perfect Cube");
}
}

3. Divide two give numbers & print the quotient value without using ( / ) operator.

import java.util.*;

class Main{
public static void main(String[] args ){
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int res = 0;

// Use repeated subtraction to calculate the quotient


while(a >= b){
a = a - b;
res = res + 1;
}

System.out.println("Quotient: " + res);


}
}

4. Multiply two given inputs without using * operator.


import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
long number = scanner.nextLong();
long multiple = scanner.nextLong();
long value;
long sum = 0;

for (value = 1; value <= multiple; value++) {


sum = number + sum;
}

System.out.println(sum);
}
}

5. Check whether the given number is triangular number.


import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int input = scanner.nextInt();
int num, flag = 0;

for (num = 1; num <= (input / 2) + 1; num++) {


if ((num * (num + 1)) / 2 == input) {
flag = 1;
break;
}
}

if (flag == 1)
System.out.println("Triangular Number");
else
System.out.println("Not a Triangular Number");
}
}

6. Print all the alphabet from a to zfor given n times.

public class Main {


public static void main(String[] args) {
java.util.Scanner scanner = new java.util.Scanner(System.in);
long inp = scanner.nextLong();

for (long val = 1; val <= inp; val++) {


for (char alpha = 'a'; alpha <= 'z'; alpha++) {
System.out.print(alpha+" ");
}
System.out.println();
}
}
}

7. Fibonacci Series accept an integer N and generate the First n terms of the
Fibonacci Series.
import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of terms (N) in the Fibonacci series: ");
int n = scanner.nextInt();

int first = 0, second = 1;

System.out.print(first+" ");
System.out.print(second+ " ");
for (int i = 1; i <= n; i++) {
int next = first + second;
System.out.print(next + " ");

first = second;
second = next;

}
}
}

8. Write a program to print the values from 1 to n except multiples of 4. Use


continue statement to skip 4.
import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
long input = scanner.nextLong();

for (long num = 1; num <= input; num++) {


if (num % 4 == 0) {
continue;
}
System.out.print(num + " ");
}
}
}

You might also like