Metod Kullanarak Klavyeden girilen sayının alttan ve üstten en yakın asal sayıları bulmak

H

HZ.CİN

Cin Terbiyecisi
Thread Starter
Ultra Member
Joined
Dec 22, 2019
Messages
387
Reaction score
73
Location
Cehennem
Metod Kullanarak Klavyeden girilen sayının alttan ve üstten en yakın asal sayıları bulmak

C#:
using System;

class Program
{
    static void Main()
    {

        int GirilenSayi;

        Console.Write(" Bir sayi giriniz : ");
        GirilenSayi = Convert.ToInt32(Console.ReadLine());


        Console.WriteLine("Önceki Sayı {0} dır.", OncekiAsalSayi(GirilenSayi));


        Console.WriteLine("Sonraki Sayı {0} dır.", SonrakiAsalSayi(GirilenSayi));

    }

    static int OncekiAsalSayi(int GirilenSayi)
    {
        int b, c, son = 0, d;
        for (b = GirilenSayi - 1; b < GirilenSayi; b--)
        {
            d = 1;
            for (c = 2; c < b; c++)
            {

                if (b % c == 0)
                {
                    d = 0;

                    break;
                }
            }

            if (d == 1)
            {
                son = b;
                b = GirilenSayi + 1;
            }
        }
        return son;
    }

    static int SonrakiAsalSayi(int GirilenSayi)
    {
        int b, c, son = 0, d;
        for (b = GirilenSayi + 1; b > GirilenSayi; b++)
        {
            d = 1;
            for (c = 2; c < b; c++)
            {

                if (b % c == 0)
                {
                    d = 0;

                    break;
                }
            }

            if (d == 1)
            {
                son = b;
                b = 0;
            }
        }
                return son;
    }
}
 
SPAM IS FORBIDDEN!
  • SPAMMERS ARE BANNED FROM THE FORUM AND CANNOT USE ANY OF THE CHEATS
  • For example: thanks, thx, very good, asdqwe, working, ty and so on!
  • For example: Writing the same message over and over. thanks, thx and so on!
  • Copying and copying someone else's message is prohibited.
  • It is forbidden to send messages to increase the number of comments on threads that you have no knowledge of.
  • Write your own opinion when commenting!
  • If you see spam message, please let us know with the REPORT button!

Theme editor

Top Bottom