Com 121
Com 121
Onochie Emmanuel
Add three integers
// this program sums three integers and prints the result.
package javaapplication1;
import java.util.Scanner;
public class IO {
public static void main(String[] args) {
int num1;
int num2;
int num3;
int Sum;
Scanner input = new Scanner(System.in);
num1 = input.nextInt();
num2 = input.nextInt();
num3 = input.nextInt();
Sum = num1+num2+num3;
System.out.println(" The numbers are " + num1 + " ," + num2 + " , and" + num3);
System.out.println(" The Sum is " + Sum);
}
}
4/15/2014 2 Property of Projectcorp Nigeria
Three timestable
// this program produces the three times table
package javaapplication1;
import java.util.Scanner;
class MultiplicationTable
{
public static void main(String args[])
{
int c;
System.out.println("Multiplication table of 3 is :-");
for ( c = 1 ; c <= 10 ; c++ )
System.out.println("3 * "+c+" = "+(3*c));
}
}
4/15/2014 3 Property of Projectcorp Nigeria
Area of triangle
//This program calculates the area of a triangle
package javaapplication1;
import java.util.Scanner;
public class IO {
public static void main(String[] args) {
int h;
int b;
int h;
Scanner input = new Scanner(System.in);
h = input.nextInt();
b = input.nextInt();
a = 1/2 * b * h;
System.out.println(" The numbers are " + h + ", and" + b);
System.out.println(" The Area is " + a);
}
}
4/15/2014 4 Property of Projectcorp Nigeria
Convert a string to integer
//This java statement converts a string value to an
integer value to be stored in x
int x =Integer.parseInt("9")
4/15/2014 5 Property of Projectcorp Nigeria
Quadratic equation
The java program below solve quadratic equation
import java.util.Scanner;
public class Project4 {
public static void main(String[]args){
// Create a Scanner
Scanner input = new Scanner (System.in);
// Prompt the user to enter three double intergers.
System.out.print("Enter a:");
double a = input.nextDouble();
System.out.println("Enter b:");
double b = input.nextDouble ();
System.out.println("Enter c:");
double c = input.nextDouble();
4/15/2014 6 Property of Projectcorp Nigeria
Quadratic equation cntd.
double discriminat = Math.pow(b,2) - 4*a*c;
double x1 = (-b + Math.sqrt(discriminat))/(2*a);
double x2 = (-b - Math.sqrt(discriminat))/(2*a);
double i=Math.sqrt(-1);
double x3 = (-b + (Math.sqrt(discriminat))*i)/(2*a);
double x4 = (-b + (Math.sqrt(discriminat))*i)/(2*a);
if (discriminat > 0 ){
System.out.println("there are two solutions:" +x1+"and"+x2);
}
else if (discriminat == 0){
System.out.println("The solutions is:"+x1);
}
else if (discriminat < 0){
System.out.println("The solutions are"+x3+"and"+x4);
}
}
}
4/15/2014 7 Property of Projectcorp Nigeria
THE END
4/15/2014 Property of Projectcorp Nigeria 8