0% found this document useful (0 votes)
43 views

Ompilation OF Different Programs: IN Programming 1

This document contains C# code examples demonstrating basic programming concepts like program structure, decision making statements (if/else, nested if/else, switch case), looping (for, while, do-while loops), and arrays. The code samples show how to get user input, perform conditional checks, iterate loops a set number of times, and store and access values in an array.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Ompilation OF Different Programs: IN Programming 1

This document contains C# code examples demonstrating basic programming concepts like program structure, decision making statements (if/else, nested if/else, switch case), looping (for, while, do-while loops), and arrays. The code samples show how to get user input, perform conditional checks, iterate loops a set number of times, and store and access values in an array.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

COMPILATION OF DIFFERENT

PROGRAMS

IN
PROGRAMMING 1

RENZON Q. DONIEGO
BSIT-1A
Basic program structure

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Basic_Structure
{
class Program
{
static void Main (string[ ] args)

{
int number;
Console.Write("Enter a number : ");
number= Convert.ToInt32(Console.ReadLine());
Console.WriteLine("The value is : " + number);
Console.ReadKey();
}
}
}

Decision Making Statement


I. if-else statement
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace If_Else
{
class Program
{
static void Main (string[ ] args)

{
int number;
Console.Write("Enter a number : ");
number= Convert.ToInt32(Console.ReadLine());
Console.WriteLine("The number value is : "+ number);
if ( number >0 )
{
Console.WriteLine("The number you entered is NOT ZERO");
}
else
{
Console.WriteLine("The number you entered is ZERO");
}
Console.ReadKey();
}
}
}
II. Nested if-else statement
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Nested_IE
{
class Program
{
static void Main (string[ ] args)

{
int number;
Console.Write("Enter a number : ");
number= Convert.ToInt32(Console.ReadLine());
Console.WriteLine("The number value is : "+ number);
if ( number >0 )
{
Console.WriteLine("The number you entered is NOT ZERO");
if ( number % 2 = = 0 )
{
Console.WriteLine("The number you entered is EVEN");
}
else
{
Console.WriteLine("The number you entered is ODD");
}

}
else
{
Console.WriteLine("The number you entered is ZERO");
}
Console.ReadKey();
}
}
}

III. Switch-case
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Switch_Case
{
class Program
{
static void Main (string[ ] args)

{
int number;
Console.Write("Enter a number from 1-7 : ");
number= Convert.ToInt32(Console.ReadLine());
Console.WriteLine("The number value is : "+ number);
switch (number)
{
case 1: Console.WriteLine("The day is MONDAY");break;
case 2: Console.WriteLine("The day is TUESDAY");break;
case 3: Console.WriteLine("The day is WEDNESDAY");break;
case 4: Console.WriteLine("The day is THURSDAY");break;
case 5: Console.WriteLine("The day is FRIDAY");break;
default: Console.WriteLine("The day is WEEKEND");break;
}
Console.ReadKey();
}
}
}
Looping

A.for loop
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace For_Loop
{
class Program
{
static void Main (string[ ] args)

{
int number ;
for(int counter=1;counter<=3;counter++)
{
Console.Write("Enter a number : ");
number= Convert.ToInt32(Console.ReadLine());
if ( number > 0 )
{
Console.WriteLine("The number you entered is NOT ZERO");
}
else
{
Console.WriteLine("The number you entered is ZERO");
}
}
Console.ReadKey();
}
}
}

B.while loop
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace while_loop
{
class Program
{
static void Main (string[ ] args)

{
int number ;
int counter =1;
while(counter<=3)
{
Console.Write("Enter a number : ");
number= Convert.ToInt32(Console.ReadLine());
if ( number > 0 )
{
Console.WriteLine("The number you entered is NOT ZERO");
}
else
{
Console.WriteLine("The number you entered is ZERO");
}
counter++;
}
Console.ReadKey();
}
}
}

C.do-while loop
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace do_while
{
class Program
{
static void Main (string[ ] args)

{
int number ;
int counter =1;
do
{
Console.Write("Enter a number : ");
number= Convert.ToInt32(Console.ReadLine());
if ( number > 0 )
{
Console.WriteLine("The number you entered is NOT ZERO");
}
else
{
Console.WriteLine("The number you entered is ZERO");
}
counter++;
} while (counter<=3);
Console.ReadKey();
}
}
}

ARRAY
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Array
{
class Program
{
static void Main (string[ ] args)

{
int [ ] number = new int [ 3] ;
Console.Write("\n\nEnter three number : ");
number [0]= Convert.ToInt32(Console.ReadLine());
number[1]= Convert.ToInt32(Console.ReadLine());
number[2]= Convert.ToInt32(Console.ReadLine());
Console.WriteLine("The numbers you entered are : " );
Console.WriteLine("Third number : " + number[2] );
Console.WriteLine("Second number : " + number[1] );
Console.WriteLine("First number : " + number[0] );

Console.ReadKey();
}
}
}

You might also like