C#.Net Examples

C# Program To Find out Binary Number of the character.



//C# Programme To find out the Binary number of the programme
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace binaryCon
{
class Program
{
static void Main()
{
Console.Clear();
int rem; //for Find Its remainder for binary number....
char a=' '; int b=0;
Console.WriteLine("Please enter character to find out its binary number");
try // exception Handling....
{
a = char.Parse(Console.ReadLine());
b = a;
}
catch (FormatException e) //catch exception.....
{
Console.WriteLine("Please write a single character");
Console.WriteLine(e.Message);
Main(); // jump to begining....
}

Console.Clear();
Console.WriteLine("ASCII Code for '{0}' Is {1}",a,b); //Show ASCII Code...
Console.WriteLine();
Console.Write("Binary Code of {0} : ",a);//show Binary number of the character....
do // loop for Binary number
{
rem = b % 2;
Console.Write(rem); //write binary number (0,1)
b = b / 2;
}while(b>0);
Console.WriteLine();
for (int i = 0; i <= 79; i++) { Console.Write("-"); } Console.WriteLine("\nWant to Find More Binaries type (Y) for More"); string cont = Console.ReadLine(); cont.ToUpper(); if (cont == "y") //asking for more... { Main(); } Console.ReadLine(); } } }



A program to input a number and print its reverse.



For Doing this you may run a 'for' loop. and doing mod by 10.
here is the code:


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

namespace reverse
{
class Program
{
static void Main(string[] args)
{
int a, b;
a = int.Parse(Console.ReadLine());
for(int i=a;i>0;i=i/10)
{
b = i % 10;
Console.Write(b);
}
Console.ReadLine();

}
}
}




C# Programming how to know that the number is Armstrong or not..?.




For Doing this you may run a 'for' loop. and doing mod by 10.
here is the code:


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

namespace reverse
{
class Program
{
static void Main(string[] args)
{
int num, sum, y;
Console.WriteLine("Please write the number to know")
num=int.Parse(Console.ReadLine());
for(int i=num;i>0;i=i/10)
{
y=i%10;
sum=sum+y*y*y;
}
if(sum==num)
{
Console.WriteLine("Armstrong");
}
else
{
Console.WriteLine("Not Armstrong");
}
Console.ReadLine();

}
}
}


armstrong numbers are just like - 153 = 1*1*1 = 1 , 5*5*5 = 125, 3*3*3 = 27
1+125+27=153





print the sum of number you type
Print the number one by one...



class Program
{
static void Main(string[] args)
{
int sum = 0,a,b;
a=int.Parse(Console.ReadLine());
for (int i = a; i > 0;i= i / 10)
{
b = i % 10;
sum = sum + b;
}
Console.WriteLine(sum);
Console.ReadLine();
}
}


For Example You type 123 it returns 6 as answer.. 1 + 2 + 3 = 6











A program to input a number and print its factorial.


Factorial exapmle like you type 5 for its factorial:
5*4=20*3=60*2=120*1= 120



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

namespace reverse
{
class Program
{
static void Main(string[] args)
{
int num, fact=1;
num = int.Parse(Console.ReadLine());
for(int i=num;i>=1;i--)
{
fact=fact*i;
}
Console.WriteLine("factorial is {0}",fact);
Console.ReadLine();

}
}
}

1 comment: