• LCM(Least common multiple):

    Least Common Multiple(LCM) is a method to find the smallest common multiple between any two or more numbers. A common multiple is a number which is a multiple of two or more numbers.

    Program:

    class NtoB{
    public static void main(String args[]){
    int no = 12;
    int div=2;
    String binary=””;
    int remainder =0;
    while(no>0) {
    remainder =no%2;
    binary = remainder+binary;
    no=no/2;

        }
        System.out.println("the binary word is "+binary);
    
    }
    

    }

    output:

    sobhika@sobhika-HP-Laptop-15-db1xxx:~$ javac LCM.java
    sobhika@sobhika-HP-Laptop-15-db1xxx:~$ java LCM
    the lcm is 30

    Binary to Decimal:

    Binary to decimal conversion is done to convert a number in a binary number system (base-2) to a number in a decimal number system (base-10). It is very necessary to understand the binary to decimal conversion for computer programming applications.

    Program:

    class BtoN{
    public static void main(String args[]){
    int no=1111;
    int pow=0;
    double result=0;
    while(no>0) {
    int last = no%10;
    result=result+last*Math.pow(2,pow);
    no=no/10;
    pow++;
    }
    System.out.println(result);

    }
    

    }

    output:

    sobhika@sobhika-HP-Laptop-15-db1xxx:~$ javac BtoN.java
    sobhika@sobhika-HP-Laptop-15-db1xxx:~$ java BtoN
    15.0

    Decimal to Binary:

    In decimal to binary conversion, we convert a base 10 number to a base 2 number by using simple methods. For example, if 1210 is a decimal number then its equivalent binary number is 11002

    Program:

    class NtoB{
    public static void main(String args[]){
    int no = 12;
    int div=2;
    String binary=””;
    int remainder =0;
    while(no>0) {
    remainder =no%2;
    binary = remainder+binary;
    no=no/2;

        }
        System.out.println("the binary word is "+binary);
    
    }
    

    }

    output:

    sobhika@sobhika-HP-Laptop-15-db1xxx:~$ javac NtoB.java
    sobhika@sobhika-HP-Laptop-15-db1xxx:~$ java NtoB
    the binary word is 1100

  • Todays class:

    1. fibonacci series:
    2. A Fibonacci Sequence is a series of whole numbers such that every third number is equal to the sum of the previous two numbers.
    3. The number of petals in a flower follows the Fibonacci sequence. The Fibonacci sequence can also be applied to finance by using four techniques including retracements, arcs, fans, and time zones.

    Program:

    class FIBONACCI{
    public static void main(String args[]){
    int n=10;
    int f=0;
    int s=1;
    int t=0;
    System.out.println(f+” “+s);
    n=n-2;
    while(n>0){
    t=f+s;
    System.out.println(t);
    f=s;
    s=t;

    n–;
    }
    }
    }

    output:

    sobhika@sobhika-HP-Laptop-15-db1xxx:~$ javac FIBONACCI.java
    sobhika@sobhika-HP-Laptop-15-db1xxx:~$ java FIBONACCI
    0 1
    1
    2
    3
    5
    8
    13
    21
    34

    SWAP :

    Swapping two variables refers to mutually exchanging the values of the variables.

    n1, n2 is the swapping of two variables.

    class Swap{
    public static void main(String args[]){
    int n1=10;
    int n2=20;
    System.out.println(n1+” “+n2);
    n1=n1+n2;
    n2=n1-n2;
    n1=n1-n2;
    System.out.println(n1+” “+n2);
    }
    }

    output:

    sobhika@sobhika-HP-Laptop-15-db1xxx:~$ javac Swap.java
    sobhika@sobhika-HP-Laptop-15-db1xxx:~$ java Swap
    10 20
    20 10

    swapping of three number:

    n1, n2, res

    class Swap1{
    public static void main(String args[]){
    int n1=10;
    int n2=20;
    int res=0;
    res=n2;
    n2=n1;
    n1=res;
    System.out.println(n1+” “+n2);
    }
    }

    output:

    sobhika@sobhika-HP-Laptop-15-db1xxx:~$ javac Swap1.java
    sobhika@sobhika-HP-Laptop-15-db1xxx:~$ java Swap1
    20 10

    Odd or Even:

    class WORD{
    public static void main(String args[]){
    String word=”sofiya”;
    int i=0;
    while(i<=word.length()-1){
    if(i%2==0){
    System.out.println(“even “+word.charAt(i));
    }
    else{
    System.out.println(“odd “+word.charAt(i));
    }
    i++;
    }

    }
    }

    output:

    sobhika@sobhika-HP-Laptop-15-db1xxx:~$ javac WORD.java
    sobhika@sobhika-HP-Laptop-15-db1xxx:~$ java WORD
    even s
    odd o
    even f
    odd i
    even y
    odd a

  • Todays Program:

    Strong Number:

    Strong number is a special number whose sum of the factorial of digits is equal to the original number.

    for example:

    145=1! + 4! + 5!

    =1+4*3*2*1+5*4*3*2*1

    =1+24+120

    145=145, hence proved.

    Here, we done it in the mathematical form . now, we are gonna do it in the program.

    Program:

    class Strong{
    public static void main(String args[]){
    int n=145;
    int copy=n;
    int pop=0;
    int sum=0;
    int fac;
    while(n>0){
    fac=1;
    pop=n%10;
    while(pop>0){
    fac=fac*pop;
    pop–;
    }
    sum=sum+fac;
    n=n/10;
    }
    if(copy==sum){
    System.out.println(“strong”);
    }
    else{
    System.out.println(“not strong”);
    }
    }
    }

    output:

    sobhika@sobhika-HP-Laptop-15-db1xxx:~$ javac Strong.java
    sobhika@sobhika-HP-Laptop-15-db1xxx:~$ java Strong
    strong
    sobhika@sobhika-HP-Laptop-15-db1xxx:~$

    GCD (GCD (Greatest Common Divisor)

    GCD (Greatest Common Divisor) of two given numbers A and B is the highest number that can divide both A and B completely, i.e., leaving remainder 0 in each case.

    example:

    Program:

    class GCD{
    public static void main(String args[]){
    int n=8;
    int m=12;
    int min=n>m?n:m;
    while(min>1){
    if(n%min==0){
    if(m%min==0){
    System.out.println(min);
    break;

    }

    }
    min–;
    }
    }
    }

    output:

    sobhika@sobhika-HP-Laptop-15-db1xxx:~$ javac GCD.java
    sobhika@sobhika-HP-Laptop-15-db1xxx:~$ java GCD
    4
    sobhika@sobhika-HP-Laptop-15-db1xxx:~$

  • Homework:

    1. NEON NUMBERS:

    Im gonna do a program on neon number. Neon number is nothing but a number where the sum of digits of square of the number is equal to the number.

    for example:

    9power9 =81. here, 9 to the power 9 is 81 also 81 is drived as 8+1=9 bithe the ans are are then it is called neon number.

    Program:

    class Neon{
    public static void main(String args[]){
    int n=10;
    int result=n*n;
    int i=0;
    int a=0;
    while(result>0){
    a=result%10;
    i=i+a;
    result=result/10;
    }
    if(n==i){
    System.out.println(“Neon”);
    }
    else{
    System.out.println(“not Neon”);
    }
    }
    }

    output:

    sobhika@sobhika-HP-Laptop-15-db1xxx:~$ javac Neon.java
    sobhika@sobhika-HP-Laptop-15-db1xxx:~$ java Neon
    not Neon
    sobhika@sobhika-HP-Laptop-15-db1xxx:~$

    ARMSTRONG NUMBER:

    Armstrong number is nothing but one whose sum of digits raised to the power three equals the number itself. 371, for example, is an Armstrong number because 3**3 + 7**3 + 1**3 = 371.

    Program:

    class Armstrong{
    public static void main(String args[]){
    int n=153;
    int m=n;
    int c=0;
    int res;
    while(n>0){
    res=n%10;
    c=c+(resresres);
    n=n/10;
    }
    if(m==c){
    System.out.println(“Armstrong”);
    }
    else{
    System.out.println(“not a Armstrong”);
    }
    }
    }

    sobhika@sobhika-HP-Laptop-15-db1xxx:~$ javac Armstrong.java
    sobhika@sobhika-HP-Laptop-15-db1xxx:~$ java Armstrong
    Armstrong
    sobhika@sobhika-HP-Laptop-15-db1xxx:~$

  • Homework problems

    Power program;

    class Power{
    public static void main(String args[]){
    int base=2;
    int ex=4;
    int result=1;
    while(ex>0){
    result=result*base;
    ex–;
    }
    System.out.println(result);
    }
    }

    output;

    sobhika@sobhika-HP-Laptop-15-db1xxx:~$ javac Factorial.java
    sobhika@sobhika-HP-Laptop-15-db1xxx:~$ java Factorial
    3628800

    Factorial program:

    class Factorial{
    public static void main(String args[]){
    int n=10;
    int h=1;
    int result=1;
    while(h<=10){
    result=result*h;
    h=h+1;
    }
    System.out.println(result);
    }
    }

    output:

    sobhika@sobhika-HP-Laptop-15-db1xxx:~$ javac Power.java
    sobhika@sobhika-HP-Laptop-15-db1xxx:~$ java Power
    16

  • Palindrome:

    palindrome is nothing but while revering the word we get the same word as result.

    example:

    wow=wow

    level=level

    mom=mom

    dad=dad

    malayalam=malayalam

    program:

    class Palindrome{
    public static void main(String[] args) {
            String s="sobhi";
            String rs="";
            int len=s.length()-1;
            while(len>=0){
                rs=rs+s.charAt(len);
                  len=len-1;
            }
            if(s.equals(rs)){
                 System.out.println("palindrome");
            }
            else{
                 System.out.println("not a palindrome");
            }
        }
    

    output:

    sobhika@sobhika-HP-Laptop-15-db1xxx:~$ javac Palindrome.java
    sobhika@sobhika-HP-Laptop-15-db1xxx:~$ java Palindrome
    not a palindrome

  • Adding Two numbers using loop:

    We are adding the two numbers using the loop.

    this is the homework sum.

    program:

    class Add{

    int add(){
    int no=1234;
    int a=0;
    while(no>0){
    int c =no%10;
    a=a+c;
    no=no/10;
    }
    System.out.println(“\n”+”Adding in between int “+a);

    return (a);
    }
    public static void main(String args[]){
    Add a=new Add();
    a.add();
    }

    }

    output:

    sobhika@sobhika-HP-Laptop-15-db1xxx:~$ javac Add.java
    sobhika@sobhika-HP-Laptop-15-db1xxx:~$ java Add

    Adding in between int 10

  • Prime number:

    Prime number i nothing but , the number is divided by 1 or the same number. Today we learnt about prime number. By the above program we able to understand the program meaning.

    Before doing the program we have to go though the flow chart . then only we can do the program without any confusion.

    flowchart:

    program:

    public class PrimeNmber {
    public static void main(String[]args) {
    int no =1009;
    int isprime=0;
    int div=2;
    if(no<=1) {
    System.out.println(“enter valid number “);

    }
    else {
    while(div<=no/2) {
    if(no%div==0) {
    isprime=1;
    break;
    }
    div=div+1;
    }
    System.out.println(“how many time the loop has executed “+div);
    if(isprime==0) {
    System.out.println(no+” is a prime number “); }
    else {
    System.out.print(no+” its not a prime number”);
    }
    }
    }

    }

    output:

    sobhika@sobhika-HP-Laptop-15-db1xxx:~$ javac PrimeNmber.javasobhika@sobhika-HP-Laptop-15-db1xxx:~$ java PrimeNmberhow many time the loop has executed 5051009 is a prime number sobhika@sobhika-HP-Laptop-15-db1xxx:~$

  • Loop :

    Today we learn about loop. I did two program.

    Program 1:

    class Divident{
    public static void main(String args[]){
    int n=15;
    int d=1;
    while(d<n){
    if(n%d==0){
    System.out.println(d);
    }
    d=d+1;
    }
    }
    }

    output:

    sobhika@sobhika-HP-Laptop-15-db1xxx:~$ javac Divident.java
    sobhika@sobhika-HP-Laptop-15-db1xxx:~$ java Divident
    1
    3
    5

    program 2:

    class Divide{
    public static void main(String args[]){
    int n=15;
    int d=1;
    int count=0;
    while(d<=n){
    if(n%d==0){
    System.out.println(d);
    count=count+1;
    }
    d=d+1;
    }
    System.out.println(count);
    }
    }

    output:

    sobhika@sobhika-HP-Laptop-15-db1xxx:~$ javac Divide.javasobhika@sobhika-HP-Laptop-15-db1xxx:~$ java Divide135154

  • Looping statement:

    The Java for loop is a control flow statement that iterates a part of the programs multiple times. The Java while loop is a control flow statement that executes a part of the programs repeatedly on the basis of given boolean condition.

    While Loop:

    The Java while loop is used to iterate a part of the program repeatedly until the specified Boolean condition is true. As soon as the Boolean condition becomes false, the loop automatically stops.

    Program:

    class Notes{
    public static void main(String args[]){
    int n=1;

    while (n<=9){
    System.out.println(n);
    n=n+2;
    }
    }
    }

    output:

    sobhika@sobhika-HP-Laptop-15-db1xxx:~$ javac Notes.java
    sobhika@sobhika-HP-Laptop-15-db1xxx:~$ java Notes
    1
    3
    5
    7
    9

    if else if:

    class Multiple{
    public static void main(String args[]){
    int n=1;
    while(n<=50){
    if(n%3==0)
    {
    System.out.println(“yes”);
    }
    else if(n%5==0){
    System.out.println(“yes”);
    }
    else{
    System.out.println(“no”);
    }
    n=n+1;
    }
    }
    }

    output:

    sobhika@sobhika-HP-Laptop-15-db1xxx:~$ javac Multiple.java
    sobhika@sobhika-HP-Laptop-15-db1xxx:~$ java Multiple
    no
    no
    yes
    no
    yes
    yes
    no
    no
    yes
    yes
    no
    yes
    no
    no
    yes
    no
    no
    yes
    no
    yes
    yes
    no
    no
    yes
    yes
    no
    yes
    no
    no
    yes
    no
    no
    yes
    no
    yes
    yes
    no
    no
    yes
    yes
    no
    yes
    no
    no
    yes
    no
    no
    yes
    no
    yes

Design a site like this with WordPress.com
Get started