create a console program to create a file of numbers then store even & odd numbers of that file in two seperate files
1
Expert's answer
2013-04-26T11:27:47-0400
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace Evenandodd_numbers { class Program { static void Main(string[] args) { int []arrayofnumbers=new int[1000];//variable for numbers int []arrayofevennumbers=new int[1000];//variable for even numbers int []arrayofOddnumbers=new int[1000];//variable for odd numbers Random rand=new Random(); //fill array of random numbers for (int i = 0; i < 1000; i++) { arrayofnumbers[i] = rand.Next(1,1000); } int counteven = 0;//count for even numbers int countodd = 0;//count for odd numbers for (int i = 0; i < 1000; i++) { //check if number is even if (arrayofnumbers[i] % 2 == 0) { arrayofevennumbers[counteven] = arrayofnumbers[i];//add even numbers to array counteven++; } //check if number is odd else { arrayofOddnumbers[countodd] = arrayofnumbers[i];//add odd numbers to array countodd++; } } //save even numbers using (System.IO.StreamWriter file = new System.IO.StreamWriter("even.txt", true)) { for (int i = 0; i < counteven; i++) { file.WriteLine(arrayofevennumbers[i].ToString()); ;//save array to file } } //save Odd numbers using (System.IO.StreamWriter file = new System.IO.StreamWriter("Odd.txt", true)) { for (int i = 0; i < countodd; i++) { file.WriteLine(arrayofOddnumbers[i].ToString());//save array to file } }
Comments
Leave a comment