0% found this document useful (0 votes)
13 views

Programs

Uploaded by

anupygzp0805
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Programs

Uploaded by

anupygzp0805
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Fibonacci series Program

import java.util.Scanner;
class Fibonacci
{
public void series(int n)
{
int a=0,b=1,sum,i;
for(i=1;i<=n;i++)
{
System.out.println(a +" ");
sum=a+b;
a=b;
b=sum;
}
}
}
• class UseFibonacci
• {
• public static void main(String [] args)
• {
• Scanner kb=new Scanner(System.in);
• System.out.println("Enter the Nth term of Fibonacci Series:");
• int n=kb.nextInt();
• Fibonacci obj=new Fibonacci();
• obj.series(n);
• }
• }
Perfect Number Program
• import java.util.Scanner;
• class PerfectNumber
• {
• public static void main(String[] args)
• {
• int n, sum = 0;
• Scanner s = new Scanner(System.in);
• System.out.println("Enter the number you want to
check:");
• n = s.nextInt();
• for(int i = 1; i < n; i++)
• {
• if(n % i == 0)
• {
• sum = sum + i;
• }
• }
• if(sum == n)
• {
• System.out.println("Given number is Perfect");
• }
• else
• {
• System.out.println("Given number is
not Perfect");
• }
• }

• }
Factorial Program
• import java.util.Scanner;

• class Fact
• {
• public static void main(String [] args)
• {
• Scanner kb=new Scanner(System.in);
• int n,fact=1;
• System.out.println("Enter a number");
• n=kb.nextInt();
• while(n>1)
• {
• fact=fact*n;
• n--;
• }
• System.out.println("factorial is "+fact);
• }
• }
Palindrome Program
• import java.util.Scanner;
• class Palindrome
• {
• public static void main(String [] args)
• {
• Scanner kb=new Scanner(System.in);
• int rem,sum=0,temp;
• System.out.println("Enter the number");
• int n=kb.nextInt();//It is the number variable to be
checked for palindrome

• temp=n;
• while(n>0)
• {
• rem=n%10; //getting remainder
• sum=(sum*10)+rem;
• n=n/10;
• }
• if(temp==sum)
• System.out.println("palindrome number ");
• else
• System.out.println("not palindrome");
• }
• }
Reverse String Program
• import java.util.Scanner;
• class ReverseString
• {
• public static void main(String[] args)
• {
• System.out.println("Enter string to reverse:");

• Scanner kb = new Scanner(System.in);
• String str = kb.nextLine();
• String reverse = "";
• for(int i = str.length() - 1; i >= 0; i--)
• {
• reverse = reverse + str.charAt(i);
• }

• System.out.println("Reversed string is:");
• System.out.println(reverse);
• }
• }
GCD Program
• import java.util.Scanner;
• class GCD
• {
• public int gcd(int a,int b)
• {
• if (b==0)
• {
• return a;
• }
• else
• {
• return gcd(b,a%b);
• }
• }
• }
• class UseGCD
• {
• public static void main(String[] args)
• {
• Scanner kb=new Scanner(System.in);
• System.out.println("Enter 2 numbers:");
• int a=kb.nextInt();
• int b=kb.nextInt();
• GCD obj=new GCD();
• int g= obj.gcd(a,b);
• System.out.println("GCD is :"+g);
• }
• }

Prime Number Program
• class Prime
• {
• public int IsPrime(int n)
• {

• if(n==1||n==0)
• {
• return 1;
• }
• for (int i=2;i<n;i++)
• {
• if(n%i==0)
• {
• return 1;
• }
• }
• return 0;
• }
• }
• class UsePrime
• {
• public static void main(String [] args)
• {
• Scanner kb=new Scanner(System.in);
• System.out.println("Enter a number to check
whether it is prime or not?");
• int n=kb.nextInt();
• Prime obj=new Prime();
• int g=obj.IsPrime(n);
• if (g==0)
• {
• System.out.println("Prime number");
• }
• else
• {
• System.out.println("Non Prime Number");
• }
• }
• }
Sort Array Program
• import java.util.Scanner;
• class SortedArray
• {
• public static void main(String [] args)
• {
• Scanner kb=new Scanner(System.in);
• System.out.println("Enter the size of array");
• int n=kb.nextInt();
• int c;
• int [] arr=new int[n];
• System.out.println("Enter "+n+ " Numbers");
• for (int i=0;i<arr.length;i++)
• {
• arr[i]=kb.nextInt();
• }
• for (int i=0;i<arr.length;i++)
• {
• for(int j=i+1;j<arr.length;j++)
• {
• if (arr[i]>arr[j])
• {
• c=arr[i];
• arr[i]=arr[j];
• arr[j]=c;
• }
• }
• }
• System.out.println("Sorted array is ");
• for (int i=0;i<arr.length;i++)
• {
• System.out.println(arr[i]);
• }
• }
• }

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy