Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using static System.Console;
- namespace Cubic_Numbers_Challenge
- {
- internal class Program
- {
- public static void Main(string[] args)
- {
- // first, we ask the user for a number
- // then, we send the input to the IsCubic method
- WriteLine("Please enter a number");
- var number = ReadLine();
- IsCubic(number);
- }
- private static void IsCubic(string input)
- {
- // first we see each character individually
- var characters = new List<char>();
- foreach (var character in input)
- {
- characters.Add(character);
- }
- // we evaluate the cubic value of each character
- var results = new int[characters.Count];
- for (int i = 0; i < results.Length; i++)
- {
- var value = int.Parse(characters[i].ToString());
- var powerValue = (int) Math.Pow(value, 3); // value to the third power
- results[i] = powerValue;
- }
- // now we compare the sum of all results to the entered value and determine if it is cubic or not
- int sum = 0;
- foreach (int number in results)
- {
- sum += number;
- }
- var enteredValue = int.Parse(input);
- if (enteredValue == sum)
- {
- WriteLine("Lucky");
- }
- else
- {
- WriteLine("Unlucky");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement