• Home
  • Courses
  • School
  • Programs
  • Problems
  • Contact Us
  • My account
  • Register

Have any question?

(+91) 98222 16647
info@simplycoding.in
RegisterLogin
Simply Coding
  • Home
  • Courses
  • School
  • Programs
  • Problems
  • Contact Us
  • My account
  • Register

Java

For and While programs

  • Categories Java, Iterative Statements, For & While

For Loop :

1 . Write a program to display FACTORIAL of a number

import java.util.*;
class FactorialOfNum{
public static void main(String args[ ])
{
Scanner sc =new Scanner(System.in);
int n,i,f=1;
System.out.println("enter a number");
n= sc.nextInt( );
for(i=1;i<=n;i++)
{
f=f*i;
}
System.out.println("THE FACTORIAL OF THE NUMBER IS : "+f);
}
}

2 . Write a program to display first 15 Fibonacci numbers 

public class febonacciNum
{
public static void main(String args[])
{
int a=0,b=1,c,i;
System.out.println("First 15 Fibonacci numbers are :");
System.out.print(a+" "+b+" ");
for(i=3;i<=15;i++)
{
c=a+b;
System.out.print(c+" ");
a=b;
b=c;
}
}
}

3 . Write a program to print all the factors of a number.

import java.util.Scanner;
public class FactorsOfNumber
{
    public static void main(String args[])
      {
        int n ;
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the value of n: ");
        n = sc.nextInt();
        System.out.println ("The all Factors of given no. "+n+" is");
        for(int i = 1;i<= n;i++)
        {
            if(n%i == 0)
                System.out.println (i);
        }
      }
}

4 . Write a program to display the Mathematical Table from the given number:

import java.util.Scanner;
public class MathTables
{
   public static void main(String[] args)
   {
       Scanner sc=new Scanner(System.in);
       System.out.println("Enter number ");
       int num = sc.nextInt(); 
       System.out.println("Table of " + num);
       for (int i = 1; i<= 10; i++) {
          System.out.println(num*i);
       }
   }
}

5 . Write a program to calculate and display the factorials of all the numbers between ‘m’ and ‘n’ (where m<n, m>0, n>0).

import java.util.Scanner;
public class FactorialOfMToN
{
   public static void main(String args[]) 
   {
      Scanner in = new Scanner(System.in);
      System.out.print("Enter m: ");
      int m = in.nextInt();
      System.out.print("Enter n: ");
      int n = in.nextInt();
      if (m < n && m > 0 && n > 0) {
        for (int i = m; i <= n; i++) {
          long fact = 1;
          for (int j = 1; j <= i; j++)
             fact *= j;
          System.out.println("Factorial of " + i + " = " + fact);
        }
      }
      else {
         System.out.println("Invalid Input");
      }
   }
}

6 . Write a program to display HCF and LCM of two numbers

import java.util.*;
public class LCMandHCF
{
   public static void main(String args[])
   {
      Scanner sc =new Scanner(System.in);
      int m,n,i,min,hcf=0;
      System.out.println("enter two numbers");
      m= sc.nextInt( );
      n= sc.nextInt( );
      min=Math.min(m,n);
      for(i=1;i<min;i++)
      {
         if(m%i==0 && n%i==0)
         hcf=i;
      }
      System.out.println("HCF of "+m+ " and "+n+" is : "+hcf);
      System.out.println("LCM of "+m+ " and "+n+" is : "+ ((m*n)/hcf));
   }
}

7 . Write a program to generate all 3 digit palindrome numbers.

public class ThreeDigitPrime
{
static void main()
{
int i,j,r,d;
for(i=100;i<=999;i++)
{
r=0;
for(j=i;j>0;j=j/10)
{
d=j%10;
r=r*10+d;
}
if(r==i)
System.out.println(i);
}
}
}

8 . Write a program to check and display whether two numbers are amicable pair or not

Amicable numbers are two different numbers so related that the sum of the proper divisors of each is equal to the other number.

The first ten amicable pairs are: (220, 284), (1184, 1210), (2620, 2924), (5020, 5564), (6232, 6368), (10744, 10856), (12285, 14595), (17296, 18416), (63020, 76084), and (66928, 66992).

import java.util.*;
public class amicablePair
{
public static void main(String args[])
{
Scanner sc =new Scanner(System.in);
int m,n,sfm=0,sfn=0,i;
System.out.println("enter two number");
m=sc.nextInt( );
n=sc.nextInt( );
for(i=1;i<=m;i++)
{
if(m%i==0)
sfm+=i;
}
for(i=1;i<=n;i++)
{
if(n%i==0)
sfn+=i;
}
if(sfm==sfn)
System.out.println("Amicable Pair");
else
System.out.println("Not Amicable Pair");
}
}

While loop : 

1 . Write a program to accept a number then print the sum of digits and number of digits present in it. (E.g. If the input number is 225, the sum of digits is 9 and number of digits is 3)

public class sumOfDigits
{
public static void main(int n)
{
int sum = 0,count = 0,div;
while(n>0)
{
div = n%10;
sum = sum+div;
count = count+1;
n = n/10;
}
System.out.println ("Sum of digits : "+sum);
System.out.println ("no. of digits : "+count);
}
}

2 . Write a program to finding GCD of two numbers using while loop

import java.util.Scanner;
public class GCD_2_num
{
    public static void main(String[] args)
    {
        int num1 , num2 ;
        System.out.print("Enter any two number:");
        Scanner sc = new Scanner(System.in);
        num1 = sc.nextInt();
        num2 = sc.nextInt();
        while (num1 != num2) {
            if(num1 > num2)
                num1 = num1 - num2;
            else
                num2 = num2 - num1;
        }
        System.out.printf("GCD of given numbers is: "+num2);
    }
}

3 . Write a program to accept a number then print the number in reverse order. (E.g. If the input number is 368, the output will be 863)

public class reverseNum
{
public static void main(int num)
{
int reverseNumber = 0, digit;
while(num>0)
{
digit = num%10;
reverseNumber = reverseNumber*10+digit;
num = num/10;
}
System.out.println ("Reverse Number is : " +reverseNumber);
}
}

4 . Write a program to find ASCII value of a Character

import java.util.*;
public class DecimalToBinary
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        // Create Stack object
        Stack stack = new Stack();
        System.out.println("Enter decimal number: ");
        int num = in.nextInt();
        while (num != 0)
        {
            int d = num % 2;
            stack.push(d);
            num /= 2;
        }
        System.out.print("\nBinary representation is:");
        while (!(stack.isEmpty() ))
        {
            System.out.print(stack.pop());
        }
        System.out.println();
    }
}

 5 . Write a java program to calculate SquareRoot of given number.

import java.util.Scanner;
public class SquareRoot
{
    public static double squareRoot(int number) {
      double temp;
      double sr = number / 2;
      do {
            temp = sr;
            sr = (temp + (number / temp)) / 2;
      } while ((temp - sr) != 0);
      return sr;
    }
    public static void main(String[] args) 
    {
      System.out.print("Enter any number:");
      Scanner scanner = new Scanner(System.in);
      int num = scanner.nextInt();
      scanner.close();
      System.out.println("Square root of "+ num+ " is: "+squareRoot(num));
    }
}
  • Share:
author avatar
Simply Coding

Previous post

Loops in Java
June 24, 2021

Next post

2D array in Java
June 29, 2021

You may also like

Single Linked List Programs in Java
Single Linked List Programs in Java
28 August, 2021
Implementing Stack using Array in Java
Implementing Stack using Array in Java
28 August, 2021
Constructor Programs
Constructor Programs
3 July, 2021

Leave A Reply Cancel reply

You must be logged in to post a comment.

Categories

  • Uncategorized
  • Programs
    • Python
    • Java
  • Problems
    • Python
    • Java
    • Web Development
      • Internet
    • Emerging Technologies
  • Notes
    • General
    • QBasic
    • MS Access
    • Web Development
      • XML
      • HTML
      • JavaScript
      • Internet
    • Database
    • Logo Programming
    • Scratch
    • Emerging Trends
      • Artificial Intelligence
      • Internet of Things
      • Cloud Computing
      • Machine Learning
    • Computer Fundamentals
      • Computer Networks
      • E-Services
      • Computer Hardware
    • Python
    • Java
  • School
    • ICSE
      • Computers Class 9
        • Java Introduction
        • Tokens & Data Types
        • Java Operators
        • Math Library
        • if & switch
        • For & While
        • Nested loops
      • Computer Class 10
        • Sample Papers
        • OOPS concepts
        • Functions in Java
        • Constructors
        • Arrays in Java
        • Strings in Java
    • SSC
      • IT Class 11
        • IT Basics
        • DBMS
        • Web Designing
        • Cyber Laws
      • IT Class 12
        • Web Designing
        • SEO
        • Advanced JavaScript
        • Emerging Tech
        • Server Side Scripting
        • E-Com & E-Gov
      • Computer Science 11
      • Computer Science 12
    • CBSE
      • Computer 9
        • Basics of IT
        • Cyber Safety
        • Scratch
        • Python
      • Computer 10
        • Sample Papers
        • Networking
        • HTML
        • Cyber Ethics
        • Scratch
        • Python
      • Computer Science 11
        • Computer Systems
        • Python 11
          • Python Basics
          • Python Tokens
          • Python Operators
          • Python if-else
          • Python loops
          • Python Strings
          • Python List
          • Python Tuple
          • Python Dictionary
          • Python Modules
        • Data Management
      • Computer Science 12
        • Sample Papers
        • Python 12
          • Python Functions
          • Python File Handling
          • Python Libraries
          • Python Recursion
          • Data Structures
        • Computer Networks
        • Data Management
    • ISC
      • Computer Science 11
        • Introduction to Java
        • Values & Data Types
        • Operators
        • if & switch
        • Iterative Statements
        • Functions
        • Arrays
        • String
        • Data Structures
        • Cyber Ethics
      • Computer Science 12
        • Sample Papers
        • Boolean Algebra
        • OOPS
        • Wrapper Classes
        • Functions
        • Arrays
        • String

Categories

  • Uncategorized
  • Programs
    • Python
    • Java
  • Problems
    • Python
    • Java
    • Web Development
      • Internet
    • Emerging Technologies
  • Notes
    • General
    • QBasic
    • MS Access
    • Web Development
      • XML
      • HTML
      • JavaScript
      • Internet
    • Database
    • Logo Programming
    • Scratch
    • Emerging Trends
      • Artificial Intelligence
      • Internet of Things
      • Cloud Computing
      • Machine Learning
    • Computer Fundamentals
      • Computer Networks
      • E-Services
      • Computer Hardware
    • Python
    • Java
  • School
    • ICSE
      • Computers Class 9
        • Java Introduction
        • Tokens & Data Types
        • Java Operators
        • Math Library
        • if & switch
        • For & While
        • Nested loops
      • Computer Class 10
        • Sample Papers
        • OOPS concepts
        • Functions in Java
        • Constructors
        • Arrays in Java
        • Strings in Java
    • SSC
      • IT Class 11
        • IT Basics
        • DBMS
        • Web Designing
        • Cyber Laws
      • IT Class 12
        • Web Designing
        • SEO
        • Advanced JavaScript
        • Emerging Tech
        • Server Side Scripting
        • E-Com & E-Gov
      • Computer Science 11
      • Computer Science 12
    • CBSE
      • Computer 9
        • Basics of IT
        • Cyber Safety
        • Scratch
        • Python
      • Computer 10
        • Sample Papers
        • Networking
        • HTML
        • Cyber Ethics
        • Scratch
        • Python
      • Computer Science 11
        • Computer Systems
        • Python 11
          • Python Basics
          • Python Tokens
          • Python Operators
          • Python if-else
          • Python loops
          • Python Strings
          • Python List
          • Python Tuple
          • Python Dictionary
          • Python Modules
        • Data Management
      • Computer Science 12
        • Sample Papers
        • Python 12
          • Python Functions
          • Python File Handling
          • Python Libraries
          • Python Recursion
          • Data Structures
        • Computer Networks
        • Data Management
    • ISC
      • Computer Science 11
        • Introduction to Java
        • Values & Data Types
        • Operators
        • if & switch
        • Iterative Statements
        • Functions
        • Arrays
        • String
        • Data Structures
        • Cyber Ethics
      • Computer Science 12
        • Sample Papers
        • Boolean Algebra
        • OOPS
        • Wrapper Classes
        • Functions
        • Arrays
        • String
Simply Coding Computer Courses for School                Privacy Policy     Terms of Use     Contact Us

© 2021 Simply Coding

Login with your site account

Lost your password?

Not a member yet? Register now

Register a new account

Are you a member? Login now