C Program to Check Neon Number

In this tutorial, i am going to show you how to check neon number in c program using for loop, while loop and function.

Algorithm and Programs to Check Neon Number in C

  • Algorithm to Check a Number is Neon or Not
  • C Program to Check Neon Number using For Loop
  • C Program to Check Neon Number using While Loop
  • C Program to Check Neon Number using Function

Algorithm to Check a Number is Neon or Not

Follow the below given algorithm to write a program to check whether a number is neon or not; as follows:

  • Take the number as input from the user
  • Find the square of the number
  • Find the sum of the digits in the square
  • Compare the sum with the number. If both are equal then it is a Neon number

C Program to Check Neon Number using For Loop

#include <stdio.h>
int main()
{
    int i, num, square, sumDigits = 0;
    printf("Enter a number: ");
    scanf("%d", &num);
    square = num * num;
    for(i = 0; i<=square; i++)
    {
        sumDigits += square % 10;
        square /= 10;
    }
    if (num == sumDigits)
    {
        printf("It is a Neon number\n");
    }
    else
    {
        printf("It is not a Neon number\n");
    }
    return 0;
}

The result of the above c program; as follows:

Enter a number: 10
It is not a Neon number

C Program to Check Neon Number using While Loop

#include <stdio.h>
int main()
{
    int num, square, sumDigits = 0;
    printf("Enter a number: ");
    scanf("%d", &num);
    square = num * num;
    while (square != 0)
    {
        sumDigits += square % 10;
        square /= 10;
    }
    if (num == sumDigits)
    {
        printf("It is a Neon number\n");
    }
    else
    {
        printf("It is not a Neon number\n");
    }
    return 0;
}

The result of the above c program; as follows:

Enter a number: 9
It is a Neon number

C Program to Check Neon Number using Function

#include <stdio.h>
int isNeon(int num)
{
    int square, sumDigits = 0;
    square = num * num;
    while (square != 0)
    {
        sumDigits += square % 10;
        square /= 10;
    }
    if (num == sumDigits)
    {
        printf("It is a Neon number\n");
    }
    else
    {
        printf("It is not a Neon number\n");
    }
}
int main()
{
    int n;
    printf("Enter a number: ");
    scanf("%d", &n);
    
    isNeon(n);
    return 0;
}

The result of the above c program; as follows:

Enter a number: 1
It is a Neon number

Be First to Comment

Leave a Reply

Your email address will not be published.