[Go to site: main page, start]

0% found this document useful (0 votes)
15 views8 pages

Java Programs for Basic Logic Checks

The document contains multiple Java programs that perform various tasks such as checking if a character is a vowel or consonant, determining if a number is single or double digit, identifying the largest of three numbers, and verifying if a triangle is equilateral, isosceles, or scalene. It also includes programs for checking divisibility, determining if a number is positive, negative, or zero, and validating voting eligibility based on age. Additionally, it features calculations for areas of geometric shapes and checks for leap years and even/odd numbers.

Uploaded by

vedanshisharma
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)
15 views8 pages

Java Programs for Basic Logic Checks

The document contains multiple Java programs that perform various tasks such as checking if a character is a vowel or consonant, determining if a number is single or double digit, identifying the largest of three numbers, and verifying if a triangle is equilateral, isosceles, or scalene. It also includes programs for checking divisibility, determining if a number is positive, negative, or zero, and validating voting eligibility based on age. Additionally, it features calculations for areas of geometric shapes and checks for leap years and even/odd numbers.

Uploaded by

vedanshisharma
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

//WAP to enter a character and print it is vowel or consonant

public class Ex7_1


{
static void main(char ch)
{
if(ch=='a' ||ch=='e' ||ch=='i' ||ch=='o' ||ch=='u')
[Link]("Vowel");
else
[Link]("Consonent");

}
}

Write a program to input a number and find out if it is single digit or a double digit
number. Display the output with proper messages

public class Ex7_2


{
static void main(int n)
{
if(n>0 && n<10)
[Link]("Single Digit");
else if(n>9 && n<100)
[Link]("Double Digit");
}

//WAP to input a character and check whether it is a capital letter or a small letter

public class Ex7_3


{
static void main(char ch)
{
if(ch>=65 && ch<=90)
[Link]("Capital letter");
else
[Link]("Small Case");
}

}
//WAP to accept three numbers from the user and print which is the largest number
public class Ex7_2_1
{
static void main(int n1, int n2, int n3)
{
if(n1>n2 && n1>n3)
[Link]("First is greatest");
else if(n2>n1 && n2>n3)
[Link]("Second is greatest");
else
[Link]("Third is greatest");
}
}

//Write a program to accept three sides of a triangle and check if the triangle is
equilateral, isosceles or scalene.

public class Ex7_2_2


{
static void main(int s1, int s2, int s3)
{
if(s1==s2 && s2==s3)
[Link]("Equilateral");
else if(s1==s2 || s2==s3 || s1==s3)
[Link]("Isosceles ");
else
[Link]("Scalene");
}
}
//Write a program to input a number and find out if it is single digit, double digit, 8 three
digit or more than three digits. Display the output with proper messages.
public class Ex7_2_3
{
static void main(int n)
{
if(n>0 && n<10)
[Link]("Single Digit");
else if(n>=10 && n<100)
[Link]("Double Digit");
else if(n>=100 && n<1000)
[Link]("Three Digit");
else
[Link]("More than three digit number");
}
}

/*WAP to accept a number and check


1. Whether it is divisible by 3 and 7.
2. Whether it is divisible by 3 and not by 7.
3. Whether it is divisible by 7 and not by 3.
*/
public class Ex7_2_4
{
static void main(int n)
{
if(n%3==0 && n%7==0)
[Link]("Number is divisible by 3 and 7");
else if(n%3==0 && n%7 !=0)
[Link]("Number is divisible by 3 and not by 7");
else if(n%7==0 && n%3 !=0)
[Link]("Number is divisible by 7 and not by 3");
}
}

Write a program to find maximum between two numbers


public class abc
{
static void main(int n1, int n2)
{
if(n1>n2)
[Link]("First is greatest");
else
[Link]("Second is greatest");
}

//WAP to accept a number and print whether it is positive or negative or zero

public class if2


{
static void main(int n)
{
if (n==0)
[Link]("Number is zero");
else if(n>0)
[Link]("Number is Positive");
else
[Link]("Number is Negative");
}
}

//if - else statement in Java


//WAP to accept age and print eligible for voting

public class if1


{
static void main(int age)
{
if (age>=18)
[Link]("Eligible for voting");
else
[Link]("Not eligible for voting");
}
}
Write a Java program to accept percentage and check for the following;
• More than 35 and less than 45, display the message pass class
• More than 45 and less than 59, display the message second class
• More than or equal to 60, display first class
public class rev4
{
static void main(int p)
{
if(p>=35 && p<45)
[Link]("Pass Class");
Else if(p>=45&& p<=59)
[Link]("Second Class");
else
[Link]("First Class");
}
}

Write a Java program to accept the side, base and hypotenuse from the user and check if
right angle triangle can be formed. Display proper messages. (Formula :- side*side +
base * base = hypotenuse * hypotenuse)
public class rev3
{
static void main(int s, int b, int h)
{
if( s*s + b*b == h*h)
[Link]("Right angle triangle can be formed");
else
[Link]("Right angle triangle cannot be formed");
}
}

//Write a program to check whether a number is even or odd


public class abc
{
public static void main(int a)
{
if(a%2==0)
[Link]("Even Number");
else
[Link]("Odd Number");
}

}
//Write a program to check whether a year is leap year or not
public class abc
{
public static void main(int y)
{
if(y%4==0)
[Link]("Leap Year");
else
[Link]("Not a Leap Year");
}

//Write a program to check whether a character is alphabet or not


public class abc
{

public static void main(char a)


{
if((a>=65&&a<=90)||(a>=97&&a<=122))
[Link]("It is Alphabet");
else
[Link]("It is not Alphabet");
}

Practice
//WAP to print square and cube of a number entered by the user
//WAP to print area of a rectangle by using function argument method
//WAP to print area of a circle

public class practice


{
static void main(int r)
{ float p=3.14f;
[Link]("Area = "+(p*r*r));

}}

{
static void main(int a)
{
//int a =5;
int sq=a*a;
[Link]("Square = "+sq);

}
}

//Mixed and Pure Expression in Java


public class PureMix
{
static void main()
{
int a=5;
int b=10;
char d='a';
float c=10.25f;
[Link](a+b);
[Link](a+c);
[Link](a+d);
}
}

public class ImpConcepts


{
static void main()
{

//this is to explain use of + sign

[Link]("1 + 2 = " + 1 + 2);


[Link]("1 + 2 = " + (1 + 2));
/*1 + 2 = 12 and 1 + 2 = 3, respectively. If either (or both) of the
operands of the + operator is a string, the other is automatically cast to a
string.*/
[Link](1 + 2 + "abc");
[Link]("abc" + 1 + 2);

/**3abc and abc12, respectively. The + operator is left-to-right


associative, whether it is string concatenation or addition
*/

}
}

Common questions

Powered by AI

The program checks if the age is 18 or more to determine eligibility, as the minimum age criterion for voting is set at 18. It prints 'Eligible for voting' if the criterion is met, otherwise 'Not eligible for voting' .

The program checks the range in which the number falls. If it's between 1 and 9, it is a 'Single Digit'. If between 10 and 99, it's a 'Double Digit'. For numbers between 100 and 999, it's a 'Three Digit', and anything beyond is labeled as 'More than three digit number' .

The program demonstrates that when operands of the + operator include a string, the rest are cast to strings and concatenated. For instance, '1 + 2 = ' + 1 + 2 produces '1 + 2 = 12', whereas '1 + 2 = ' + (1 + 2) results in '1 + 2 = 3'. Operations follow the left-to-right associative property .

The program classifies a triangle based on the equality of its sides. If all three sides are equal, it outputs 'Equilateral'. If only two sides are equal, it is 'Isosceles'. If all three sides are different, it classifies the triangle as 'Scalene' .

The program checks if a given character matches any of 'a', 'e', 'i', 'o', 'u'. If so, it identifies the character as a 'Vowel'; otherwise, it's a 'Consonant'. The scope of identification is limited to lowercase vowels only .

The program evaluates divisibility by checking the remainder when the number is divided by 3 and 7. It looks for three outcomes: divisible by both 3 and 7, divisible by 3 but not 7, and divisible by 7 but not 3. Specific conditions check these cases using modulus operations .

The program defines a leap year as one divisible evenly by 4. If a year divided by 4 yields no remainder, it prints 'Leap Year'. Otherwise, it outputs 'Not a Leap Year' .

The program classifies performance as 'Pass Class' for percentages between 35 and 44, 'Second Class' for scores from 45 to 59, and 'First Class' for 60 and above. Scores below 35 are not classified in the document .

The program uses the condition that if the sum of squares of the side and base equals the square of the hypotenuse, a right-angled triangle can be formed. This checks conformity with the Pythagorean theorem: side² + base² = hypotenuse² .

The program uses modular arithmetic, checking if a number modulo 2 equals zero. If true, the number is 'Even', otherwise, it is 'Odd' .

You might also like