Friday, 29 July 2016

Palindrome Program in Java

import java.util.*;
class Pal{
    public static void main(String args[]){
        Scanner nn=new Scanner(System.in);
        int a=nn.nextInt();
        int b=nn.nextInt();
        int r,sum=0,temp;
        int n;
       
        n=a*b;
        temp=n;
        //System.out.print(n);
        while(n>1)
        {
            r=n%10;
            sum=(sum*10)+r;
            n/=10;

        }
         System.out.println(sum+" palindrome number ");

     
    }
}

Thursday, 28 July 2016

Largest prime factor

ProjectEuler 

Problem 3


The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?


import java.util.*;
class prime{
    public static void main(String args[]){
        Scanner nn=new Scanner(System.in);
        long n=nn.nextLong();
        int i=2;
        while(n>1)
        {
            if(n%i==0)
            {
                System.out.print(i+" ");
                n/=i;
            }
            else
            i++;
        }
    }
}


Output


71 839 1471 6857 

Wednesday, 27 July 2016

Even Fibonacci numbers


ProjectEuler Problem 2

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.



class fibb{
    public static void main(String args[]){
        int a=0,b=1,c,sum=2;
        for(int i=1;i<=4000000;i++)
        {
            c=a+b;
            a=b;
            b=c;
            if(c%2==0)
            {
            sum+=c;
            //System.out.print(c);

            }
        }
        System.out.print(sum);
    }
}


output

-1833689712

Multiples of 3 and 5

ProjectEuler Problem1


If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.


import java.util.*;
class mno{
  public static void main(String args[])
  {
  Scanner s=new Scanner(System.in);
    int sum=0;
    for(int i=1;i<1000;i++)
      if(i%3==0 || i%5==0){
      //System.out.println(i);
      sum+=i;
     
      }
      System.out.print(sum);
  }
}


Power User

When it comes to computers, there are regular users and there are power users. Most people fall into the regular computer user category. These types of people use their computers for basic functions like Web browsing, sending e-mails, typing papers, working with spreadsheets, doing finances, and playing games. Regular computer users can typically get by with a middle-of-the-line computer that is fast enough to do their everyday work.
Power users, however, require top-of-the-line machines that are optimized for their work purposes. Power users include video-editing professionals, high-end graphic designers, audio producers, and those who use their computers for scientific research. Professional gamers (yes, there is such a thing) also fall under this category. These users seek the latest and greatest systems because no computer is really "fast enough" to suit their needs. Even the fastest computers can take substantial time to render large amounts of video and audio or to manipulate large images. Gamers want machines that will play their games in as many frames per second (FPS) as possible. So, to be a power user means to never be really satisfied with your system, but to always want something faster and better. Then again, that sounds like most of us, but power users usally have justifiable reasons.

Tuesday, 26 July 2016

Write a program to print all numbers from 1 to 30. If the number is a multiple of 13, print the string "lucky number" instead of the number.

import java.util.*;
class Lucky{
    public static void main(String args[]){
        Scanner m=new Scanner(System.in);
        String s="lucky number";
        for(int i=1;i<=30;i++){
            if(i%13==0){
            System.out.print(s+" ");
        }
        else
            System.out.print(i+" ");
        }
    }
}

Write a program which will take time(hour) and distance(km) as parameters and print the speed in kmph.

Write a program which will take time(hour) and distance(km) as parameters and print the speed in  kmph.
Input Specification :
First input is a value of time in hour.
Second input is a value of distance in km.
Output Specification:
Calculate the value of speed up to two decimal places.
Sample Input:
3
65
Sample Output:
21.67
Note: Print the output upto 2 decimal places

import java.util.*;
class Speed{
    public static void main(String args[]){
        Scanner s=new Scanner(System.in);
        float h=s.nextInt();
        float k=s.nextInt();
        //long kh=k/h;
        //System.out.println(kh);
        System.out.format("%.2f%n", k/h);
    }
}

Saturday, 23 July 2016

Java Pro

Odd summation

/*Write your code here */
import java.util.Scanner;
class odd{
    
    public static void main(String args[]){
        Scanner r=new Scanner(System.in);
        int a,d,l,s=0;
        a=r.nextInt();
        d=r.nextInt();
        l=r.nextInt();
        if(a>=-1000 && d<=1000 && 1>=-1000000 && 1<=1000000){
            for(int i=a;i<=l;i=i+d){
                if((i%2)!=0)
                s=s+i;
            }
            System.out.println(s);
        }
    }
}

Sum


/*Write your code here */
import java.util.Scanner;
class odd{
    
    public static void main(String args[]){
        Scanner r=new Scanner(System.in);
        int a,d,l,s=0;
        a=r.nextInt();
        d=r.nextInt();
        l=r.nextInt();
        if(a>=-1000 && d<=1000 && 1>=-1000000 && 1<=1000000){
            for(int i=a;i<=l;i=i+d){
                if((i%2)!=0)
                s=s+i;
            }
            System.out.println(s);
        }
    }
}

Pakistan vs India Cricket match

/*Write your code here */
public class match{
    public static void main(String args[]){
        int total=282;
        float req=(float)(total-(3.2*10));
        float rate=req/40;
        System.out.println(rate);
    }
}

Special Eleven

/*Write your code here */
import java.util.Scanner;
class ohno{
    public static void main(String args[]){
        
        Scanner n=new Scanner(System.in);
        int a=n.nextInt();
        if(a>0)
        {
            if(a%11==0){
                System.out.println(a);
            }
            else
                System.out.println(a+" is not a special number");
        }
        else if(a<0)
            System.out.println("Invalid Input Specification");
    }
}

His master?s card

/*Write your code here */
import java.util.Scanner;
public class mno{
    public static void main(String args[]){
         Scanner q=new Scanner(System.in);
         int n=q.nextInt();
        if((n==34)||(n==37))
            System.out.println(n+" is issued by American Express");
        else if(n>=300 && n<=305)
            System.out.println(n+" is issued by Diners Club");
        else if(n>=3528 && n<=3589)   
            System.out.println(n+" is issued by JCB");
        else if(n>=51 && n<=55)   
            System.out.println(n+" is issued by Master");
        else if(n==4)   
            System.out.println(n+" is issued by Visa");
        else
            System.out.println(n+" is not a valid IIN");
    }
    
}

Squares and Sum

/*Write your code here */
import java.util.*;
class sum{
    public static void main(String args[]){
        Scanner n=new Scanner(System.in);
        int a=n.nextInt();
        int b=n.nextInt();
        a=Math.abs(a);
        b=Math.abs(b);
        if(a==0 || b==0){
            System.out.println("Invalid Input");
        }
        else if(a!=0 && b!=0){
             if(a%2==0 && b%2==0 || a%2!=0 && b%2!=0)
           {
                System.out.println(a*a+b*b);
           }
           else{
               System.out.println((a+b)*(a+b));
           }
        }
        
    }
}

Play with 6

/*Write your code here */
import java.util.Scanner;
class mno{
    public static void main(String args[]){
        
        Scanner sc=new Scanner(System.in);
        int a=sc.nextInt();
        int b=sc.nextInt();
        int c=a+b;
        int d=Math.abs(a-b);
        if(a==6 || b==6 || c==6 || d==6)
        
        System.out.println("You can play with "+a+" and "+b+".");
        else
         System.out.println("You can not play with "+a+" and "+b+".");
    }
}

Java Programs

1.DividesSelf

/*Write your code here */
import java.util.*;
class mno{
    public static void main(String args[]){
        Scanner s=new Scanner(System.in);
        int num=s.nextInt();
        int i=num;
        while(i!=0){
            int d=i%10;
            if(i==0 || num%d!=0) System.out.print("false");
            else System.out.print("true");
            i/=10;
            break;
        }
    }
}

2.Reverse

/*Write your code here */
import java.util.*;
class mno{
    public static void main(String args[]){
        Scanner s=new Scanner(System.in);
        int n,temp=0,rem=0;n=s.nextInt();
        do{ temp=n%10;rem=rem*10+temp;n/=10; }while(n>0);
        System.out.println(rem);
    }
}

3.Mini Matrix Calculator

/*Write your code here */
import java.util.*;
class mno{
    public static void main(String args[]){
        Scanner s=new Scanner(System.in);
        int[][] a=new int[3][3]; 
        int[][] b=new int[3][3];
        int i,j;
        for(i=0;i<3;i++){
            for(j=0;j<3;j++){
                a[i][j]=s.nextInt();
            }
        }
        for(i=0;i<3;i++){
            for(j=0;j<3;j++){
                b[i][j]=s.nextInt();
            }
        }
        for(i=0;i<3;i++){
            for(j=0;j<3;j++){
                System.out.print(a[i][j]+b[i][j]);
                if(j<2){
                    System.out.print(" ");
                }
            }
            System.out.println(" ");
        }
        System.out.println("\n");
    for(i=0;i<3;i++){
            for(j=0;j<3;j++){
                System.out.print(a[i][j]-b[i][j]);
                if(j<2){
                    System.out.print(" ");
                }
            }
            System.out.println("\n");
        }
       



    }

}

4.Error evaluation

/*Write your code here */
import java.util.*;
class imn{
    public static void main(String args[]){
        Scanner n=new Scanner(System.in);
        double d=n.nextDouble();
        double a=(d+d)+((d*d)/100);
        System.out.println(a);
    }
}

5.ATM Cash Withdrawal

/*Write your code here */
import java.util.*;
class mno{
    public static void main(String args[]){
        Scanner s=new Scanner(System.in);
        int x=s.nextInt();
        int y=s.nextInt();
        if(x%100==0)
        {
            if(x<y)
            {
                int z=(y-(x+5));    
                System.out.println(z);
            }
            else
            {
                System.out.println(y);
            }
        }
        else
             System.out.println(y);
    }
}

Even Odd In Java

Get comma separated String of numbers from user and print the set of odd numbers and even numbers.
sample input:
1,2,3,4,5,6,7,8,9,10
Sample output:
Odd Numbers:
1,3,5,7,9
Even Numbers:
2,4,6,8,10
Sample input:
20,30,40
Sample output:
Even Numbers:
20,30,40


/*Write your code here */
import java.util.*;
class mno{
    public static void main(String args[]){
        Scanner s=new Scanner(System.in);
        String a=s.nextLine();
        String[] b=a.split(",");
        
        int[] ar=new int[b.length];
        String e="",o="";
        
        for(int i=0;i<b.length;i++){
            ar[i]=Integer.parseInt(b[i]);
        }
        
        for(int i=0;i<ar.length;i++){
        if(ar[i]%2==0){
            e=e+ar[i]+",";
        }else
             o=o+ar[i]+",";

        }
        if(o.length()!=0){
            o=o.substring(0,o.length()-1);
            System.out.println("Odd Numbers:");
            System.out.println(o);
        }
        if(e.length()!=0){
            e=e.substring(0,e.length()-1);
            System.out.println("Even Numbers:");
            System.out.println(e);
        }
        
    
    }
}

Java Program to print the odd and even numbers in array.

import java.util.Scanner;
public class Even
{
    public static void main(String[] args) 
    {
        int n;
        Scanner s = new Scanner(System.in);
       System.out.print("Enter no. of elements you want in array:");
        n = s.nextInt();
        int a[] = new int[n];
       System.out.println("Enter all the elements:");
        for (int i = 0; i < n; i++) 
        {
            a[i] = s.nextInt();
        }
        System.out.print("Odd numbers:");
        for(int i = 0 ; i < n ; i++)
        {
            if(a[i] % 2 != 0)
            {
                System.out.print(a[i]+" ");
            }
        }
        System.out.println("");
        System.out.print("Even numbers:");
        for(int i = 0 ; i < n ; i++)
        {
            if(a[i] % 2 == 0)
            {
                System.out.print(a[i]+" ");
            }
        }
    }
}