C program to check neon number; Through this tutorial, we will learn how to check neon number in c program using for loop, while loop and function.
Let’s use the following 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
Programs to Check Neon Number
- 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
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 output 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 output 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 output of the above c program; as follows:
Enter a number: 1 It is a Neon number
I appreciate your work.