Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
1.Write a java program that prints all real solutions of the quadratic equation ax2+bx+c=0.
import java.util.*;
class quadratic
{
public static void main(String args[])
{
int a,b,c;
double r1,r2;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the values a,b and c");
a=sc.nextInt();
b=sc.nextInt();
c=sc.nextInt();
int ra=(b*b)-(4*a*c);
if(ra==0)
{
System.out.println("The roots are real and equal");
r1=-b/(2*a);
r2=r1;
}
else if(ra>0)
{
System.out.println("The roots are real and distinct");
r1=(-b+Math.sqrt(ra))/(2*a);
r2=(-b-Math.sqrt(ra))/(2*a);
System.out.println("The roots are:"+r1+" "+r2);
}
else
{
System.out.println("The roots are Imaginary");
}
}
}
2. Write a java program to find out the fibonacci series upto n numbers.
import java.util.*;
import java.io.*;
class fibonacci
{
public static void main(String args[])throws IOException
{
int f1=1,f2=1,f3,i=3;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the value of n");
int n=sc.nextInt();
System.out.println(f1);
System.out.println(f2);
while(i<=n)
{
f3=f1+f2;
System.out.println(f3);
f1=f2;
f2=f3;
i++;
}
}
}
3.Write a java program to print out all prime numbers up to that integer.
import java.util.*;
import java.io.*;
class primeuptoN
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the value of n");
int n=Integer.parseInt(br.readLine());
System.out.println("Prime numbers upto "+n+" are:");
boolean flag=true;
for(int i=1;i<n;i++)
{
for(int j=2;j<=i/2;j++)
{
flag=true;
if((i%j)==0)
{
flag=false;
break;
}
}
if(flag==true)
//System.out.println("Prime numbers upto n are:"+i);
System.out.println(i);
}
}
}
4.Write a program to find whether the given string is palindrome or not using StringBuffer Class.
//Palindrome
import java.util.*;
import java.io.*;
import java.lang.*;
class palindrome
{
public static void main(String args[])
{
Scanner sr=new Scanner(System.in);
System.out.println("Enter a String");
String str=sr.nextLine();
String temp=str;
StringBuffer sb=new StringBuffer(str);
sb.reverse();
if(temp.equals(sb.toString()))
System.out.println("String is Palindrome");
else
System.out.println("String is not Palindrome");
}
}
5. Print 'N' numbers using while statement.
import java.io.*;
class whilestmt
{
public static void main(String args[])
{
int n=0;
while(n<10)
{
System.out.println("n="+n);
n++;
}
}
}
6. Write a java program to implement grade of marks using if-else-if statement.
import java.util.*;
class ifelseifstatement
{
public static void main(String args[])
{
Scanner sr=new Scanner(System.in);
int m1,m2,m3,m4,m5,m6,total;
System.out.println("Enter 6 subjects marks");
m1=sr.nextInt();
m2=sr.nextInt();
m3=sr.nextInt();
m4=sr.nextInt();
m5=sr.nextInt();
m6=sr.nextInt();
total=(m1+m2+m3+m4+m5+m6)/6;
if(total>50)
{
System.out.println("grade is A");
}
else if(total>=40)
{
System.out.println("grade is B");
}
else if(total>=30)
{
System.out.println(" grde is c");
}
else
{
System.out.println(" grade is D");
}
}
}
7.Find the Factorial of a given number.
import java.util.*;
class Recursion
{
int fact(int i)
{
int result;
if(i==1)
return 1;
result=i*fact(i-1);
return result;
}
}
class Rec
{
public static void main(String args[])
{
Recursion f=new Recursion();
Scanner sr=new Scanner(System.in);
System.out.println("Enter a number to find the factorial");
int n=sr.nextInt();
System.out.println("factorial of "+n+" is:"+f.fact(n));
}
}
8.Write a java program using Switch Statement.
import java.io.*;
class switchstmt
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int i;
System.out.println("enter the number between 1 to 3");
i=Integer.parseInt(br.readLine());
switch(i)
{
case 1:
System.out.println("one");
break;
case 2:
System.out.println("two");
break;
case 3:
System.out.println("three");
break;
default:
System.out.println("enter correct number");
}
}
}
9. Invoking Super Class Constructor using super key.
class A
{
double width,height,depth;
A(double w,double h,double d)
{
width=w;
height=h;
depth=d;
}
double volume()
{
return width*height*depth;
}
}
class B extends A
{
double weight;
B(double w,double h,double d,double m)
{
super (w,h,d);
weight=m;
}
}
class SuperDemo
{
public static void main(String args[])
{
B b=new B(20.5,10.2,15.1,50.6);
System.out.println("volume is:"+b.volume());
}
}
10. Invoking Super Class method using super key.
class A
{
int x;
void show()
{
System.out.println("This is super class method");
}
}
class B extends A
{
int x;
B(int a,int b)
{
super.x=a;
x=b;
}
void show()
{
super.show();
System.out.println("This is sub class method");
System.out.println("x in superclass: "+ super.x);
System.out.println("x in subclass: "+x);
}
}
class superMethod
{
public static void main(String args[])
{
B b=new B(10,20);
b.show();
}
}
11. write a program using StringTokenizer class.
import java.io.*;
import java.lang.*;
import java.util.*;
class Strtokenizer
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a String");
String str=br.readLine();
StringTokenizer t=new StringTokenizer(str);
System.out.println(t.countTokens());
while(t.hasMoreTokens())
{
System.out.println(t.nextToken());
}
}
}
12.Write a Simple Java Program using 2-Dimensional Array
class Array
{
public static void main(String args[])
{
int number[][]=new int[4][5];
int i,j,k=0;
for(i=0;i<4;i++)
{
for(j=0;j<5;j++)
{
number[i][j]=k;
k++;
}
}
for(i=0;i<4;i++)
{
for(j=0;j<5;j++)
{
System.out.println(number [i][j]+" ");
}
}
}
}
13.Write a Simple Java Program using all Primitive data types.
class Pdatatypes
{
public static void main(String args[])
{
byte marks=35;
short length=23;
int age=25;
long width=35;
float price=34.5f;
double cost=34.4562;
System.out.println("marks="+marks+"length="+length);
System.out.println("Age="+age+"width="+width);
System.out.println("price="+price+"cost="+cost);
Boolean rate=false;
if(rate==true)
{
System.out.println("mango rate is high");
}
else
{
System.out.println("mango rate is low");
}
}
}
14.Write a Simple Java Program using instance class local variable.
class Scope
{
int qty=40;
static double rate=50.5;
void compute()
{
double comm=3.5;
System.out.println("pay Rs"+qty*rate*comm/100);
}
public static void main(String args[])
{
int qty=50;
Scope s1=new Scope();
double total=s1.qty*rate;
System.out.println("Total pay"+total);
s1.compute();
}
}
15.Write a Java Program to define lifetime of a variable.
class Life
{
public static void main(String args[])
{
int i;
for(i=0;i<3;i++)
{
int j=-1;
System.out.println("j is "+j);
j=100;
System.oput.println("now j is"+j);
}
}
}
16.Write a Java Program to Assign values to one-dimensional array.
class Assign
{
public static void main(String args[])
{
int i;
Double rate[]=new Double[3];
rate[0]=20.5;
rate[1]=30.75;
rate[2]=45.25;
System.out.println("size of arry is"+rate.length);
System.out.println("second element in array in array is "+rate[1]);
System.out.println("element assigned to array are");
for(i=0;i<rate.length;i++)
{
System.out.println(rate[i]+",");
}
}
}
17.Write a simple Java program passing string arguments using command line argument.
class commandline
{
public static void main(String args[])
{
System.out.println("Number of elements passed are"args.length);
System.out.println("The element passed are");
for(i=0;i<ars.length;i++)
{
System.out.println(args[i]);
}
}
}
18.Write a Java program to find out largest number among 3 numbers using Nested if else.
class nested if else statement
{
public static void main(String args[])
{
int a=10,b=20,c=9;
if(a>b)
{
if(a>c)
{
System.out.println("a largest number");
}
else
{
System.out.println("c is largest number");
}
}
else
{
if(b>c)
{
System.out.println("b is largest number");
}
else
{
System.out.println("c is largest number");
}
}
}
}
19.Write a Java Program using while statement.
import java.io.*;
class whilestmt
{
public static void main(String args[])
{
int n=0;
while(n<10)
{
System.out.println("n="+n);
n++;
}
}
}
20.Write a Java Program using do-while statement.
class Test
{
public static void main(String args[])
{
int n=0;
do
{
System.out.println("n="+n);
n++;
}
while(n<10);
}
}
21.Write a Java Program using for loop statement.
class forstatement
{
public static void main(String args[])
{
int i=1, factorial=1;
int fact(n){
if(n==1)
return 1;
factorial=fact(n-1)*n;
return factorial;
}
int n=5;
for(i=1;i<=n;i++)
{
factorial=factorial*n;
}
System.out.println("factorial is"+ factorial);
}
}