0% found this document useful (0 votes)
40 views17 pages

Basic C# Program Examples

The document contains 17 code examples showing different ways to write programs in C# including: printing "hello", using command line arguments, taking keyboard input, using multiple main methods, multi-file programs, swapping numbers, initializing variables, finding Armstrong, sum of digits, Fibonacci series, palindrome numbers, prime numbers, reversing numbers, and printing various patterns.

Uploaded by

Abhishek Shukla
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)
40 views17 pages

Basic C# Program Examples

The document contains 17 code examples showing different ways to write programs in C# including: printing "hello", using command line arguments, taking keyboard input, using multiple main methods, multi-file programs, swapping numbers, initializing variables, finding Armstrong, sum of digits, Fibonacci series, palindrome numbers, prime numbers, reversing numbers, and printing various patterns.

Uploaded by

Abhishek Shukla
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

1:WAP to print hello in C#

using System;
class myclass
{
public static void Main()
{
[Link]("hello");
}
}

2:WAP for command line argument


GAURAV KUMAR SHUKLA

Page 1

using System;

class test
{
public static void Main(String []ar)

{
[Link]("welcome");
[Link](" "+ar[0]);
[Link](" "+ar[1]);
}
}

3:WAP for taking input through the keyboard


GAURAV KUMAR SHUKLA

Page 2

using System;
class test
{
public static void Main()
{
[Link]("Enter your name");
string name=[Link]();
[Link]("Hello"+name);
}
}

4:WAP for using multiple main in a single program


GAURAV KUMAR SHUKLA

Page 3

using System;
class first
{
public static void Main()
{
[Link]("This is first main");
}

}
class second
{
public static void Main()
{
[Link]("this is second main");
}
}

5:Multi-file program
using System;
GAURAV KUMAR SHUKLA

Page 4

class first
{
Public void disp()
{
[Link]("Enter your name");
string str=[Link]();
[Link]("Welcome"+str);
}
}
using System;
class second
{
public static void Main()
{
first t1=new first();
[Link]();
[Link]("thank you");
}
}

6:WAP for swapping numbers


using System;
class swap
GAURAV KUMAR SHUKLA

Page 5

public static void Main()


{
int a=15;
int b=10;
[Link]("The value of a before swapping ="+a);
[Link]("the value of b before swapping="+b);
a=a+b;
b=a-b;
a=a-b;
[Link]("after swapping the value of a="+a);
[Link]("after swapping the value of b="+b);
}

7:Initialization of variables ion C#


using System;
class first
{
GAURAV KUMAR SHUKLA

Page 6

public static void Main()


{
test t1=new test();
t1.b=1;
test t2=new test();
t2.b=2;
test.a=4;
[Link]();
[Link]();
}
}
class test
{
public static int a;
public int b;
public void show()
{
[Link]("value of a={0} value of b={1}",a,b);
}
}

8:WAP to find a Armstrong number


using System;
class arm
{
public static void Main()
GAURAV KUMAR SHUKLA

Page 7

{
int n,n1,rem,num=0;
[Link]("enter the number");
n=[Link]([Link]());
n1=n;
while(n1!=0)

{
rem=n1%10;
num+=rem*rem*rem;
n1/=10;

}
if(num==n)
[Link]("Number Is armstrong number");
else
[Link]("number is not armsrtong");
}
}

9:WAP to find sum of digits of a number


using System;
class digit
{
public static void Main()
{
GAURAV KUMAR SHUKLA

Page 8

[Link]("Enter the number");


int number=[Link]([Link]());
int sum = 0;
int input = number;
while (input != 0)
{
int lastdigit = input % 10;
sum += lastdigit;
input /= 10;
}

[Link]("Sum of digit is"+sum);


}
}

10:WAP to print Fibonacci series


using System;
class fab
{
public static void Main()
{
int k,r;
GAURAV KUMAR SHUKLA

Page 9

int i=0,j=1,f;

[Link]("Enter the range");


r=[Link]([Link]());
for(k=2;k<r;k++)
{
f=i+j;
i=j;
j=f;
[Link](" "+j);

}
}
}

11:WAP to find palindrome number


using System;
class pal
{
public static void Main()
{
int n,rev=0,rem,temp;
[Link]("Enter the number");
GAURAV KUMAR SHUKLA

Page 10

n=[Link]([Link]());
temp=n;
while(temp!=0)
{
rem=temp%10;
rev=rev*10+rem;
temp= temp/10;

}
if(rev==n)
[Link]("palindrome number");
else
[Link]("not palindrome");

}
}

12:WAP to find a prime number


using System;
class prime
{
public static void Main()
{
int n,i,flag=0;
[Link]("enter the positive number");
n=[Link]([Link]());
GAURAV KUMAR SHUKLA

Page 11

for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
flag=1;
break;
}
}
if(flag==0)
[Link]("Prime Number");
else
[Link]("Not a prime number");
}
}

13:WAP to find reverse of a given number


using System;
class rev
{
public static void Main()
{
int num,r,rev=0;
[Link]("Enter the number");
num=[Link]([Link]());
while(num>0)
GAURAV KUMAR SHUKLA

Page 12

{
r=num%10;
rev=rev*10+r;
num=num/10;
}
[Link]("reverse of number"+rev);
}
}

14:WAP print the pattern


using System;
class pat1
{
public static void Main()
{
int i,j;

for(i=1;i<=4;++i)
{
for(j=1;j<=i;++j)
GAURAV KUMAR SHUKLA

Page 13

{
[Link]("*");
}
[Link]("\n");
}

}
}

15:WAP to print the pattern


using System;
class pat2
{
public static void Main()
{
int i,space,k=0;

for(i=1;i<=4;++i)
{
for(space=1;space<=4-i;++space)
{
GAURAV KUMAR SHUKLA

Page 14

[Link](" ");
}
while(k!=2*i-1)
{
[Link]("* ");
++k;
}
k=0;
[Link]("\n");
}

}}

16:WAP to print pattern


using System;
class pat3
{
public static void Main()
{
int i,space,k=0;

for(i=1;i<=4;++i)
{
for(space=1;space<=4-i;++space)
{
[Link](" ");
GAURAV KUMAR SHUKLA

Page 15

}
while(k!=2*i-1)
{
[Link]("* ");
++k;
}
k=0;
[Link]("\n");
}

}
}

17:WAP to pattern
using System;
class pat5
{
public static void Main()
{
int i,j;
j=1;
for(i=1;i<=4;i++)
{
for(int c=4-i;c>=1;c--)
[Link](" ");
for(j=1;j<=i;j++)
[Link](j+"");
GAURAV KUMAR SHUKLA

Page 16

for(j=i-1;j>=1;j--)
[Link](j+"");

[Link]("\n");

}
[Link]();
}
}

GAURAV KUMAR SHUKLA

Page 17

You might also like