Avance Zone

Technical Experts Zone Blogs

c sharp Function examples

This program helps in understanding the c sharp Function bascis

using System;
 
class function1
{
//function Definition
 
public static int fun(int a)
{
a=a*a;
return a;
}
 
public static void Main()
{
string st;
int i,res;
Console.WriteLine("Enter number to find factorial");
st=Console.ReadLine();
i=Int32.Parse(st);
//calling a Method
res=fun(i);
Console.WriteLine("The Result is "+res);
}
}

c sharp array examples

This program helps in understanding the c sharp array

using System;
public class test
{
static void Main()
{
int s1=0;
int[] a=new int[3];
 
foreach(int i in a)
{
string s;
s=Console.ReadLine();
a[i]=Int32.Parse(s);
s1=s1+a[i];
}
Console.WriteLine(s1);
}
}

The jagged array in c sharp

This program helps in understanding the c sharp jagged array

 
using System;
class jagged
{
public static void Main()
{
//create the jagged array
int [][] scores=new int[2][]{new int[]{2,3,4,5},new int[]{1,'\0',5}};
 
//scores.length refers to main index
 
for(int i=0;i<scores.Length;i++)
{
//scores[i].length refers to sub index
 
    for(int j=0;j<scores[i].Length;j++)
        {
        Console.Write("{0}\t",scores[i][j]);
        }
        Console.WriteLine();
 
    }
}
}

Assigning string and integer i n multiple array

This program helps in understanding the c sharp Multidimensional array

 
using System;
 
class ar2
{
public static void Main()
{
int[ , ] num1=new int[2,2];
int[ , ] num2=new int[2,2];
int[ , ] num3=new int[2,2];
 
int i,j;
string st;
 
Console.WriteLine("Enter the First Array");
 
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
st=Console.ReadLine();
num1[i,j]=Int32.Parse(st);
}
}
Console.WriteLine("Enter the Second Array");
 
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
st=Console.ReadLine();
num2[i,j]=Int32.Parse(st);
}
}
 
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
num3[i,j]=num1[i,j]+num2[i,j];
}
}
Console.WriteLine("The Added Matrix is as Follows");
 
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
Console.Write(num3[i,j]);
Console.Write("\t");
}
Console.Write("\n");
}
}
}

Assigning integer and string in an array

This program helps in understanding the c sharp array basics

using System;
 
class ar1
{
static void Main()
{
int i,s=0;
string st;
 
int [ ] arr=new int[5];
 
Console.WriteLine("Enter the Values for the Array");
 
for(i=0;i&lt;5;i++)
{
st=Console.ReadLine();
arr[i]=Int32.Parse(st);
s=s+arr[i];
}
Console.WriteLine("The Sum of the given Numbers are "+s);
}
}

Printing String in C Sharp with Example

This program helps in understanding the c sharp console basics

class kar
{
        public static void Main()
        {
                System.Console.WriteLine("Hai Welcome to C Sharp Program");
        }
}
|