1. Write a program that prompt the user to enter an integer number then determine if the number is odd or even .
Answer:-
import java.io.*;
import java.util.*;
public class test{
public static void main (String[] args)
{
Scanner input=new Scanner(System.in);
int x;
System.out.print("enter x");
x=input.nextInt();
if( (x%2==0)
System.out.print("even number");
else
System.out.print("odd number");
}
}
2. Write a program that reads the first and last name of student and prints the initials in capital letters.
Example:
Enter first and last name of student: Nora Ali
The initials are = N A
Answer:-
import java.io.*;
import java.util.*;
public class test{
public static void main (String[] args)
{
Scanner input=new Scanner(System.in);
String firstname,lastname;
System.out.print("enter first name and last name :");
firstname=input.next();
lastname=input.next();
System.out.print("The initials are = ");
System.out.print(firstname.substring(0,1).toUpperCase());
System.out.print(" "+lastname.substring(0,1).toUpperCase());
}
}
--------------------------------------
3. Write a program that read three strings from the user, and print out the three strings ordered by length, shortest first. If two strings are of equal length, their order doesn't matter.
Example:
Enter three Strings:
String1 = Ahmad
String2 = Ali
String3 = Hind
The order of the Strings is: Ali, Hind, Ahmad
Answer:-
import java.io.*;
import java.util.*;
public class test{
public static void main (String[] args)
{
Scanner input=new Scanner(System.in);
String string1,string2,string3;
System.out.println("enter three strings :");
System.out.print("string 1:");
string1=input.next();
System.out.print("string 2:");
string2=input.next();
System.out.print("string 3:");
string3=input.next();
int l1,l2,l3;=string1.length();
l2=string2.length();
l3=string3.length();
System.out.print("The order of the Strings is:");
if(l1<l2 && l1<l3)
{
System.out.print(string1+" ");
if(l2<l3)
System.out.print(string2 + " "+string3);
else
System.out.print(string3 + " "+string2);
}
if(l2<l1 && l2<l3)
{
System.out.print(string2+" ");
if(l1<l3)
System.out.print(string1 + " "+string3);
else
System.out.print(string3 + " "+string1);
}
if(l3<l1 && l3<l2)
{
System.out.print(string3+" ");
if(l1<l2)
System.out.print(string1 + " "+string2);
else
System.out.print(string2 + " "+string1);
}
}
}
-------------------------------------
4. Write an application that reads in two integers and determines if one of them is a division of the other. If not, print a suitable message.
Example:
Enter two integer numbers:
Number1 = 8
Number2 = 4
4 is a divider of 8
Answer:-
import java.io.*;
import java.util.*;
public class oddeven{
public static void main (String[] args)
{
Scanner input=new Scanner(System.in);
int num1,num2;
System.out.println("enter two numbers :");
System.out.print("number 1:");
num1=input.nextInt();
System.out.print("number 2:");
num2=input.nextInt();
if(num1%num2==0)
System.out.println(num2+ " is a divider of "+num1);
}
}
--------------------------------------------
5. Write a program that reads three integers, then prints them in an ascending
order.
Example:
Enter three integer numbers:
Number1 = 5
Number2 = 10
Number3 = 3
The order of the numbers is: 3 , 5 , 10
نفس السؤال الثالث
---------------------------------------
6. Write a program that prompts the user to enter the lengths of three sides of a triangle and then outputs a message indicating whether the triangle is a right triangle or not.
Answer:-
import java.io.*;
import java.util.*;
public class test{
public static void main (String[] args)
{
Scanner input=new Scanner(System.in);
int a,b,c;
System.out.print("enter a");
a=input.nextInt();
System.out.print("enter b");
b=input.nextInt();
System.out.print("enter c");
c=input.nextInt();
if( (c*c)==(a*a+b*b))
System.out.print("right triangle");
else
System.out.print("not right triangle");
}
}