0% found this document useful (0 votes)
29 views5 pages

Week 1

This document contains code snippets for 4 Java programs: 1) A program to solve quadratic equations and check for real or complex solutions. 2) A program to print the Fibonacci sequence. 3) A program to calculate and print the sum of two numbers input by the user. 4) A program to check if a given number is even or odd. For each program, the aim, code, and sample output are provided.
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)
29 views5 pages

Week 1

This document contains code snippets for 4 Java programs: 1) A program to solve quadratic equations and check for real or complex solutions. 2) A program to print the Fibonacci sequence. 3) A program to calculate and print the sum of two numbers input by the user. 4) A program to check if a given number is even or odd. For each program, the aim, code, and sample output are provided.
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
You are on page 1/ 5

WEEK-1

1)Write a java program that prints all real solutions to the quadratic equations ax 2 +bx+c=0 read a,b,c and
use the quadratic formula if the discriminant is b 2-4ac is negative display a message stating that there are
no real solutions.

AIM:-To print all real solutions of the quadratic equations and to display that there are no real solutions.

CODE:-

import java.util.*;

class Quad

public static void main(String args[])

int a,b,c,k;

double x,y,l;

Scanner sc=new Scanner(System.in);

System.out.println("enter a,b and c values");

a=sc.nextInt();

b=sc.nextInt();

c=sc.nextInt();

k=b*b-4*a*c;

if(k<0)

System.out.println("there is no real number"+k);

else

l=Math.sqrt(k);

x=(-b-l)/2*a;

y=(-b+l)/2*a;

1
System.out.println("it is a real number"+x+" "+y);

OUTPUT:-

2)The fibinacci sequence is defined by the following rulews are 1st sequence are 1 and 1 and subsequent
values are sum of 2 values are preceding values.

AIM:-To print the fibonacci sequence starting with 1 and 1.

CODE:-

import java.util.*;

class Fib

public static void main(String args[])

int x=1,y=1,z,n,i;

Scanner sc=new Scanner(System.in);

System.out.println("enter n value");

n=sc.nextInt();

System.out.println(x+" "+y);

2
for(i=2;i<n;i++)

z=x+y;

System.out.println(" "+z);

x=y;

y=z;

OUTPUT:-

3)Write a java program to print sum of two numbers using scanner class.

AIM:-To print sum of two numbers.

CODE:-

import java.util.*;

class Sum

public static void main(String args[])

3
int x,y,z;

Scanner sc=new Scanner(System.in);

System.out.println("enter x value");

x=sc.nextInt();

System.out.println("enter y value");

y=sc.nextInt();

z=x+y;

System.out.println("sum of two number is"+z);

OUTPUT:-

4)Write a java program to implement given number is even or odd using scanner class.

AIM:-To print the given number is even or odd.

CODE:-

import java.util.*;

class Evenodd

public static void main(String args[])

4
int n;

Scanner sc=new Scanner(System.in);

System.out.println("enter n value");

n=sc.nextInt();

if(n%2==0)

System.out.println("even number is"+n);

else

System.out.println("odd number is"+n);

OUTPUT:-

You might also like