Write a program in C# Sharp to separate odd and even integers in separate arrays ?

  • برمجة سي شارب
  • برمجة

Write a program in C# Sharp to separate odd and even integers in separate arrays ?

                                                                                                       
Separate odd and even integers in separate arrays:                                                            
------------------------------------------------------                                                        
Input the number of elements to be stored in the array :2                                                     
Input 2 elements in the array :                                                                               
element - 0 : 3                                                                                               
element - 1 : 6                                                                                                
The Even elements are :                                                                                       
6                                                                                                       
The Odd elements are :                                                                                        
3
Explanation:
Calculating a Even Numbers:

Even Numbers between 1 to 100:

Calculating a Odd Numbers:

Odd Numbers between 1 to 100:

الأجوبة

using System;  
public class Exercise10 
{  
    public static void Main() 
 {
	int[] arr1 = new int[10];
	int[] arr2 = new int[10];
	int[] arr3 = new int[10];
    int i,j=0,k=0,n;	
       Console.Write("\n\nSeparate odd and even integers in separate arrays:\n");
       Console.Write("------------------------------------------------------\n");	
       Console.Write("Input the number of elements to be stored in the array :");
	   n = Convert.ToInt32(Console.ReadLine()); 	   
       Console.Write("Input {0} elements in the array :\n",n);
       for(i=0;i<n;i++)
            {
	      Console.Write("element - {0} : ",i);
    	  arr1[i] = Convert.ToInt32(Console.ReadLine()); 		  
	    }
    for(i=0;i<n;i++)
    {
	if (arr1[i]%2 == 0)
	{
	   arr2[j] = arr1[i];
	   j++;
	}
	else
	{
	   arr3[k] = arr1[i];
	   k++;
	}
    }
    Console.Write("\nThe Even elements are : \n");
    for(i=0;i<j;i++)
    {
	Console.Write("{0} ",arr2[i]);
    }
    Console.Write("\nThe Odd elements are :\n");
    for(i=0;i<k;i++)
    {
	Console.Write("{0} ", arr3[i]);
    }
    Console.Write("\n\n");	
   }	
 } 
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...