Find out all Sunny numbers present within a given range

A Number is a Sunny number if 1 is added to that number and the square root of it becomes a whole number.

Example:

               Given Number=35
               (35+1)=36 square root of 36 =6(it is a whole number )
               so, 8 is Sunny Number.
                 
                Given Number=11
               (11+1)=12 square root of 12 =3.46(it is not a whole number )
               so, 8 is not a Sunny Number.
                                          
Problem statement:-  Program to Find out all Sunny numbers present within a given range.

Data requirement:-

   Input Data:- range1, range2

   Output Data:- i, range1, range2

   Additional Data:- num, root

Program in C

Here is the source code of the C Program to Find out all Sunny numbers present within a given range.

Code:

//Sunny Number in range
#include<stdio.h>
#include<math.h>
int main()
{
    int range1,range2,i;
    printf("Enter a range:");
    scanf("%d %d",&range1,&range2);
    printf("Sunny Numbers between %d and %d are: ",range1,range2);
    for(i=range1;i<=range2;i++)
    {
    double root;
    root=sqrt(i+1);
   if((int)root==root)
    printf("%d ",i);
}
}

Input/Output:
Enter a range:1
20
Sunny Numbers between 1 and 20 are: 3 8 15

Program in C++

Here is the source code of the C++ Program to Find out all Sunny numbers present within a given range.

Code:

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
    int range1,range2,i;
    cout<<"Enter a range:";
    cin>>range1;
    cin>>range2;
    cout<<"Sunny numbers between "<<range1<<" and "<<range2<<" are: ";
    for(i=range1;i<=range2;i++)
    {
    double root;
    root=sqrt(i+1);
   if((int)root==root)
    cout<<i<<" ";
}
}

Input/Output:
Enter a range:20
100
Sunny numbers between 20 and 100 are: 24 35 48 63 80 99

Program in Java
  
Here is the source code of the Java Program to Find out all Sunny numbers present within a given range.

Code:

import java.util.Scanner;
public class FindSunnyNumbersInRange {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
      int range1,range2;
      System.out.println("Enter a range:");
      range1=cs.nextInt();
      range2=cs.nextInt();
      System.out.println("Sunny numbers between "+range1+" and "+range2+" are: ");
      for(int i=range1;i<=range2;i++)
      {
      double root;
      root=Math.sqrt(i+1);
      if((int)root==root)
      System.out.print(i+" ");
      }
      cs.close();
}
}

Input/Output:
Enter a range:
100
200
Sunny numbers between 100 and 200 are: 
120 143 168 195 


Program in Python
  
Here is the source code of the Program to Find out all Sunny numbers present within a given range.

Code:

import math
print("Enter a range:")
range1=int(input())
range2=int(input())
print("Sunny numbers between ",range1," and ",range2," are: ")
for i in range(range1,range2+1):
    root = math.sqrt(i+ 1)
    if int(root)==root:
        print(i,end=" ")

Input/Output:
Enter a range:
300
400
Sunny numbers between  300  and  400  are: 
323 360 399 

More:-

C/C++/Java/Python Practice Question 

Post a Comment

0 Comments