Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

Java

Java

Uploaded by

yugi s
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Java

Java

Uploaded by

yugi s
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

PRIME NUMBER

public class Main {

public static void main(String[] args) {

int num = 50; // Define the upper limit

int count; // Initialize counter for divisibility checks

// Iterate from 1 up to 'num' to identify prime numbers

for (int i = 1; i <= num; i++) {

count = 0; // Reset counter for each 'i'

// Check for divisibility from 2 up to i/2

for (int j = 2; j <= i / 2; j++) {

if (i % j == 0) {

count++; // Increment if 'i' is divisible by 'j'

break; // Exit loop if a divisor is found

// If the count is 0, 'i' is prime

if (count == 0) {

System.out.println(i); // Output the prime number

FIBINOCII SERIES

class Main {
public static void main(String[] args) {

int n = 10, firstTerm = 0, secondTerm = 1;


System.out.println("Fibonacci Series till " + n + " terms:");

for (int i = 1; i <= n; ++i) {


System.out.print(firstTerm + ", ");
// compute the next term
int nextTerm = firstTerm + secondTerm;
firstTerm = secondTerm;
secondTerm = nextTerm;
}
}
}
RATIONAL NUMBER
import java.io.*;
import java.util.*;
//to use predefined keywords and functions present in the libraries
class Rational
{//88/121 8/11
int n,d;
/* This constructor to get Numerator and Denominator value*/
Rational(int x,int y)
{
n=x;
d=y;
System.out.println("NUMERATOR="+n);
System.out.println("DENOMINATOR="+d);
System.out.println("BEFORE SIMPLIFICATION="+n+"/"+d);
}
/* This method finds the reduce form of Numerator and Denominator value*/

public void reducedform()


{
if(n<d)
{
for(int i=2;i<=n;i++)
{
while((n%i)==0 && (d%i)==0)
{
n=n/i;
d=d/i;
}
}
}
else
{
for(int i=2;i<=d;i++)
{
while((n%i)==0 && (d%i)==0)
{
n=n/i;
d=d/i;
}
}
}
}

void display()
{
System.out.println("Reduced form="+n+"/"+d);
}
public static void main(String args[])
{
Scanner S=new Scanner(System.in);
System.out.println("***********ENTER NUMERATOR VALUE*****************");
int A=S.nextInt();
System.out.println("***********ENTER DENOMINATOR VALUE*****************");
int B=S.nextInt();
Rational R=new Rational(A,B);
R.reducedform();
R.display();
}
}
INHERITANCE

class Animal {

// field and method of the parent class

String name;

public void eat() {

System.out.println("I can eat");

// inherit from Animal

class Dog extends Animal {

// new method in subclass

public void display() {

System.out.println("My name is " + name);

class Main {

public static void main(String[] args) {

// create an object of the subclass

Dog labrador = new Dog();

// access field of superclass

labrador.name = "Rohu";

labrador.display();

// call method of superclass


// using object of subclass

labrador.eat();

POLYMORPHISM

class Bike{

void run(){System.out.println("running");}

class Splendor extends Bike{

void run(){System.out.println("running safely with 60km");}

public static void main(String args[]){

Bike b = new Splendor();//upcasting

b.run();

REPLACING STRING

public class Main {

public static void main(String[] args){

String str = "abracadabra";

System.out.println(str);

// Replace substring in String

str = str.replace("ab", "AB");

System.out.println(str);

}
AMSTRONG NUMBER

public class Armstrong {

public static void main(String[] args) {

// TODO Auto-generated method stub

int a = 153;

int n = a;

int rem = 0;

int sum = 0;

while(a>0) {

rem = a%10;

a = a/10;

sum = sum+rem*rem*rem;

if(sum == n)

System.out.println("given number is armstrong number"+ sum);

else

System.out.println("given number is not armstrong number");

You might also like