Friday, 4 July 2014

Write C# code to produce the output shown below:

x value        y value        expression                 result
10                       5              x=y+3                     x=8
10                       5              x=y-2                      x=3
10                       5              x=y*5                     x=25
10                       5              x=x/y                      x=2
10                       5              x=x%y                    x=0

Solution:

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

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


int x=10;

int y=5;

Console.WriteLine("Result:");

Console.WriteLine("x value\t\ty value\t\tExpressions\tResult");

Console.WriteLine("{0,-8}\t{1,-8}\tx=y+3 \t x={2,-8}",x,y,y + 3);
Console.WriteLine("{0,-8}\t{1,-8}\tx=y-2 \t x={2,-8}", x, y, y-2);
Console.WriteLine("{0,-8}\t{1,-8}\tx=y*5 \t x={2,-8}", x, y, y*5);
Console.WriteLine("{0,-8}\t{1,-8}\tx=x/y \t x={2,-8}", x, y, (float)x/y);
Console.WriteLine("{0,-8}\t{1,-8}\tx=x%y \t x={2,-8}", x, y, x%y);


Console.ReadLine();
 
   }
  }
}

Write C# code to prompt a user to input his/her name and then the output will be shown as an example below:

Hello GEORGE!
Solution:

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

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

  string name;
  Console.Write("Please enter your name:");
  name = Console.ReadLine();
 
  Console.WriteLine("Hello {0}!", name);
  Console.ReadLine();
 
         }
   }
}

Write C# code to declare two integer variables, one float variable, and one string variable and assign 10, 12.5, and "C# programming" to them respectively. Then display their values on the screen.

Solution:

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

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

int x;
float y;
string s;
x = 10;
y = 12.5f;
s = "C# programming";
Console.WriteLine(x);
Console.WriteLine(y);
Console.WriteLine(s);
Console.ReadLine();
 
}
}
}

Write C# code to display the asterisk pattern as shown below:

*****
*****
*****
*****
*****
Solution:

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

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

   Console.WriteLine("*****");
   Console.WriteLine("*****");
   Console.WriteLine("*****");
   Console.WriteLine("*****");
   Console.WriteLine("*****");
   Console.ReadLine();
 
        }
    }
}

Write C# code to declare a variable to store the age of a person. Then the output of the program is as an example shown below:

You are 20 years old.
Solution:
using System;
­using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Csharp_exercises
{
class Program
{
static void Main(string[] args)
{
int age = 20;// declaring variable and assign 20 to it.
Console.WriteLine("You are {0} years old.",age);
Console.ReadLine();
}
}

}

UNDERSTANDING PARAMETER

using System;
 
namespace Understanding_Parameter
{
  class Program
   {
     // function with parameter
     public static int power(int num1)
      {
        int result;
        result = num1 * num1;
        return result;
      }
 
     static void Main(string[] args)
      {
        int pow;
        // passing arguement as parameter
        pow = Program.power(5);
        Console.Write("\nPower = {0}", pow);
        Console.ReadLine();
      }
   }
}

Wednesday, 22 January 2014

Auto-Refresh using Javascript

<html>
<head>
<script type="text/JavaScript">
<!--
function timedRefresh(timeoutPeriod) {

        setTimeout("location.reload(true);",timeoutPeriod);
}
//   -->
</script>
</head>
<body onload="JavaScript:timedRefresh(5000);">
<p>This page will refresh every 5 seconds. This is because we're using the 'onload' event to call our function. We are passing in the value '5000', which equals 5 seconds.</p>
<p>But hey, try not to annoy your users too much with unnecessary page refreshes every few seconds!</p>
</body>
</html>