Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Java Assignment -1

1:- Write a java program to find the greatest


among three numbers?

public class number {

public static void main(String[] args) {

int a=14, b=13, c=24;


if (a>b && a>c)
{
System.out.println ("a is greater ");
}
else if (b>a && b>c)
{
System.out.println ("b is greater ");
}
else if(c>a && c>b)
{
System.out.println ("c is greater ");
}

Output: -

c is greater
2. Write a java program to find the character is
vowel or not?

import java.util.Scanner;
public class javanew {

public static void main(String[] args) {


int i=1;
System.out.println("Enter character : ");
Scanner sc=new Scanner(System.in);
char chh = sc.next( ).charAt(0);

if(chh=='a' || chh=='e' || chh=='o' || chh=='u' || chh=='A' || chh=='E' ||


chh=='I' || chh=='O' || chh=='U') {
System.out.println("This character "+chh+" is vowel");
}else {
System.out.println("This character "+chh+" is consonant");
}
}

Output:-

Enter character :

This character o is vowel


3. write a java program
A candidate is requirement based on following
criteria:
If male, age should be above 30yrs and height
above 160cm.
If female, age should be above 25yrs and height
above 155cm.

import java.util.Scanner;
public class javanew {

public static void main(String[] args) {


Scanner sc= new Scanner(System.in);
System.out.println("Enter Catogery(M/F):");
char Category=sc.next().charAt(0);
System.out.println("Enter Age : ");
int Age = sc.nextInt();
System.out.println("Enter Height : ");
int Height = sc.nextInt();

if(Category=='M' && Age>=30 && Height>=160) {


System.out.println("Male is selected for this requirement ...!!!");
}
else if(Category=='F' && Age>=25 && Height>=155){
System.out.println("Female is selected for this requirement
...!!!");
}else {
System.out.println("You are not eligible for this
requirement...!!!");
}
}

Output:-
Enter catogery(M/F):
F
Enter Age :
29
Enter Height :
159
Female is selected for this requirment ...!!!
4. Write java program for following:
1. Print string n number of times

public class MCA {

public static void main(String[] args) {


int n=5;
for(int i=1;i<=n;i++) {
System.out.println("Hello Mca");
}
}

Output:-

Hello Mca

Hello Mca

Hello Mca

Hello Mca

Hello Mca

2. 1-100

public class MCA {

public static void main(String[] args) {


int number ;
for (number=1; number<=100; number++)
{
System.out.println(number);
}

}
Output:-
1
2
3
4
5
6
7
8
9
10
-
-
-
-
99
100

3. 100-1
public class MCA {

public static void main(String[] args) {

int number ;
for (number=100; number>=1; number--)
{
System.out.println (number);
}
}

}
Output:-

100
99
98
97
96
95
94
93
92
91
-
-
-
-
1

4. 1 to n number

public class MCA {

public static void main(String[] args) {


int number , n=5 ;
for (number=1; number<=n; number++)
{
System.out.println (number);
}

}
Output:-

1
2
3
4
5

5. Number between range

public class MCA {

public static void main(String[] args) {


int number ;
for (number=65; number<=70; number++)
{
System.out.println (number);
}
}

Output:-

65
66
67
68
69
70

6. Even/odd no s between range

public class MCA {

public static void main(String[] args) {


int number ;
for (number=45; number<=50; number++)
{
if(number%2==0)
{
System.out.println ("Even no="+number);
}
if(number%2!=0)
{
System.out.println ("Odd no="+number);
}
}
}

}
Output:-

Odd no=45
Even no=46
Odd no=47
Even no=48
Odd no=49
Even no=50

7. Sum of n numbers
public class MCA {

public static void main(String[] args) {


int number ,sum=0;
for (number=65; number<=70; number++)
{
sum=sum+number;
}
System.out.println (sum);
}
}
Output: - 405

8. Factorial of entered number

import java.util.Scanner;
public class javanew {

public static void main(String[] args) {


int fact=1;
Scanner sc= new Scanner(System.in);
System.out.println("Enter the number :");
int num =sc.nextInt();
for(int i=1;i<=num;i++) {
fact=fact*i;
}
System.out.println("Factorial of "+num+" is : "+fact);

Output:-

Enter the number :


7
Factorial of 7 is : 5040

9.Table of entered number

import java.util.Scanner;
public class javanew {

public static void main(String[] args) {


Scanner sc= new Scanner(System.In);
System.out.println("Enter the number : ");
int num =sc.nextInt();
System.out.println("Table of "+num+" is : ");
for(int i=1;i<=num;i++) {
System.out.println(num*i);

}
}
}

Output:-
Enter the number :
7
Table of 10 is :
7
14
21
28
35
42
49
56
63
70

You might also like