You're online now.

Hurray! you are online now.

Sunny Number in programming

A number is sunny number. If 1 is added to that number and the square root of it becomes a whole number.

For Example:
3 is a sunny number.
(3 + 1) = 4 and sqrt(4) = 2, a whole number.

// Sunny Number in C program
    #include<stdio.h>
    #include<math.h>
    
    int main()
    {
        int n;
        printf("Enter the Number for check sunny number : ");
        scanf("%d", &n);
    
        double x;
        x = sqrt(n+1);
    
        if((int)x==x){
            printf("%d is sunny Number",n);
        }
        else{
            printf("%d is not sunny Number",n);
        }
        return 0;
    }
// Sunny Number in c++ program
    #include<iostream>
    #include<math.h>
    
    using namespace std;
    
    int main(){
    
        int n;
    
        cout<<"Enter the number for check sunny number : ";
        cin>>n;
    
        double x;
        x = sqrt(n + 1);
        if((int)x == x){
            cout<<n<<" is sunny Number";
        }
        else{
            cout<<n<<" is not sunny Number";
        }
        return 0;
    }
    

Algorithm check the sunny number

  1. START
  2. SET n and READ n
  3. SET x = sqrt(n + 1)
  4. Check if ((int)x == x) then PRINT “Given Number is a sunny number” otherwise PRINT “Given Number is not a sunny number”
  5. END

Print all sunny number between 1 to n

// Print all sunny numbers between 1 to n in c program
    #include<stdio.h>
    #include<math.h>
    
    int main(){
    
        int n;
        printf("Enter the number : ");
        scanf("%d", &n);
        double x ; // sqrt(n + 1)
        printf("\nSunny Numbers between 1 to %d : ", n);
    
        for(int i=1; i<=n; i++){
            x = sqrt(i + 1);
    
            if((int)x == x){
                printf("%d ", i);
            }
        } 
        return 0;
    }
    
// Print all sunny number between 1 to n in c++ program
    #include<iostream>
    #include<math.h>
    
    using namespace std;
    
    int main(){
    
        int n;
    
        cout<<"Enter the number : ";
        cin>>n;
    
        double x;
        cout<<"Sunny Numbers between 1 to "<<n<<" : ";
        for(int i=1; i<=n; i++){
        x = sqrt(i + 1);
        if((int)x == x){
            cout<<i<<" ";
        }
        }
        return 0;
    }
    

Algorithm to print all sunny numbers

  1. START
  2. SET n and READ x
  3. Initialized x
  4. REPEAT step 5 and step 6 when the condition (i ≤ n) true
  5. SET x = sqrt(i + 1)
  6. Check if ((int)x == x) the PRINT “i”
    [END LOOP]
  7. EXIT

Is 8 a sunny number?

A Number is a sunny number, if (n + 1) s a perfect square.
Let's find out 8 is a sunny number or not.
(8 + 1) = 9 and sqrt(9) = 3, a whole number. So 8 is a sunny number. 

🖤 0
Buy me coffee ☕

Comments

Oops!

No comments here...