You're online now.

Hurray! you are online now.

Fascinating Number or Not in C and CPP

Three-digit number is called fascinating number when it is concatenated with two multiplications (n * 2) and three multiplications (n * 3) so that all the digit from 1 to 9 are present exactly once.

 NOTE: No matter how many zeros there are, they will be ignored. All the other digits need to occur only once.

For Example:

273 is a Fascinating Number.

273 * 1 = 273

273 * 2 = 546

273 * 3 = 819

We get the final number 273546819 by concatenating all the number. In this number, all digits from 1 to 9 appear exactly once.

C program to check Fascinating


    #include<stdio.h>
    int isFascinating(int n);
    
    int main(){
        int n;
        printf("Enter 3 digit number : ");
        scanf("%d", &n);
        if(isFascinating(n)){
            printf("Given number is Fascinating.");
        }else{
            printf("Given Number is not Fascinating.");
        }
        return 0;
    }
    int isFascinating(int n){
        int n2 = n * 2;
        int n3 = n * 3;
        int freq[11] = {0}; // to count freq. of the one digits
    
        while(n!=0){
            freq[n%10]++;
            n = n / 10;
        }
    
        while(n2 != 0){
            freq[n2 % 10]++;
            n2 = n2 / 10;
        }
        while(n3 != 0){
            freq[n3 % 10]++;
            n3 = n3 / 10;
        }
    // If the number is fascinating then the freq array must have exactly 1 in all the indexes from 1 to 9;
        for(int i = 1; i <= 9; i++){
            if(freq[i] != 1){
                return 0; // Not Fascinating No.
            }
        }
        return 1; // Fascinating No.
    }

C++ Program to check Fascinating number


    #include<iostream>
    using namespace std;
    int isFascinating(int n);
    
    int main(){
        int n;
        cout<<"Enter 3 digit number : ";
        cin>>n;
        if(isFascinating(n)){
           cout<<"Given number is Fascinating.";
        }else{
            cout<<"Given Number is not Fascinating.";
        }
        return 0;
    }
    int isFascinating(int n){
        int n2 = n * 2;
        int n3 = n * 3;
        int freq[11] = {0}; // to count freq. of the one digits
    
        while(n!=0){
            freq[n%10]++;
            n = n / 10;
        }
    
        while(n2 != 0){
            freq[n2 % 10]++;
            n2 = n2 / 10;
        }
        while(n3 != 0){
            freq[n3 % 10]++;
            n3 = n3 / 10;
        }
    // If the number is fascinating then the freq array must have exactly 1 in all the indexes from 1 to 9;
        for(int i = 1; i <= 9; i++){
            if(freq[i] != 1){
                return 0; // Not Fascinating No.
            }
        }
        return 1; // Fascinating No.
    }
🖤 0
Buy me coffee ☕

Comments

Oops!

No comments here...