Program to Find nth Sunny Number

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=24
               (24+1)=25 square root of 25 =5(it is a whole number )
               so, 8 is Sunny Number.
                 
                Given Number=13
               (13+1)=14 square root of 14 =3.74(it is not a whole number )
               so, 8 is not a Sunny Number.
                                          
Problem statement:-  Program to Find nth Sunny Number.

Data requirement:-

   Input Data:- rangenumber

   Output Data:- rangenumber, letest

   Additional Data:- cnum, num1,root

Program in C

Here is the source code of the C Program to Find nth Sunny Number.

Code:

//Nth Sunny Number
#include <stdio.h>
#include<math.h>
int
main ()
{
  int i, rangenumber, num = 1, c = 0, letest = 0;
  printf ("Enter the Nth value:");
  scanf ("%d", &rangenumber);
  while (c != rangenumber)
    {
    int num1=num;
   double root;
    root=sqrt(num1+1);
   if((int)root==root)
   {
          c++;
          letest=num;
    }
      num = num + 1;
    }
  printf ("%dth Sunny number is %d", rangenumber, letest);
  return 0;
}

Input/Output:
Enter the Nth value:5
5th Sunny number is 35

Program in C++

Here is the source code of the C++ Program to Find nth Sunny Number.

Code:

#include <iostream>
#include<cmath>
using namespace std;
int
main ()
{
  int i, rangenumber, num = 1, c = 0, letest = 0;
  cout<<"Enter the Nth value:";
  cin>>rangenumber;

  int sum=0;
  while (c != rangenumber)
    {
    int num1=num;
   double root;
    root=sqrt(num1+1);
   if((int)root==root)
   {
          c++;
          letest=num;
    }
      num = num + 1;
    }
  cout<<rangenumber<<"th Sunny number is "<<letest;
  return 0;
}

Input/Output:
Enter the Nth value:10
10th Sunny number is 120

Program in Java
  
Here is the source code of the Java Program to Find nth Sunny Number.

Code:

import java.util.Scanner;
public class NthSunnyNumber {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int  rangenumber, num = 1, c = 0, letest = 0;
  System.out.println("Enter Nth number:");
  rangenumber=cs.nextInt();
           
  while (c != rangenumber)
    {
  int num1=num;
   double root;
    root=Math.sqrt(num1+1);
   if((int)root==root)
   {
          c++;
          letest=num;
    }
      num = num + 1;
    }
System.out.println(rangenumber+"th Sunny number is "+letest);
cs.close();
}
}

Input/Output:
Enter Nth number:
15
15th Sunny number is 255

Program in Python
  
Here is the source code of the Program to Find nth Sunny Number.

Code:

import math
rangenumber=int(input("Enter a Nth Number:"))
c = 0
letest = 0
num = 1
while c != rangenumber:
    num1=num
    root = math.sqrt(num1 + 1)
    if int(root) == root:
            c+=1
            letest = num

    num = num + 1
print(rangenumber,"th Sunny number is ",letest)

Input/Output:
Enter a Nth Number:7
7 th Sunny number is  63

More:-

C/C++/Java/Python Practice Question 

Post a Comment

0 Comments