Advertisement
Guest User

Primalitate

a guest
May 20th, 2018
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.52 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace proiect
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         int n, i = 2, ok, invalid = 0, contor;
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.         public static int primfunctie(int a)
  21.         {
  22.             for (int i = 2; i <= Math.Sqrt(a); i++)
  23.                 if (a % i == 0) return 0;
  24.             return 1;
  25.         }
  26.  
  27.         private void label1_Click(object sender, EventArgs e)
  28.         {
  29.  
  30.         }
  31.  
  32.         private void label2_Click(object sender, EventArgs e)
  33.         {
  34.  
  35.         }
  36.  
  37.         private void button1_Click(object sender, EventArgs e)
  38.         {
  39.             if ((textBox1.Text) != "")
  40.                {
  41.                 n = Convert.ToInt32(textBox1.Text);
  42.                 if (n <= 9999 && n >= 0)
  43.                 {
  44.                     textBox6.Text = "";
  45.                     textBox6.Text = " Numarul este valid.";
  46.                 }
  47.                 else
  48.                 {
  49.                     textBox6.Text = "";
  50.                     textBox6.Text = " Numarul este invalid.";
  51.                     MessageBox.Show("Introduceti un numar pozitiv de maxim 4 cifre.");
  52.                     invalid = 1;
  53.                 }
  54.             }
  55.         }
  56.  
  57.         private void label3_Click(object sender, EventArgs e)
  58.         {
  59.  
  60.         }
  61.  
  62.         private void checkBox2_CheckedChanged(object sender, EventArgs e)
  63.         {
  64.  
  65.         }
  66.  
  67.         private void button2_Click(object sender, EventArgs e)
  68.         {
  69.             if (textBox1.Text != "")
  70.             {
  71.                 if (invalid == 0)
  72.                 {
  73.  
  74.                     n = Convert.ToInt32(textBox1.Text);
  75.                     if (primfunctie(n) == 0)
  76.                     {
  77.                         textBox4.Text = "";
  78.                         textBox4.Text = " Nu este numar prim.";
  79.                     }
  80.                     else
  81.                     {
  82.                         textBox4.Text = "";
  83.                         textBox4.Text = " Este numar prim.";
  84.  
  85.                     }
  86.  
  87.                 }
  88.                 else
  89.                 {
  90.                     textBox4.Text = "";
  91.                     textBox4.Text = " Numar invalid.";
  92.                 }
  93.             }
  94.  
  95.         }
  96.  
  97.         private void button3_Click(object sender, EventArgs e)
  98.         {
  99.             if (textBox1.Text != "") {
  100.             if (invalid == 0)
  101.             {
  102.                 if (checkBox1.CheckState == CheckState.Checked)
  103.                 {
  104.                     i = Convert.ToInt32(textBox1.Text);
  105.                     if (i < 3)
  106.                     {
  107.                         textBox2.Text = "";
  108.                         textBox2.Text = " Nu exista.";
  109.                     }
  110.                     else
  111.                     {
  112.                         textBox2.Text = "";
  113.                         ok = 1;
  114.                         while (i > 2 && ok >= 0)
  115.                         {
  116.                             i = i - 1;
  117.                             if (primfunctie(i) == 1 && ok >= 0)
  118.                             {
  119.                                 textBox2.Text = textBox2.Text + " " + Convert.ToString(i);
  120.                                 ok = ok - 1;
  121.                             }
  122.  
  123.                         }
  124.                     }
  125.                 }
  126.                 if (checkBox2.CheckState == CheckState.Checked)
  127.                 {
  128.                     i = Convert.ToInt32(textBox1.Text);
  129.                     if (i > 9967)
  130.                     {
  131.                         textBox3.Text = "";
  132.                         textBox3.Text = " Nu exista.";
  133.                     }
  134.                     else
  135.                     {
  136.                         textBox3.Text = "";
  137.                         ok = 1;
  138.                         while (i <= 9999 && ok >= 0)
  139.                         {
  140.                             i = i + 1;
  141.                             if (primfunctie(i) == 1 && ok >= 0)
  142.                             {
  143.                                 textBox3.Text = textBox3.Text + " " + Convert.ToString(i);
  144.                                 ok = ok - 1;
  145.                             }
  146.                         }
  147.  
  148.                     }
  149.                 }
  150.                 if (checkBox1.CheckState == CheckState.Unchecked && checkBox2.CheckState == CheckState.Unchecked)
  151.                     MessageBox.Show("Selectati cel putin o optiune.");
  152.  
  153.             }
  154.             else
  155.             {
  156.                 if (checkBox1.CheckState == CheckState.Checked)
  157.                 {
  158.                     textBox2.Text = "";
  159.                     textBox2.Text = " Numar invalid.";
  160.                 }
  161.                 if (checkBox2.CheckState == CheckState.Checked)
  162.                 {
  163.                     textBox3.Text = "";
  164.                     textBox3.Text = " Numar invalid.";
  165.                 }
  166.             }
  167.         }
  168.         }
  169.  
  170.         private void button4_Click(object sender, EventArgs e)
  171.         {
  172.             if (textBox1.Text != "")
  173.             {
  174.                 if (invalid == 0)
  175.                 {
  176.                     n = Convert.ToInt32(textBox1.Text);
  177.                     textBox5.Text = "";
  178.                     if (n < 2)
  179.                     {
  180.                         textBox5.Text = "";
  181.                         textBox5.Text = " Nu exista.";
  182.                         textBox7.Text = " ";
  183.                         textBox7.Text = " 0 numere.";
  184.                     }
  185.                     else
  186.                     {
  187.                         contor = 0;
  188.                         for (i = 2; i <= n; i++)
  189.                         {
  190.                             if (primfunctie(i) == 1)
  191.                             {
  192.                                 textBox5.Text = textBox5.Text + " " + Convert.ToString(i);
  193.                                 contor = contor + 1;
  194.                             }
  195.  
  196.                         }
  197.                         textBox7.Text = "";
  198.                         textBox7.Text = Convert.ToString(contor) + " numere.";
  199.                     }
  200.                 }
  201.                 else
  202.                 {
  203.                     textBox5.Text = "";
  204.                     textBox5.Text = " Numar invalid.";
  205.                 }
  206.             }
  207.         }
  208.  
  209.         private void linkLabel1_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
  210.         {
  211.             this.label1.BackColor = System.Drawing.Color.Transparent;
  212.             try
  213.             {
  214.                 VisitLink();
  215.             }
  216.             catch (Exception ex)
  217.             {
  218.                 MessageBox.Show("Nu s-a putut deschide link-ul.");
  219.             }
  220.         }
  221.  
  222.         private void VisitLink()
  223.         {
  224.             linkLabel1.LinkVisited = true;
  225.             System.Diagnostics.Process.Start("http://www.pastebin.com");
  226.         }
  227.         private void button5_Click(object sender, EventArgs e)
  228.         {
  229.             textBox1.Text = "";
  230.             textBox2.Text = "";
  231.             textBox3.Text = "";
  232.             textBox4.Text = "";
  233.             textBox5.Text = "";
  234.             textBox6.Text = "";
  235.             textBox7.Text = "";
  236.  
  237.         }
  238.  
  239.         private void Form1_Load(object sender, EventArgs e)
  240.         {
  241.  
  242.         }
  243.  
  244.         private void checkBox1_CheckedChanged(object sender, EventArgs e)
  245.         {
  246.  
  247.         }
  248.  
  249.         private void label4_Click(object sender, EventArgs e)
  250.         {
  251.  
  252.         }
  253.     }
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement