ttmind

Main Navigation

ttmind
  • jim-jams
  • Tech
  • Positive
Login

Login

Facebook Google

OR

Remember me Forgot password?

Don't have account? Signup here.

Sort by Categorys

.Net

PHP

Java

JavaScript

Database

Server

Client Side

Tools

Artificial Intelligence

Cloud

Hybrid Development

Event

Smart City

Education

Security

Scrum

Digital Marketing

APP Development

Business

Internet

Simulation

Art

Network

Microservices

Architecture

Technology

Leadership

    Top Articles

  • How Does Social Media Bring People Together?
    TTMind Author
  • How to read appSettings JSON from Class Library in ASP.NET Core
    Anil Shrestha
  • Printing Support In Asp.Net Core
    TTMind Author
  • HOW TO EXTRACT TEXT FROM IMAGE USING JAVASCRIPT (OCR with Tesseract.js)?
    Prakash Pokhrel
  • Images Upload REST API using ASP.NET Core
    Prakash Pokhrel
  • Related Topic

  • How to read appSettings JSON from Class Library in ASP.NET Core
  • Printing Support In Asp.Net Core
  • Images Upload REST API using ASP.NET Core
  • How to use IActionFilter, IAsyncActionFilter in ASP.NET Core MVC?
  • ASP.NET CORE - Blazor CRUD operation using ADO.NET
  • Tech
  • About Us
  • Contact Us
  • TechHelp
  • PositiveHelp
  • Jim-Jams Help
  • Terms & Conditions

© Copyright ttmind.com

Main Content

Magic Array:Tricky Question Solution in C#

.Net C# about 7 years ago || 2/21/2019 || 4.4 K View

  • Hide

Q.) An array is defined to be a Magic array if the sum of the primes in the array is equal to the first element of the array. If there are no primes in the array, the first element must be 0. So {21, 3, 7, 9, 11 4, 6} is a Magic array because 3, 7, 11 are theprimes in the array and they sum to 21 which is the first element of the array. {13, 4, 4, 4, 4} is also a Magic array because the sum of the primes is 13 which is also the first element. Other Magic arrays are {10, 5, 5}, {0, 6, 8, 20} and {3}. {8, 5, -5, 5, 3} is not a Magic array because the sum of the primes is 5+5+3 = 13. Note that -5 is not a prime because prime numbers are positive. Write a function named isMagicArraythat returns 1 if its integer array argument is a Magic array. Otherwise it returns 0.If you are writing in Java or C#, the function signature isint isMagicArray (int[ ])
a)If you are writing in C or C++, the function signature isint isMagicArray (int a[ ], int len) where len is the number of elements in the array.
You may assume that a function named isPrimeexists that returns 1 if its int argument is a prime, otherwise it returns 0. You do not have to write this function ! You are allowed to use it.

So, the code for the problem is provided below:

​
using System;

namespace MagicArray
{
    class Program
    {
        //Function to check whether the array is Margic arrray
        int isMagicArray(int[] arr, int l)
        {
            Console.WriteLine(l);
            int sumPrime = 0;

            int sumnotP = 0;

            Console.WriteLine("The value of l is: {0}", l);
            for (int i = 1; i < l; i++)
            {
                int s = arr[i];

                Console.WriteLine("The number to be checked is {0}", s);

                if (isPrime(s, l) == 1)
                {
                    sumPrime = sumPrime + s;
                    Console.WriteLine("The number {0} is prime", s);
                }

                if (isPrime(s, l) == 0)
                {
                    Console.WriteLine("The number {0} is not prime", s);
                    sumnotP = sumnotP + s;
                }


            }

            if (arr[0] == sumPrime)
            {
                Console.WriteLine("The sumPrime is {0}", sumPrime);
                return 1;
            }
            else
            {
                Console.WriteLine("The notsumP is {0}", sumnotP);
                return 0;
            }
        }

        //To check whether the array is prime or not......
        int isPrime(int s, int l)
        {
            int ctr = 0;


            for (int i = 1; i <= s; i++)
            {
                if ((s % i) == 0)
                {
                    ctr++;
                    if (ctr == 3)
                    {
                        break;
                    }

                }
            }

            if (ctr == 2 && s != 1)
            {
                return 1;
            }
            else
            {
                return 0;

            }

        }


        static void Main(string[] args)
        {
            int[] arr = new int[10];

            Console.WriteLine("how many numbers do you wanna enter:");
            int l = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Enter the numbers:");

            for (int i = 0; i < l; i++)
            {
                arr[i] = Convert.ToInt32(Console.ReadLine());
            }

            Program p = new Program();

            //calling the magic array function.............. 
            int k = p.isMagicArray(arr, l); 

            if (k == 1)
            {
                Console.WriteLine("The array is magic array");

            }
            else
            {
                Console.WriteLine("The array is not magic array");
            }

            Console.ReadKey();
        }
    }
}

​

The output for the problem is provided below:

So this is how I have solved the problem of checking whether the given array is magic array. But you can also do it in any other way. Hope this will be helpful for new bies in C# and this kind of problems are even asked in the interviews.

If you have different solution for the problem comment it down below:

Also Read: Biggest Issues in IT today

  • 0
  • 0
  • 0
    • Facebook
    • Twitter
    • Google +
    • LinkedIn

About author

Binod  Bhandari

Binod Bhandari

Life is not just about thrieving but all about living.

Reset Your Password
Enter your email address that you used to register. We'll send you an email with your username and a link to reset your password.

Quick Survey