Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,026 questions

51,982 answers

573 users

How to check whether a number is magic number in C#

2 Answers

0 votes
// A magic number is a number that repeated the sum of its digits 
// #till we get a single digit is equal to 1
 
// 1729 = 1 + 7 + 2 + 9 = 19 
// 19 = 1 + 9 = 10
// 10 = 1 + 0 = 1

using System;

class Program
{
    static void Main() {
        int num = 1729;
        int digitCount = (int)Math.Log10(num) + 1;
        int sumOfDigits = 0;
 
        int temp = num; 
         
        while (digitCount > 1) {
            sumOfDigits = 0;
         
            while (temp > 0) {
                sumOfDigits += temp % 10;
                temp = temp / 10;
            }
             
            temp = sumOfDigits;
            Console.WriteLine(sumOfDigits);
               
            digitCount = (int)Math.Log10(sumOfDigits) + 1;
        }
         
        if (sumOfDigits == 1)
            Console.Write("Magic number");
        else
            Console.Write("Not a magic number");
    }
}




/*
run:

19
10
1
Magic number

*/

 



answered Oct 30, 2021 by avibootz
0 votes
// A magic number is that number that repeated sum of its digits 
// #till we get a single digit is equal to 1
 
// 1729 = 1 + 7 + 2 + 9 = 19 
// 19 = 1 + 9 = 10
// 10 = 1 + 0 = 1

using System;

class Program
{
    static bool magicNumber(int number) {  
        if( ((number - 1) % 9) == 0)  
            return true;  
        else  
            return false;  
    }  
    static void Main() {
        int num = 1729;

        if (magicNumber(num))
            Console.Write("Magic number");
        else
            Console.Write("Not a magic number");
    }
}




/*
run:

Magic number

*/

 



answered Oct 30, 2021 by avibootz

Related questions

1 answer 48 views
2 answers 151 views
2 answers 148 views
2 answers 197 views
...