Thursday, 24 August 2017

Computer Science Half Yearly Practical Paper Solution

Half-Yearly [Practical] Solution

$ Question 1 :
import java.util.Scanner;
class Question1
{
    Scanner S = new Scanner(System.in);
    public String noToDate(int x)
    {
        String ns="",temp="",str= "";
        int day = x/10000;
        int month = (x%10000)/100;
        int year = x%100;
        str = " "+Integer.toString(year);
        if(month<=12)
        {
            if(month==1)
            str = " January,"+str;
            if(month==2)
            str = " February,"+str;
            if(month==3)
            str = " March,"+str;
            if(month==4)
            str = " April,"+str;
            if(month==5)
            str = " May,"+str;
            if(month==6)
            str = " June,"+str;
            if(month==7)
            str = " July,"+str;
            if(month==8)
            str = " August,"+str;
            if(month==9)
            str = " September,"+str;
            if(month==10)
            str = " October,"+str;
            if(month==11)
            str = " November,"+str;
            if(month==12)
            str = " December,"+str;
        }
        else
        return "INVALID DATE";
        if((day==31)&&((month==2)||(month==4)||(month==6)||(month==9)||(month==11)))
        return "INVALID DATE";
        if((day>28)&&(month==2))
        {
            if(day>29)
            return "INVALID DATE";
            if((day==29)&&(isLeapYear(year)==false))
            return "INVALID DATE";
        }
        if((day<10)&&(day>20))
        {
            if(day%10==1)
            str = Integer.toString(day)+"st"+str;
            if(day%10==2)
            str = Integer.toString(day)+"nd"+str;
            if(day%10==3)
            str = Integer.toString(day)+"rd"+str;
        }
        else
        str = Integer.toString(day)+"th"+str;
        int o =day%10;
        if((o==4)||(o==5)||(o==6)||(o==7)||(o==8)||(o==9)||(o==10))
        str = Integer.toString(day)+"th"+str;
        str = str+"\nVALID DATE";
        return str;
    }
    public boolean isLeapYear(int x)
    {
        if((x%100)%4==0)
        return true;
        else
        return false;
    }
    public void main()
    {
        System.out.print("Enter the date in ddmmyy format : ");
        int date = S.nextInt();
        System.out.println(noToDate(date));
    }
}
$ Question 2 :
import java.util.Scanner;
class Question2
{
    Scanner S = new Scanner(System.in);
    public boolean isKeith(int n)
    {
        int no = n;
        int sum = 0;
        int d = nod(n);
        int digits[] = new int[nod(no)];
        int terms[] = new int[n];
        for(int i = nod(n)-1;i>=0;i--)
        {
            terms[i] = no%10;
            no=no/10;
        }
        while(sum<n)
        {
            sum = 0;
            for(int k =0;k<= nod(n);k++)
            sum=sum+terms[d-k];
            terms[d] = sum;
            d++;
        }
        if(sum==n)
        return true;
        else
        return false;
    }
    public void main()
    {
        System.out.println("Enter the range in which you want to find Keith Numbers :- ");
        System.out.print("Enter m : ");
        int m = S.nextInt();
        System.out.print("Enter n : ");
        int n = S.nextInt();
        if((m<10)||(n<10)||(m>n)||(m>=5000)||(n>=5000))
        System.out.println("Invalid Input");
        else
        System.out.print("Keith numbers between "+m+" and "+n+" are : ");
       
        for(int i= m;i<=n;i++)
        if(isKeith(i))
        System.out.print(i+",");
        System.out.println("\b\n");
    }
    public int nod(int no)
    {
        int result = 0;
        while(no>0)
        {
            result++;
            no=no/10;
        }
        return result;
    }
}
$ Question 3 :
import java.util.Scanner;
class Question3
{
    Scanner S = new Scanner(System.in);
    public boolean areAmicable(int x,int y)
    {
        if((sum_of_factors(x)==y)&&(sum_of_factors(y)==x))
        return true;
        else
        return false;
    }
    public int sum_of_factors(int x)
    {
        int sum = 0;
        for(int i = 1;i<x;i++)
        if(x%i==0)
        sum = sum + i;
        return sum;
    }
    public void main()
    {
        System.out.print("Number 1 : ");
        int a = S.nextInt();
        System.out.print("Number 2 : ");
        int b = S.nextInt();
        if(areAmicable(a,b))
        {
            System.out.print("Amicable Numbers\nSum of Factors of "+a+" : 1");
            for(int u = 2;u<a;u++)
            if(a%u==0)
            System.out.print("+"+u);
            System.out.println(" = "+b);
            System.out.print("Sum of Factors of "+b+" : 1");
            for(int u = 2;u<b;u++)
            if(b%u==0)
            System.out.print("+"+u);
            System.out.println(" = "+a);
        }
        else
        System.out.println("Not Amicable Numbers");
    }
}
     

Tuesday, 18 July 2017

Program to check for Happy Number

Program to check for Happy Number
=========================================================================
If a number is happy, then all members of its sequence are happy; if a number is unhappy, all members of the sequence are unhappy.
For example, 19 is happy, as the associated sequence is:
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1.
The 143 happy numbers up to 1,000 are:
1, 7, 10, 13, 19, 23, 28, 31, 32, 44, 49, 68, 70, 79, 82, 86, 91, 94, 97, 100, 103, 109, 129, 130, 133, 139, 167, 176, 188, 190, 192, 193, 203, 208, 219, 226, 230, 236, 239, 262, 263, 280, 291, 293, 301, 302, 310, 313, 319, 320, 326, 329, 331, 338, 356, 362, 365, 367, 368, 376, 379, 383, 386, 391, 392, 397, 404, 409, 440, 446, 464, 469, 478, 487, 490, 496, 536, 556, 563, 565, 566, 608, 617, 622, 623, 632, 635, 637, 638, 644, 649, 653, 655, 656, 665, 671, 673, 680, 683, 694, 700, 709, 716, 736, 739, 748, 761, 763, 784, 790, 793, 802, 806, 818, 820, 833, 836, 847, 860, 863, 874, 881, 888, 899, 901, 904, 907, 910, 912, 913, 921, 923, 931, 932, 937, 940, 946, 964, 970, 973, 989, 998, 1000.

The happiness of a number is unaffected by rearranging the digits, and by inserting or removing any number of zeros anywhere in the number.


The program to check if a number is Happy or not is given below : -
-------------------------------------------------------------------------------------------------------------
import java.util.Scanner;
class Happy
{
    Scanner S = new Scanner(System.in);
    int no;
    public boolean isHappy(int x)
    {
        int y = x;
        int sum = sumofdigitsquare(y);
        int u=0;
        while(sum>9)
        sum = sumofdigitsquare(sum);
        if(sum==1)
        return true;
        else
        return false;
    }
    public int sumofdigitsquare(int x)
    {
        int sum = 0;
        while(x>0)
        {
            sum = sum +((x%10)*(x%10));
            x=x/10;
        }
        return sum;
    }
    public void main()
    {
        System.out.println("Enter the number to be checked for Happy :-");
        if(isHappy(S.nextInt()))
        System.out.println("It is a Happy Number");
        else 
        System.out.println("Tis not a Happy Number");
    }
}

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

For other Number checking programs, check my other blog...<Click Here>



Saturday, 10 June 2017

Holiday Homework Assignments [XI] Solution

Computer Science Assignments Solution
_________________________________________________________________________________________________________
Assignments PDF :-
Download Link


=====================================================================
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
=====================================================================
SOLUTIONS :-
Assignment 1
import java.util.Scanner;
class Assignment_1
{
    Scanner S = new Scanner(System.in);
    public int digit(int x,int y)
    {
        int j = x;
        int i = 1;
        while(j>0)
        {
           
            int d = j%10;
            j=j/10;
           
            if(i==y)
            return d;
            i++;
        }
        return j;
    }
    public int no_of_digits(int x)
    {
        int y = x;
        int nod = 0;
        while(y>0)
        {
            y=y/10;
            nod++;
        }
        return nod;
    }
    public boolean isKaprekar(int x)
    {
        int square = x*x;
        int nod = no_of_digits(square);
        double n1=0,n2=0;
       
        if(nod%2==0)
        {
            for(int i=1;i<=nod/2;i++)
            n2 =n2+ digit(square,i)*Math.pow(10,(i-1));
            for(int i=(nod/2)+1;i<=nod;i++)
            n1 = n1+ digit(square,i)*Math.pow(10,(i-(nod/2)-1));
        }
        else
        {
            for(int i=1;i<=(nod+1)/2;i++)
            n2 = n2+ digit(square,i)*Math.pow(10,(i-1));
            for(int i=((nod+1)/2)+1;i<=nod;i++)
            n1 = n1+ digit(square,i)*Math.pow(10,(i-((nod+1)/2)-1));
        }
        if((n1+n2)==x)
        return true;
        else
        return false;
    }
}
Assignment 2
import java.util.Scanner;
class Assignment_2
{
    Scanner S = new Scanner(System.in);
    public boolean isPalindrome(int x)
    {
        int sum = 0;
        int y = x;
        while(x!=0)
        {
            sum = sum*10;
            sum = sum+(y%10);
            y= y/10;
        }
        if(sum==y)
        return true;
        else
        return false;
    }
    public boolean isPrime(int x)
    {
        int killme = 0;
        for(int i = 2;i<x;i++)
        {
            if(x%i==0)
            killme++;
            else
            continue;
        }
        if(killme>0)
        return false;
        else
        return true;
    }
        public boolean isPrimePalindrome(int x)
    {
        int y = x;
        if(isPrime(y))
        {
            String str = Integer.toString(y);
            String ns = "";
            for(int i = 0;i<str.length();i++)
             ns = str.charAt(i)+ns;
            if(ns.equalsIgnoreCase(str))
            return true;
        }
        return false;
    }
    void main()
    {
        System.out.println("Enter the number to be checked for Prime Palindrome");
        int no = S.nextInt();
       
        if(isPrimePalindrome(no))
            System.out.println("It is a Prime Palindrome No.");
            else
            System.out.println("Not a Prime Palindrome No.");
    }
}
Assignment 3
import java.util.Scanner;
class Assignment_3
{
    Scanner S = new Scanner(System.in);
    public void main()
    {
        System.out.println("Enter the ISBN Number");
        int no = S.nextInt();
        if(isISBN(no))
        System.out.println("It is a legal ISBN No.");
        else
        System.out.println("It is not a legal ISBN No.");
    }
    public boolean isISBN(long x)
    {
        long c = x;
        long sum = 0;
        for(int i = 1;i<=10;i++)
        {
            long d = c%10;
            c=c/10;
            sum = sum + (d*i);
        }
        if((sum%11)==0)
        return true;
        else 
        return false;
    }
}
Assignment 4
import java.util.Scanner;
class Assignment_4
{
    Scanner S = new Scanner(System.in);
    public int unknown(int M,int N)
    {
        int result=0;
        for(int i = M;i<=10000;i++)
        {
            int o = i;
            int sum = 0;
            while(o>0)
            {
                int d = o%10;
                o=o/10;
                sum = sum + d;
            }
            if(sum==N)
            return i;
        }
        return result;
    }
    public void main()
    {
        System.out.println("Enter the values of M and N");
        int M = S.nextInt();
        int N = S.nextInt();
        if(((M>=100)&&(M<=10000))&&((N>0)&&(N<100)))
        System.out.println("Result is "+unknown(M,N));
        else
        System.out.println("Wrong values of M and N Entered!");
    }
}
         
Assignment 5
import java.util.Scanner;
class Assignment_5
{
    Scanner S = new Scanner(System.in);
    public int no_of_digits(int x)
    {
        int y = x;
        int nod=0;
        while(y>0)
        {
            y=y/10;
            nod++;
        }
        return nod;
    }
        public boolean isPrime(int x)
    {
        int killme = 0;
        for(int i = 2;i<x;i++)
        {
            if(x%i==0)
            killme++;
            else
            continue;
        }
        if(killme>0)
        return false;
        else
        return true;
    }
    public boolean isCircularPrime(int x)
    {
        int nod = no_of_digits(x);
        int y = x;
        while(isPrime(y)==true)
        {
            int c = y%10;
            y = y/10;
            y=(c*(power(10,(nod-1))))+y;
            if(x==y)
            return true;
        }
        return false;
    }
    public int power(int x, int y)
    {
        int result = 1;
        while(y>0)
        {
            result = result*x;
            y--;
        }
        return result;
    }
}
     
-----x-----x-----x-----x-----x-----x-----x-----x-----x-----x-----x-----