Looping programs
[Link] the code to print all Evennumber for the given start and end value, such that it will print
the even numbers from start till end.
import [Link];
public class Main
public static void main(String[] args)
Scanner scanner = new Scanner([Link]);
long start = [Link]();
long end = [Link]();
long itr;
if (start % 2 == 0) {
itr = start;
} else {
itr = start + 1;
while (itr <= end)
[Link](itr + " ");
itr += 2;
[Link]();
[Link] the code to find sum of even numbers for the given range and print the sum.
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
long st = [Link]();
long end = [Link]();
long sum = 0;
for (long value = st; value <= end; value += 1)
if (value % 2 == 0)
sum = value + sum;
[Link](sum);
[Link]();
[Link] the code that finds the Factorial for the given number.
import [Link];
public class Main {
public static void main(String[] args)
Scanner scanner = new Scanner([Link]);
long num = [Link]();
long fact = 1;
for (long val = 1; val <= num; val++)
fact *= val;
[Link](fact);
[Link] the code that prints the multiplication table as per the given N and multiplicand.
import [Link];
public class Main
{
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
long nlimit = [Link]();
long multiplicand = [Link]();
for (long num = 1; num <= nlimit; num++)
[Link]("%d * %d = %d\n", num, multiplicand, num * multiplicand);
[Link] a code to print consecutive natural numbers from 1 to given input.
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
long input = [Link]();
for (long itr = 1; itr <= input; itr++) {
[Link](itr + " ");
[Link]();
[Link] the code which will check whether given number is perfect square or not.
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
long N = [Link]();
long root;
for (root = 1; root * root < N; root++);
if (root * root == N)
[Link]("Perfect Square");
else
[Link]("Not");
[Link] the code which checks whether the given number is a perfect cube or not.
import [Link];
public class Main
public static void main(String[] args)
Scanner input = new Scanner([Link]);
int number = [Link]();
for (int i = 1; i * i * i <= number; i++)
if (i * i * i == number) {
break;
if ((i*i*i) == number) {
[Link]("Perfect Cube");
} else {
[Link]("Not a Perfect Cube");
[Link] the code that finds the minimum number to be divided to make the number as a perfect
square.
import [Link];
public class Main
{
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int num = [Link]();
int factor = 2;
int count;
int divide = 1;
while (num != 1) {
count = 0;
while (num % factor == 0) {
num = num / factor;
count++;
if (count % 2 == 1) {
divide *= factor;
factor++;
[Link](divide);
[Link] the code for the given a number and a raise value, do raise the value of the given number
to taise value times. i.e. without using the inbuilt pow() inside function.
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
long num = [Link]();
long pow = [Link]();
long ans = 1;
for (int ctr = 1; ctr <= pow; ctr++)
{
ans *= num;
[Link](power(ans));
[Link] the code which will Divide the given two numbers and print the quotient without using
/ operator.
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int num = [Link]();
int divi = [Link]();
int val = 1;
while (divi <= num)
num = num - divi;
val++;
[Link](val-1);
[Link] the code which multiply the given two numbers without using * operator.
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
long number = [Link]();
long multiple = [Link]();
long value;
long sum = 0;
for (value = 1; value <= multiple; value++) {
sum = number + sum;
[Link](sum);
[Link] the code which checks whether the given number is a triangular number.
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int input = [Link]();
int num;
for (num = 1; num <= (input / 2) + 1; num++) {
if ((num * (num + 1)) / 2 == input) {
break;
if ((num * (num + 1)) / 2 == input)
[Link]("Triangular Number");
else
[Link]("Not a Triangular Number");
[Link] all the alphabets from a to z for given N times.
public class Main {
public static void main(String[] args) {
[Link] scanner = new [Link]([Link]);
long inp = [Link]();
for (long val = 1; val <= inp; val++) {
for (char alpha = 'a'; alpha <= 'z'; alpha++) {
[Link](alpha+" ");
[Link]();
[Link] the code which generate the first N terms of the fibonacci series.
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int N = [Link]();
long num1 = 0, num2 = 1;
for (int itr = 1; itr <= N; itr++)
long num3 = num1 + num2;
[Link](num1 + " ");
num1 = num2;
num2 = num3;
[Link] code that print the values from 1 to n except multiples of 4.
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
long input = [Link]();
for (long num = 1; num <= input; num++)
if (num % 4 == 0) {
continue;
[Link](num + " ");
[Link] an input value, count the number of trailing zeros in the factorial of the given number.
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
long num = [Link]();
long zero = 0;
while (num > 0) {
zero += num / 5;
num /= 5;
[Link](zero);
[Link] a code for the given a N, Write a program to generate a series of N values that has 2 and
3 powers alternatively.
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
long N = [Link]();
long two = 1;
long three = 1;
for (long num = 1; num <= N; num = num + 2) {
[Link](two + " ");
two = two * 2;
if (num + 1 <= N) {
[Link](three + " ");
three = three * 3;
[Link] the code for the given N ,which generate a N values of a series where the odd terms are
multiples of 2 and even terms are obtained by dividing previous term by 2.
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
long N = [Link]();
for (long number = 1; number <= N; number++) {
if (number % 2 == 0) {
[Link]((number / 2) - 1 + " ");
} else {
[Link](number - 1 + " ");
[Link] the code for which check whether given number is Pentagonal number. A Pentagonal
number is a number that can be represented using a regular geometric pattern typically formed using
dots that are regularly spaced. A pentagonal number takes the form of a pentagon. The first 6
pentagonal numbers are: 1, 5, 12, 22, 35, 51,.etc
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int num = [Link]();
int var1, power, var2;
if (num == 1 || num == 5) {
[Link]("Pentagonal Number");
} else {
for (var1 = 4, var2 = 5; var2 < num; var1 += 3, var2 += var1);
if (var2 == num) {
[Link]("Pentagonal Number");
} else {
[Link]("Not a Pentagonal Number");
[Link] code to check whether given number is Proth number. If a number can be expressed in
the form of kX2^n + 1, it's called a proth number, where k and n are positive integers and 2^n > k.
Proth number series goes like this: 3, 5, 9, 13, 17, 25, 33, 41, 49, 57, 65, ..
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int num = [Link]();
int power = 1;
num = num - 1;
while (num % 2 == 0) {
num = num / 2;
power = power * 2;
if (num < power) {
[Link]("Proth Number");
} else {
[Link]("Not a Proth Number");
}
21.A person walks in a rectangular path starting from the origin. For the first step he walks 10 feet on
the right. For the next step he walks 20 feet by turning in clockwise direction from the current
position. For the third step he walks 30 feet by turning in clockwise direction from the current
position and it goes on. Given the number of steps calculate the final co-ordinate of the person
starting from the origin.
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int steps = [Link]();
int x = 0, y = 0;
int count = 0, multiply = 1;
while (true) {
if (steps > 0) {
x = x + multiply * 10;
multiply++;
steps--;
if (steps > 0) {
y = y - multiply * 10;
multiply++;
steps--;
if (steps > 0) {
x = x - multiply * 10;
multiply++;
steps--;
if (steps > 0) {
y = y + multiply * 10;
multiply++;
steps--;
if (steps == 0) {
break;
[Link]("(%d , %d)", x, y);
[Link] code for the given a date, calculate the number of days remaining in that year.
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int dd, mm, yy, nod = 0;
long remdays;
dd = [Link]();
mm = [Link]();
yy = [Link]();
switch (mm - 1) {
case 12:
nod += 31;
case 11:
nod += 30;
case 10:
nod += 31;
case 9:
nod += 30;
case 8:
nod += 31;
case 7:
nod += 31;
case 6:
nod += 30;
case 5:
nod += 31;
case 4:
nod += 30;
case 3:
nod += 31;
case 2:
if ((yy % 4 == 0 && yy % 100 != 0) || yy % 400 == 0)
nod += 29;
else
nod += 28;
case 1:
nod += 31;
break;
nod += dd;
if ((yy % 4 == 0 && yy % 100 != 0) || yy % 400 == 0)
remdays = 366 - nod;
else
remdays = 365 - nod;
[Link](remdays);
[Link] the code for given a date, to calculate the number of days completed in that year.
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int dd, mm, yy, nod = 0;
dd = [Link]();
mm = [Link]();
yy = [Link]();
switch (mm - 1) {
case 12:
nod += 31;
case 11:
nod += 30;
case 10:
nod += 31;
case 9:
nod += 30;
case 8:
nod += 31;
case 7:
nod += 31;
case 6:
nod += 30;
case 5:
nod += 31;
case 4:
nod += 30;
case 3:
nod += 31;
case 2:
if ((yy % 4 == 0 && yy % 100 != 0) || yy % 400 == 0)
nod += 29;
else
nod += 28;
case 1:
nod += 31;
break;
nod += dd;
[Link](nod);
[Link] the code for given two time, calculate elapsed time in seconds.
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int hr1, min1, sec1, hr2, min2, sec2;
long time1, time2, diff;
hr1 = [Link]();
min1 = [Link]();
sec1 = [Link]();
hr2 = [Link]();
min2 = [Link]();
sec2 = [Link]();
time1 = hr1 * 3600 + min1 * 60 + sec1;
time2 = hr2 * 3600 + min2 * 60 + sec2;
if (time1 > time2)
diff = time1 - time2;
else
diff = time2 - time1;
}
[Link](diff);
[Link] the code for given two dates, calculate the difference between two dates.
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
int dd, mm, yy, nod = 0, year, total, itr, dd1, mm1, yy1;
dd = [Link]();
mm = [Link]();
yy = [Link]();
dd1 = [Link]();
mm1 = [Link]();
yy1 = [Link]();
if (yy <= yy1 && mm <= 12 && mm1 <= 12 && dd <= 31 && dd1 <= 31) {
for (year = yy + 1; year < yy1; year++) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
nod += 366;
else
nod += 365;
if (yy != yy1) {
for (itr = mm; itr <= 12; itr++) {
switch (itr) {
case 12:
nod += 31;
break;
case 11:
nod += 30;
break;
case 10:
nod += 31;
break;
case 9:
nod += 30;
break;
case 8:
nod += 31;
break;
case 7:
nod += 31;
break;
case 6:
nod += 30;
break;
case 5:
nod += 31;
break;
case 4:
nod += 30;
break;
case 3:
nod += 31;
break;
case 2:
if (yy % 4 == 0 && yy % 100 != 0 || yy % 400 == 0)
nod += 29;
else
nod += 28;
break;
case 1:
nod += 31;
break;
for (itr = 1; itr < mm1; itr++) {
switch (itr) {
case 12:
nod += 31;
break;
case 11:
nod += 30;
break;
case 10:
nod += 31;
break;
case 9:
nod += 30;
break;
case 8:
nod += 31;
break;
case 7:
nod += 31;
break;
case 6:
nod += 30;
break;
case 5:
nod += 31;
break;
case 4:
nod += 30;
break;
case 3:
nod += 31;
break;
case 2:
if (yy1 % 4 == 0 && yy1 % 100 != 0 || yy1 % 400 == 0)
nod += 29;
else
nod += 28;
break;
case 1:
nod += 31;
break;
nod += dd1 - dd;
[Link](nod);
} else {
[Link]("Invalid");