Count Two Digit Prime Numbers (Id-2437)

Given a number ‘n’, write an algorithm and the subsequent ‘C’ program to count the number of two digit prime numbers in it when adjacent digits are taken. For example, if the value of ‘n’ is 114 then the two digit numbers that can be formed by taking adjacent digits are 11 and 14. 11 is prime but 14 is not. Therefore print 1.
Input Format
A number
Ouput Format
Count of two digit prime numbers that can be formed when adjacent digits are taken
Input:
the number
Processing:
#include<stdio.h>
int isprime(int n)
{     
    for (int i = 2; i < n; i++)
    if (n%i == 0)
        return 0;
    return 1;
    
}

void main()
{
    char n[10];
    int i = 0;
    char num[3];
    int number = 0;
    int count =  0;
    while (n[i + 1] != '\0')
    {
        num[0] = n[i];
        num[1] = n[i + 1];
        num[2] = '\0';
        ++i;
        number = (num[1] - '0') + (num[0] - '0')*10;
        if (isprime(number))
            ++count;
    }
}
Program:
#include<stdio.h>
int isprime(int n)
{   
    for (int i = 2; i < n; i++)
    if (n%i == 0)
        return 0;
    return 1;
   
}

void main()
{
    char n[10];
    scanf("%s", &n);
    int i = 0;
    char num[3];
    int number = 0;
    int count =  0;
    while (n[i + 1] != '\0')
    {
        num[0] = n[i];
        num[1] = n[i + 1];
        num[2] = '\0';
        ++i;
        number = (num[1] - '0') + (num[0] - '0')*10;
        if (isprime(number))
            ++count;
    }
    printf("%d",count);
}
Pseudocode:
Step1. Get the number n
Step2. define a function isprime with one parameter n to check whether n is a prime number or not if yes return 1 else return 0
Step3.divide the number into numbers of two digits and check each of these numbers using isprime function and display the number if the number is prime

Step4.End

1 comment:

Bonus Practice Problems(GMT To IST)

IST (Indian Standard Time) is 5 hours 30 minutes ahead of GMT(Greenwich Mean Time). Develop an algorithm and write the Python code to find...