Methods or Functions



Example Programs



Program [1]

A Circular Prime is a prime number that remains prime under cyclic shifts of its digits. When the leftmost digit is removed and replaced at the end of the remaining string of digits, the generated number is still prime. The process is repeated until the original number is reached again.

A number is said to be prime if it has only two factors I and itself.

Write a program to accept one no check the number where it is Circular Prime or Not.

Example 1

INPUT :N = 197

OUTPUT: 197 971 719 197 IS A CIRCULAR PRIME

Example 2

INPUT :N = 1193

OUTPUT: 1193 1931 9311 3119 1193 IS A CIRCULAR PRIME

Example 3

INPUT :N = 29

OUTPUT: 29 92 29 IS NOT A CIRCULAR PRIME


Solution 1 :

import java.util.*;
class CircularPrime
{
    public static void main(String arr[])
    {
        Scanner sc=new Scanner(System.in);
        int i,j,n,p,r1,r2,c=0,f=0;
        System.out.println("Enter an Number ");
        n=sc.nextInt();
        System.out.println("The different Combination of Prime number s are:");
        p=n;c=0;
        while(p>0)
        {
            p=p/10;
            c++;
        }
        p=n;
        outer:
        for(i=1;i<=c;i++)
        {
            f=1;
            for(j=2;j            {
                if(p%j==0)             
                {
                    f=0;
                    break outer;
                }
            }
            if(f==1)
            {
                System.out.println(p);
                r1=(int)(p/Math.pow(10,c-1));
                r2=(int)(p%Math.pow(10,c-1));
                p=r2*10+r1;
            }
        }
        if(f==0)
        {
            System.out.println("Not a circular Prime");
        }
        else
        {
            System.out.println("Hence,"+n+"is a circular Prime");
        }
    }
}


Solution 2:

import java.util.*;
class CircularPrime
{
    static boolean isprime(int num)
    {
        boolean flag=true;
        for(int a=2;a<=num/2;a++)
        {
            if(num%a==0)
            {
                flag=false;
                break;
            }
        }
        return(flag);
    }
    public static void main(String arr[])
    {
        Scanner sc=new Scanner(System.in);
        int num,count=0,temp,base;
        System.out.println("Enter any Number");
        num=sc.nextInt();
        temp=num;
        while(temp>0)
        {
            count++;
            temp=temp/10;
        }
        base=(int)Math.pow(10,count-1);
        boolean flag=true;
        for(int a=1;a<=count;a++)
        {
            num=(num%base)*10+(num/base);
            if(CircularPrime.isprime(num)==false)
            {
                flag=false;
                break;
            }
            System.out.println(num);
        }
        if(flag==true)
        {
            System.out.println("Number is Circular Prime");
        }
        else
        {
            System.out.println("Number is Not Circular Prime");
        }
    }
}

View Solution

Program [2]

Write a Program in Java to input a number and check whether it is a Bouncy Number or not.

Increasing Number : Working from left-to-right if no digit is exceeded by the digit to its left it is called an increasing number; for example, 22344.

Decreasing Number : Similarly if no digit is exceeded by the digit to its right it is called a decreasing number; for example, 774410.

Bouncy Number : We shall call a positive integer that is neither increasing nor decreasing a “bouncy” number; for example, 155349. Clearly there cannot be any bouncy numbers below 100.

Sample:

Enter a number : 22344

The number 22344 is Increasing and Not Bouncy

Enter a number : 774410

The number 774410 is Decreasing and Not Bouncy

Enter a number : 155349

The number 155349 is bouncy


View Solution

Program [3]

Design a program to accept a fifteen digit number from the user and check whether it is a valid IMEI number or not. For an invalid input, display an appropriate message.

The International Mobile Station Equipment Identity or IMEI is a number, usually unique, to identify mobile phones, as well as some satellite phones. It is usually found printed inside the battery compartment of the phone.

The IMEI number is used by a GSM network to identify valid devices and therefore can be used for stopping a stolen phone from accessing that network.

The IMEI (15 decimal digits: 14 digits plus a check digit) includes information on the origin, model, and serial number of the device.

The IMEI is validated in three steps:

1. Starting from the right, double every other digit (e.g., 7 becomes 14).

2. Sum the digits (e.g., 14 → 1 + 4).

3. Check if the sum is divisible by 10.

Sample output

1. Enter a 15 digit IMEI code : 654122487458946 Output : Sum = 80 Valid IMEI Code

2. Enter a 15 digit IMEI code : 799273987135461 Output : Sum = 79 Invalid IMEI Code

3. Enter a 15 digit IMEI code : 79927398713 Output : Invalid Input


View Solution

Program [4]

Design a program to accept a day number (between 1 and 366), year (in 4 digits) from the user to generate and display the corresponding date. Also, accept ‘N’ (1 <= N <= 100) from the user to compute and display the future date corresponding to ‘N’ days after the generated date. Display an error message if the value of the day number, year and N are not within the limit or not according to the condition specified.


Test your program with the following data and some random data:

Example 1


INPUT:

DAY NUMBER: 255

YEAR: 2018

DATE AFTER (N DAYS): 22


OUTPUT:

DATE: 12 TH SEPTEMBER, 2018

DATE AFTER 22 DAYS: 4 TH OCTOBER, 2018


Example 2


INPUT:

DAY NUMBER: 360

YEAR: 2018

DATE AFTER (N DAYS): 45


OUTPUT:

DATE: 26 TH DECEMBER, 2018

DATE AFTER 45 DAYS: 9 TH FEBRUARY, 2019


Example 3


INPUT:

DAY NUMBER: 500

YEAR: 2018

DATE AFTER (N DAYS): 33


OUTPUT:

DAY NUMBER OUT OF RANGE.


Example 4


INPUT:

DAY NUMBER: 150

YEAR: 2018

DATE AFTER (N DAYS): 330


OUTPUT:

DATE AFTER (N DAYS) OUT OF RANGE.


View Solution

Program [5]

A Prime-Adam integer is a positive integer (without leading zeros) which is a Prime as well as an Adam number.

Prime Number : A number which has only two factors, i.e. 1 and the number itself.

Example: 2, 3, 5, 7 …etc.

Adam number: The square of a number and the square of its reverse are reverse to each other.

Example : If n=13 and reverse of ‘n’ =31, then,

(13)2  = 169

(31)2  = 961 which is reverse of 169

thus 13, is an Adam number.


Accept two positive integers m and n, where m is less than n as user input. Display all Prime-Adam integers that are in the range between m and n (both inclusive) and output them along with the frequency, in the format given below:


Test your program with the following data and some random data:


Example 1


INPUT:


m=5

n=100


OUTPUT:


THE PRIME-ADAM INTEGERS ARE:

11 13 31

FREQUENCY OF PRIME-ADAM INTEGERS IS: 3


Example 2


INPUT:

m=100

n=200


OUTPUT:


THE PRIME-ADAM INTEGERS ARE:

101 103 113

FREQUENCY OF PRIME-ADAM INTEGERS IS: 3


Example 3


INPUT:

m=50

n=70


OUTPUT:


THE PRIME-ADAM INTEGERS ARE:

NIL

FREQUENCY OF PRIME-ADAM INTEGERS IS: 0


Example 4


INPUT:

m=700

n=450


OUTPUT: INVALID INPUT.


View Solution

Program [6]

Write a program to print all the Krishnamurti number from 1 to n. Here, n is user dependent. A Krishnamurti number is a number whose sum of factorial of individual digits equals the number.

For example, 145 = 1! + 4! + 5! = 1 + 24+ 120 = 145.


View Solution

Program [7]

The International Standard Book Number (ISBN) is a unique numeric book identifier which is printed on every book. The ISBN is based upon a 10-digit code. The ISBN is legal if:

1 x digit1 + 2 x digit2 + 3 x digit3 + 4 x digit4  + 5 x digit5  + 6 x digit6  + 7 x digit7 + 8 x digit8  + 9 x digit9 + 10 x digit10 is divisible by 11.

Example: 

For an ISBN 1401601499

Sum=1 x 1 + 2 x 4 + 3 x 0 + 4 x 1 + 5 x 6 + 6 x 0 + 7 x 1 + 8 x 4 + 9 x 9 + 10 x 9 = 253 which is divisible by 11.

Write a program to:

(i) Input the ISBN code as a 10-digit integer.

(ii) If the ISBN is not a 10-digit integer, output the message, "Illegal ISBN" and terminate the program.

(iii) If the number is 10-digit, extract the digits of the number and compute the sum as explained above. If the sum is divisible by 11, output the message, "Legal ISBN". If the sum is not divisible by 11, output the message, "Illegal ISBN".



View Solution

Program [8]

A positive whole number ‘n’ that has ‘d’ number of digits is squared and split into two pieces, a right-hand piece that has ‘d’ digits and a left-hand piece that has remaining ‘d’ or ‘d-1’ digits. If the sum of the two pieces is equal to the number, then ‘n’ is a Kaprekar number. The first few Kaprekar numbers are: 9, 45, 297 ……..


Example 1:


9

92 = 81, right-hand piece of 81 = 1 and left hand piece of 81 = 8 Sum = 1 + 8 = 9, i.e. equal to the number.


Example 2:


45

452 = 2025, right-hand piece of 2025 = 25 and left hand piece of 2025 = 20 Sum = 25 + 20 = 45, i.e. equal to the number.


Example 3:


297

2972 = 88209, right-hand piece of 88209 = 209 and left hand piece of 88209 = 88 Sum = 209 + 88 = 297, i.e. equal to the number.


Given the two positive integers p and q, where p < q, write a program to determine how many Kaprekar numbers are there in the range between p and q (both inclusive) and output them.


The input contains two positive integers p and q. Assume p < 5000 and q < 5000. You are to output the number of Kaprekar numbers in the specified range along with their values in the format specified below:


SAMPLE DATA:


INPUT:

p = 1

q = 1000 OUTPUT:

THE KAPREKAR NUMBERS ARE:- 1, 9, 45, 55, 99, 297, 703, 999

FREQUENCY OF KAPREKAR NUMBERS IS: 8



View Solution

Program [9]

A prime palindrome integer is a positive  integer  (without  leading  zeros)  which  is  prime  as  well as a palindrome. Given two positive integers m and n, where m < n, write a program to determine how many  prime-palindrome integers  are  there  in  the  range  between  m  and  n  (both inclusive) and output them.

The input contains two positive integers  m  and  n  where  m  <  3000  and  n  <  3000.  Display  the number of prime-palindrome integers in the specified range along with their values in the format specified below:

Test your program with the sample data and some random data:

Example 1

INPUT: m = 100 n = 1000

OUTPUT: THE PRIME PALINDROME INTEGERS ARE:

101, 131, 151, 181, 191, 313, 353, 373, 383, 727, 757, 787, 797, 919, 929

FREQUENCY OF PRIME PALINDROME INTEGERS : 15

Example 2

INPUT: m = 100 n = 5000

OUTPUT: OUT OF RANGE


View Solution

Program [10]

A Composite Magic number is a positive integer which is composite as well as a magic number. Composite number: A composite number is a number that has more than two factors.

For example: 10

Factors are: 1, 2, 5, 10

Magic number: A magic number is a number in which the eventual sum of the digits is equal to 1

For example: 28=2+8=10=1+0=1

Accept two positive integers m and n, where m is less than n as user input. Display the number of Composite magic integers that are in the range between m and n (both inclusive) and output them along with the frequency, in the format specified below.

Test your program with the sample data and some random data:

Example 1:

INPUT: m = 10 n = 100

OUTPUT:

THE COMPOSITE MAGIC INTEGERS ARE: 10, 28, 46, 55, 64, 82, 91, 100

FREQUENCY OF COMPOSITE MAGIC INTEGERS IS: 8

Example 2:

INPUT: m = 1200 n = 1300

OUTPUT:

THE COMPOSITE MAGIC INTEGERS ARE:

1207, 1216, 1225, 1234, 1243, 1252, 1261, 1270, 1288

FREQUENCY OF COMPOSITE MAGIC INTEGERS IS: 9

Example 3:

INPUT: m = 120 n = 99

OUTPUT:

INVALID INPUT



View Solution

Program [11]

A positive natural number , (for example 27) can be represented as follows:

2+3+4+5+6+7

8+9+10

13+14

where every row represents a combination of consecutive natural numbers, which add up to 27.

Write a program which inputs a positive natural number N and prints the posibale consecutive  number combinations, which when added given N.

Test your program for the following data and some random data.


Solutions 1:


import java.util.*;
public class Consecutive
{
    public static void main(String arr[])
    {
        Scanner sc=new Scanner(System.in);
        int i,j,k,n,sum;
        System.out.println("Enter a number");
        n=sc.nextInt();
        for(i=1;i<=n/2+1;i++)
        {
            sum=0;
            for(j=i;j<=n/2+1;j++)
            {
                sum=sum+j;
                if(sum==n)
                break;
            }
            if(j<=n/2+1)
            {
                for(k=i;k<=j;k++)
                System.out.print(k+" ");
                System.out.println();
            }
        }
    }
}
Solution 2: (Using Function)

import java.util.*;
class number
{
   
    int sum(int i,int num)
    {
        int s1=0;
        for(int x=i;s1<num;x++)
        {
           
                s1=s1+x;
            
        }
        return (s1);
    }
    public static void main(String args[])
    {
        Scanner sc=new Scanner (System.in);
        System.out.println("Enter a number");
        int n=sc.nextInt();
        int s;
        number obj=new number();
        for(int j=1;j<=n;j++)
        {    
        int ans=obj.sum(j,n);
        s=0;
        if(ans==n)
        {
            for(int y=j;s<n;y++)
            {
                s=s+y;
                System.out.print(y+"  ");
            }
            System.out.println();
        }
        
    }
}
}

View Solution

Program [12]

A Class quad contains the following data members and member function s to find the roots of quadratic equation.


Class Name : quad


Data Members:

a,b,c(float) x,y(float)

Member Function/Method:

quad(float,float,float): constructor to assign values to the data members

float discriminant(): to return the discriminant [b1-4ac]

void root_equal(): to display the root if both roots are equal.

void image(): to display the root if roots are imaginary.

void root_real(): to display the two real, unequal roots.

void root(): to call other appropriate functions to find the solutions of the problem.


View Solution

Program [13]

A Goldbach number is a positive even integer that can be expressed as the sum of two odd primes. Note: All even integer numbers greater than 4 are Goldbach numbers. Example: 6 = 3 + 3, 10 = 3 + 7, 10 = 5 + 5 Hence, 6 has one odd prime pair 3 and 3. Similarly, 10 has two odd prime pairs, i.e. 3 and 7, 5 and 5.

Write a program to accept an even integer ‘N’ where N > 9 and N < 50. Find all the odd prime pairs whose sum is equal to the number ‘N’.

Test your program with the following data and some random data:

Example 1: INPUT: N = 14

OUTPUT: PRIME PAIRS ARE: 3, 11 7, 7

Example 2: INPUT: N = 30

OUTPUT: PRIME PAIRS ARE: 7, 23 11, 19 13, 17

Example 3: INPUT: N = 17

OUTPUT: INVALID INPUT. NUMBER IS ODD.

Example 4: INPUT: N = 126

OUTPUT: INVALID INPUT. NUMBER OUT OF RANGE.


View Solution

Program [14]

Write java Function to check number is prime or not using function


View Solution

CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) O Level NIELIT Study material and Quiz Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Website development Company in Allahabad Sarkari Exam Quiz