Generate Leap years
- Given two numbers, determine whether they are valid years, and if so, list all leap between them.
import
java.util.Scanner;
public class
GenerateLeap {
private static
Scanner sc;
public int
valid(int year) {
if(year>0)
{
System.out.println(year+"
is a valid year.");
return 1;
}
else {
System.out.println(year+"
is not a valid year.");
return 0;
}
}
public static void
main(String[] args) {
int
yearmin,yearmax,year1,year2,flag=0;
sc=new
Scanner(System.in);
System.out.println("Enter
two numbers:");
yearmin=sc.nextInt();
yearmax=sc.nextInt();
GenerateLeap ob=new
GenerateLeap();
year1=ob.valid(yearmin);
year2=ob.valid(yearmax);
if(yearmin>yearmax)
{
int
temp=yearmin;
yearmin=yearmax;
yearmax=temp;
}
if(year1==1&&year2==1)
{
for(int
i=yearmin+1;i<yearmax;i++) {
if((i%400==0)||(i%4==0&&i%100!=0))
{
if(flag==0)
{
System.out.println("The
leap years between "+yearmin+" and "+yearmax+"
are:");
}
System.out.println(i);
flag=1;
}
}
if(flag==0)
{
System.out.println("There
are no leap years between "+yearmin+"
and "+yearmax+".");
}
}
else {
System.out.println("Cannot
find leap years between invalid years.");
}
}
}
Output
Enter
two numbers:
2000
2013
2000
is a valid year.
2013
is a valid year.
The
leap years between 2000 and 2013 are:
2004
2008
2012
-----------------------------------------------------------------------------------------------
- Program that accepts an integer from the user and print the integer with reverse digits. For eg: rev(1234) = 4321
import java.util.Scanner;
public class
ReverseInt {
public void
revInt(int num) {
int
j,digit=0;
System.out.println("The
reversed integer is ");
while(num>0)
{
j=num%10;
digit=(digit*10)+j;
num=num/10;
}
System.out.println(digit);
}
private static
Scanner sc;
public static void
main(String[] args) {
sc=new
Scanner(System.in);
System.out.println("Enter
an integer:");
int
num=sc.nextInt();
ReverseInt ob=new
ReverseInt();
ob.revInt(num);
}
}
Output
Enter
an integer:
54789
The
reversed integer is
98745
--------------------------------------------------------------------------------------------
No comments:
Post a Comment