Monday, December 2, 2013

P4: Write a C Sharp program to display records in text boxes of windows Form through Microsoft SQL Server 2008's database by connecting via SQL Server Authentication.

Assumptions:

Server Name: MSSQLSERVER2008
Database: project
Table: login

Data in Table:
id
username
password
1
pabitra
dangol

A SQL Server user must be made before executing this program having:
Username: pabitra
Password: dangol

Form Design

















Source Code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data; //used for database connection
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient; //used for database connection

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            string connectionString = null;
            SqlConnection sqlCnn;
            SqlCommand sqlCmd;
            SqlDataAdapter adapter = new SqlDataAdapter();
            DataSet ds = new DataSet();
            int i = 0;
            string sql = null;

            //connecting SQL Server 2008 using SQL Server Authentication Mode via C Sharp
            connectionString = "Data Source=.\\MSSQLSERVER2008; Initial Catalog=project; User ID=pabitra; Password=dangol";
       
            sql = "Select * from login";

            sqlCnn = new SqlConnection(connectionString);
            try
            {
                sqlCnn.Open();
                sqlCmd = new SqlCommand(sql, sqlCnn);
                adapter.SelectCommand = sqlCmd;
                adapter.Fill(ds);
           
                for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
                {
                    txtid.Text = Convert.ToString(ds.Tables[0].Rows[i].ItemArray[0]);
                    txtusername.Text = Convert.ToString(ds.Tables[0].Rows[i].ItemArray[1]);
                    txtpassword.Text = Convert.ToString(ds.Tables[0].Rows[i].ItemArray[2]);
                    MessageBox.Show(ds.Tables[0].Rows[i].ItemArray[0] + " - " + ds.Tables[0].Rows[i].ItemArray[1] + " - " + ds.Tables[0].Rows[i].ItemArray[2]);
                }
                adapter.Dispose();
                sqlCmd.Dispose();
                sqlCnn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Can not open connection ! ");
            }

        }
    }
}

OUTPUT


Sunday, December 1, 2013

P4: Write a C Sharp program to display records in text boxes of windows Form through Microsoft SQL Server 2008's database by connecting via Windows Authentication.

Assumptions:

Server Name: MSSQLSERVER2008
Database: project
Table: login

Data in Table:
id
username
password
1
pabitra
dangol

Form Design

















Source Code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data; //used for database connection
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient; //used for database connection

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            string connectionString = null;
            SqlConnection sqlCnn;
            SqlCommand sqlCmd;
            SqlDataAdapter adapter = new SqlDataAdapter();
            DataSet ds = new DataSet();
            int i = 0;
            string sql = null;

            //connecting SQL Server 2008 using Windows Authentication Mode via C Sharp
            connectionString = "Data Source=.\\MSSQLSERVER2008; Initial Catalog=project;Integrated Security=SSPI";
         
            sql = "Select * from login";

            sqlCnn = new SqlConnection(connectionString);
            try
            {
                sqlCnn.Open();
                sqlCmd = new SqlCommand(sql, sqlCnn);
                adapter.SelectCommand = sqlCmd;
                adapter.Fill(ds);
             
                for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
                {
                    txtid.Text = Convert.ToString(ds.Tables[0].Rows[i].ItemArray[0]);
                    txtusername.Text = Convert.ToString(ds.Tables[0].Rows[i].ItemArray[1]);
                    txtpassword.Text = Convert.ToString(ds.Tables[0].Rows[i].ItemArray[2]);
                    MessageBox.Show(ds.Tables[0].Rows[i].ItemArray[0] + " - " + ds.Tables[0].Rows[i].ItemArray[1] + " - " + ds.Tables[0].Rows[i].ItemArray[2]);
                }
                adapter.Dispose();
                sqlCmd.Dispose();
                sqlCnn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Can not open connection ! ");
            }

        }
    }
}

OUTPUT



Friday, November 29, 2013

P3C4: C Sharp Program for Binary search

using System;

public class Program_Binary_Search
{
    int binary_search(int[] a, int size, int item)
    {
        int first = 0, last = size - 1, middle;
        while (first <= last)
        {
            middle = (first + last) / 2;

            if (item == a[middle])
                return middle;
            else if (item < a[middle])
                last = middle - 1;
            else
                first = middle + 1;
        }
        return -1;
    }
     
    public static void Main()
    {
     
        int[] b = new int[] {2,4,5,7,8,9,12,15};
        int size=8, item;
     
        Console.WriteLine("Enter a number to search: ");
        item =Convert.ToInt32(Console.ReadLine());


        Program_Binary_Search pbs = new Program_Binary_Search();
        int p = pbs.binary_search(b, size, item);
     
        if (p ==-1)
            Console.WriteLine("\n{0} is not present in the array", item);
        else
            Console.WriteLine("\n{0} is present in the array at index no: {1}", item, p);

        Console.WriteLine("\nPress any key to exit...");
        Console.ReadLine();
    }
}

OUTPUT

Enter a number to search:
7

7 is present in the array at index no: 3

Press any key to exit...

P3C4: C Sharp Program for Linear search or Serial search

//Source Code
using System;

public class Program_Linear_Search
{
    int linear_search(int[] a, int size, int item)
    {
        int i = 0;
        while(i<size && a[i]!=item)
            i++;

        if(i<size)
            return i; //returns index number of the item in the array
        else
            return -1; //item doesn't present in the array
    }
     
    public static void Main()
    {
     
        int[] b = new int[] {2,4,5,7,8,9,12,15};
        int size=8, item;
     
        Console.WriteLine("Enter a number to search: ");
        item =Convert.ToInt32(Console.ReadLine());


        Program_Linear_Search pls = new Program_Linear_Search();
        int p = pls.linear_search(b, size, item);
     
        if (p ==-1)
            Console.WriteLine("\n{0} is not present in the array", item);
        else
            Console.WriteLine("\n{0} is present in the array at index no: {1}", item, p);

        Console.WriteLine("\nPress any key to exit...");
        Console.ReadLine();
    }
}

OUTPUT

Enter a number to search:
5

5 is present in the array at index no: 2

Press any key to exit...

Thursday, November 28, 2013

C++: Find out any 5 possible output from the program below.

// C++ Source code
#include<stdlib.h>
#include<iostream.h>
#include<conio.h>

void main()
{
randomize();
   char city[][10]={"Kathmandu", "Lalitpur", "Bhaktapur", "Pokhara", "Lumbini"};
   int fly;

   int count=1;
   while(count<=5)
   {
    for(int i=0; i<3; i++)
    {
    fly=random(2) + 1;
       cout<<city[fly]<<" : ";
    }
      cout<<endl;
      count++;
   }
   getch();
}

OUTPUT

Bhaktapur : Lalitpur : Lalitpur :
Lalitpur : Lalitpur : Lalitpur :
Bhaktapur : Bhaktapur : Bhaktapur :
Bhaktapur : Bhaktapur : Bhaktapur :
Bhaktapur : Lalitpur : Bhaktapur :

C++: An example (source code) of class with member functions.

// C++ Source code
#include<iostream.h>
#include<stdio.h>
#include<conio.h>

class flight
{
long flightCode;
   char description[25];

   public:
    void addInfo()
      {
       cin>>flightCode;
         gets(description);
      }

      void showInfo()
      {
         cout<<endl<<"*** OUTPUT ***"<<endl;
       cout<<flightCode<<":"<<description<<endl;
      }
};

void main()
{
flight f;
   f.addInfo();
   f.showInfo();

   getch();
}

OUTPUT

1234.5678
Welcome to my C++ Source Codes Page

*** OUTPUT ***
1234:Welcome to my C++ Source Codes Page

Sunday, October 27, 2013

C Sharp Programming: Hospital Management Project

This Hospital Management Project is based on A Level Text Book. Slightly modified on database in order to show relationship.

1. Create a new project in Visual Studio 2008 with name "Hospital_Management". Choose "Windows Forms Application" while creating new project.

2. To create a new data source:
  • Click Data menu > Add New Data Source...
  • Then new Dialog box appears
  • Select "database" icon and click next button
  • Click on "new connection" button
  • Write "Hospital" on Database textbox and click on create button
  • Better to create database file inside project folder
  • Now just click on Next ... Next and Finish button
  • Finally you will see "HospitalDataSet.xsd" file in the solution explorer window of the project.

Friday, October 25, 2013

C Programming: Write a C Program to display size of all variable types using sizeof() function.

Source code

#include<conio.h>

void main()
{
int i;
   char c;
   float f;
   double d;

   printf("The size of integer is %d", sizeof(i));
   printf("\nThe size of character is %d", sizeof(c));
   printf("\nThe size of float is %d", sizeof(f));
   printf("\nThe size of double is %d", sizeof(d));

   getch();
}

Output

The size of integer is 4
The size of character is 1
The size of float is 4
The size of double is 8

Wednesday, October 9, 2013

C++: What do you mean by function overloading in C++? Explain with examples.

Function Overloading in C++

A function name having several definitions which are differentiated by the number or types of their arguments is called function overloading.

Example:

//Source code in C++

//C++ source code
#include<iostream.h>
#include<conio.h>

class printing
{
public:
    void print(int i)
      {
      cout<<"Printing integer: " << i << endl;
      }

      void print(double f)
      {
      cout<<"Printing float: " << f << endl;
      }

      void print(char* c)
      {
      cout<<"Printing character: " << c <<endl;
      }
};

void main()
{
   printing p;

   cout<<"*** OUTPUT ***" << endl;
   p.print(5);
   p.print(500.55);
   p.print("Hello! alevelcomputer.blogspot.com");

   getch();
}

OUTPUT

*** OUTPUT ***
Printing integer: 5
Printing float: 500.55
Printing character: Hello! alevelcomputer.blogspot.com

P2C2: Write a program in C Sharp to input an abbreviation and then output its meaning. If the abbreviation is not in the table, a suitable message should be output.

Input Table

Abbreviation
Meaning
m
metre
kg
kilogram
s
second
a
ampere
k
kelvin

//Source code in C Sharp


using System;
namespace Application_Abbreviation
{
    class Program_Abbreviation
    {
        static void Main(string[] args)
        {
            string abb;

            Console.WriteLine("Enter an abbreviation: ");
            abb = Convert.ToString(Console.ReadLine());

            switch (abb)
            {
                case "m":
                    Console.WriteLine("metre ");
                    break;

                case "kg":
                    Console.WriteLine("kilogram ");
                    break;

                case "s":
                    Console.WriteLine("second ");
                    break;

                case "a":
                    Console.WriteLine("ampere ");
                    break;

                case "k":
                    Console.WriteLine("kelvin ");
                    break;

                default:
                    Console.WriteLine("\nInvalid abbreviation");
                    break;

            }
        }
    }
}

OUTPUT

Enter an abbreviation:
kg
kilogram

Monday, October 7, 2013

CBSE2009AQ1. What is the difference between call by value and call by reference? Give an example in C++ illustrate both.

 Call by value
 Call by reference
  1. A copy of value or argument passed from calling routine to the function.
  2. The original value doesn't change by called function.
  1. The original value or argument passed from calling routine to the function.
  2. The original value can change by called function.

Coding for Call by value:

#include <iostream>
using namespace std;

// function declaration
void swap(int x, int y);


int main ()
{
   // local variable declaration:
   int a = 100;
   int b = 200;

   cout << "Before swap, value of a :" << a << endl;
   cout << "Before swap, value of b :" << b << endl;

   /* calling a function to swap the values using value*/
   swap(a, b);

   cout << "After swap, value of a :" << a << endl;
   cout << "After swap, value of b :" << b << endl;

   return 0;
}

void swap(int x, int y)
{
    int temp;
    temp = x;
    x=y;
    y=temp;
    
}

Coding for Call by reference:

#include <iostream>
using namespace std;

// function declaration
void swap(int &x, int &y);


int main ()
{
   // local variable declaration:
   int a = 100;
   int b = 200;
   cout << "Before swap, value of a :" << a << endl;
   cout << "Before swap, value of b :" << b << endl;

   /* calling a function to swap the values using variable reference.*/
   swap(a, b);

   cout << "After swap, value of a :" << a << endl;
   cout << "After swap, value of b :" << b << endl;
   return 0;
}

void swap(int &x, int &y)
{
    int temp;
    temp = x;
    x=y;
    y=temp;
    
}

Thursday, October 3, 2013

P3C4: Write a program in C Sharp to implement Linked List for adding, deleting and retrieving data items.

//Source code

using System;

class Subject
{
    public string SubjectName;
    public Subject Next;
}

class ListOfSubjects
{
    private int size;

    public ListOfSubjects()
    {
        size = 0;
        Head = null;
    }
    public int Count
    {
        get { return size; }
    }

    public Subject Head;

    public int Add(Subject NewItem)
    {
        Subject Sample = new Subject();

        Sample = NewItem;
        Sample.Next = Head;
        Head = Sample;
        return size++;
    }

    public Subject Retrieve(int Position)
    {
        Subject Current = Head;

        for (int i = 0; i < Position && Current != null; i++)
            Current = Current.Next;
        return Current;
    }

    public bool Delete()
    {
        if (Head == null)
        {
            Console.WriteLine("The list is empty");
            return false;
        }

        Subject Current;

        Current = Head.Next;
        Head.Next = Current.Next;
        size--;
        return true;
    }

    public bool Delete(int Position)
    {
        if (this.Retrieve(Position) == null)
            return false;

        this.Retrieve(Position - 1).Next = this.Retrieve(Position + 1);
        size--;
        return true;
    }

    public bool Find(Subject ItemToFind)
    {
        Subject Current = new Subject();

        if (ItemToFind == null)
            return false;

        for (Current = Head; Current != null; Current = Current.Next)
        {
            if (Current.SubjectName == ItemToFind.SubjectName)
                return true;
        }

        return false;
    }
}

class Exercise
{
    static int Main()
    {
        ListOfSubjects Subjects = new ListOfSubjects();
        Subject Sub;
        Subject SubjectToFind;

        Sub = new Subject();
        Sub.SubjectName = "Maths";
        Subjects.Add(Sub);

     
        Console.WriteLine(" === Output ===");
        Console.WriteLine("Number of Subjects: {0}", Subjects.Count);

        for (int i = 0; i < Subjects.Count; i++)
        {
            Subject s = Subjects.Retrieve(i);
            Console.WriteLine("\nSubjects Information");
            Console.WriteLine("Subject Name: {0}", Sub.SubjectName);
        }

        SubjectToFind = new Subject();
        SubjectToFind.SubjectName = "Maths";
     

        bool Found = Subjects.Find(SubjectToFind);
        if (Found == true)
            Console.WriteLine("\nItem was found\n");
        else
            Console.WriteLine("\nItem not found\n");

        Console.ReadKey();
        return 0;
    }
}

P3C3: Linked List

A linked list is a data structure which can link other data. E.g. a list of subjects, people's name etc. Linked list is a list of nodes i.e. data items. Each node consists a data part and a link part. The first node of linked list is pointed by a Header node which is also called an external node. Similarly, the last node's link part consists a NULL.

C Sharp can implement the linked list easily by using System.Collections.Generic class.

Source code

using System;
//used for linked list feature
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        LinkedList<string> linked = new LinkedList<string>();

        linked.AddFirst("Maths");
        linked.AddLast("Politics");
        linked.AddLast("Biology");
        linked.AddLast("Physics");
        linked.AddLast("Computing");

        foreach (var item in linked)
        {
            Console.WriteLine(item);
        }
        Console.ReadKey();
    }
}

OUTPUT

Maths
Politics
Biology
Physics
Computing

Monday, September 30, 2013

P2C3: Arrays

Arrays

An array stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables. A specific element in an array is accessed by an index.
All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.
Arrays in C#

//Source code for C Sharp Arrays

using System;
namespace Application_Array
{
    class Program_Array
    {
        static void Main(string[] args)
        {
            int[] n = new int[10]; /* n is an array of 10 integers */
            int i, j;


            /* initialize elements of array n */
            for (i = 0; i < 10; i++)
            {
                n[i] = i + 10;
            }

            /* output each array element's value */
            Console.WriteLine("=====OUTPUT=====");
            for (j = 0; j < 10; j++)
            {
                Console.WriteLine("Element[{0}] = {1}", j, n[j]);
            }
            Console.ReadKey();
        }
    }
}

OUTPUT

=====OUTPUT=====
Element[0] = 10
Element[1] = 11
Element[2] = 12
Element[3] = 13
Element[4] = 14
Element[5] = 15
Element[6] = 16
Element[7] = 17
Element[8] = 18
Element[9] = 19

P2C2: Write a program in C Sharp for fibonacci numbers using iteration and recursion method.

Fibonacci Series:

Fibonacci series is the sequence of the numbers which has following properties:
  1. First number is 1.
  2. Second number is 1
  3. Remaining other numbers are sum of their preceding numbers. Eg: third number = 1+1=2
  4. Fibonacci series: 1, 1, 2, 3, 5, 8, 13, ....
------------------------------------------------------------------

//Source code using iteration method
using System;

public class Program_Fibonacci_Iteration
{
   public static void Main()
    {
        int num;
        long ans;

        Console.Write("Enter any number: ");
        num = Convert.ToInt32(Console.ReadLine());

        ans = Fibo(num);
        Console.WriteLine("\n :::::::: OUTPUT :::::::::\n");
        Console.WriteLine("The " + num + " th number in fibonacci series is: \n" + ans);
       
        Console.WriteLine("\n\nPress any key to exit...");
        Console.ReadLine();
    }

   static int Fibo(int n)
   {
       int num1 = 1, num2 = 1;
       int num3 = 0;

       if (n <= 2)
           return 1;
       else
       {
           for (int i = 3; i <= n; i++)
           {
               num3 = num1 + num2;
               num1 = num2;
               num2 = num3;
           }
           return num3;
       }
   }
}

OUTPUT

Enter any number: 8

 :::::::: OUTPUT :::::::::

The 8 th number in fibonacci series is:
21


Press any key to exit...

-------------------------------------------------------------------

//Source code using recursion method
using System;

public class Program_Fibonacci_Recursion
{
   public static void Main()
    {
        int num;
        long ans;

        Console.Write("Enter any number: ");
        num = Convert.ToInt32(Console.ReadLine());

        ans = Fibo(num);
        Console.WriteLine("\n :::::::: OUTPUT :::::::::\n");
        Console.WriteLine("The " + num + " th number in fibonacci series is: \n" + ans);
     
        Console.WriteLine("\n\nPress any key to exit...");
        Console.ReadLine();
    }

   static int Fibo(int n)
   {
       if (n <= 2)
           return 1;
       else
           return (Fibo(n-1) + Fibo(n-2));
   }

}

OUTPUT

Enter any number: 8

 :::::::: OUTPUT :::::::::

The 8 th number in fibonacci series is:
21


Press any key to exit...

---------------------------------------------------------------
//Printing Fibonacci Series
using System;

public class Program_Fibonacci_Recursion
{
   public static void Main()
    {
        int n, num1, num2, num3;
        num1 = 1; //First fibonacci number
        num2 = 1; //Second fibonacci number
     
        Console.Write("Enter any number: ");
        n = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("\n\n:::::::: OUTPUT :::::::::");
     
        //To print first and second fibonacci numbers
        Console.WriteLine("Position: " + 1 + "\tValue: " + num1);
        Console.WriteLine("Position: " + 2 + "\tValue: " + num2);

       //Counting for fibonacci number starts from third, so i = 3 is assigned
        for (int i = 3; i <= n; i++)
        {
            num3 = num1 + num2;
            num1 = num2;
            num2 = num3;
            Console.WriteLine("Position: " + i + "\tValue: " + num3);
        }
           
        Console.WriteLine("\n\nPress any key to exit...");
        Console.ReadLine();
    }
}

OUTPUT

Enter any number: 12


:::::::: OUTPUT :::::::::
Position: 1     Value: 1
Position: 2     Value: 1
Position: 3     Value: 2
Position: 4     Value: 3
Position: 5     Value: 5
Position: 6     Value: 8
Position: 7     Value: 13
Position: 8     Value: 21
Position: 9     Value: 34
Position: 10    Value: 55
Position: 11    Value: 89
Position: 12    Value: 144


Press any key to exit...

P2C2: Write a program in C Sharp to calculate 2's power using recursion.

//Write a program in C Sharp to calculate 2's power using recursion
using System;

public class Program_Power_Recursion
{
   public static void Main()
    {
        int num;
        long ans;

        Console.Write("Enter any number: ");
        num = Convert.ToInt32(Console.ReadLine());

        ans = Power2(num);
        Console.WriteLine("\n :::::::: OUTPUT :::::::::\n");
        Console.WriteLine(" 2^" + num + " = " + ans);
     
        Console.WriteLine("\n\nPress any key to exit...");
        Console.ReadLine();
    }

   static long Power2(int n)
   {
       if (n == 0)
           return 1;
       else
           return (2 * Power2(n - 1));
   }
}

OUTPUT:

Enter any number: 10

 :::::::: OUTPUT :::::::::

 2^10 = 1024


Press any key to exit...

Sunday, September 29, 2013

P2C2: Write a program in C Sharp to check whether a number entered by a user is even or odd.

//Using if else statement

using System;

public class Program_Even_Odd
{
   public static void Main()
    {
        int num;

        Console.WriteLine("Enter any number: ");
        num = Convert.ToInt32(Console.ReadLine());

        if (num % 2 == 0)
            Console.WriteLine("\n{0} is an Even number", num);
        else
            Console.WriteLine("\n{0} is an Odd number", num);
                 
        Console.WriteLine("\nPress any key to exit...");
        Console.ReadLine();

    }
}

OUTPUT:

Enter any number:
100

100 is an Even number

Press any key to exit...
---------------------------------------------------------------------------------------------

//Using case statement

using System;

public class Program_Even_Odd
{
   public static void Main()
    {
        int num, ans;

        Console.WriteLine("Enter any number: ");
        num = Convert.ToInt32(Console.ReadLine());

       ans = (num % 2);

       switch (ans)
       { 
           case 0:
               Console.WriteLine("\n{0} is an Even number", num);
               break;

           case 1:
               Console.WriteLine("\n{0} is an Odd number", num);
               break;

           default:
               Console.WriteLine("Invalid Entry");
               break;
       }
                   
        Console.WriteLine("\nPress any key to exit...");
        Console.ReadLine();

    }
}

OUTPUT:

Enter any number:
55

55 is an Odd number


Press any key to exit...

P2C2: C Sharp Program - Sum of 10 consecutive numbers using while, do ... while and for loop

// Using while loop

using System;

public class Program_Sum
{
   public static void Main()
    {
        int n=0, sum=0;
     
        while (n < 10)
        {
            n = n + 1;
            sum = sum + n;
            Console.WriteLine("Number = {0} Sum = {1}", n, sum);
        }


        Console.WriteLine("\n\nSum of {0} numbers = {1}", n, sum);
     
        Console.WriteLine("\nPress any key to exit...");
        Console.ReadLine();

    }
}

OUTPUT:

Number = 1 Sum = 1
Number = 2 Sum = 3
Number = 3 Sum = 6
Number = 4 Sum = 10
Number = 5 Sum = 15
Number = 6 Sum = 21
Number = 7 Sum = 28
Number = 8 Sum = 36
Number = 9 Sum = 45
Number = 10 Sum = 55


Sum of 10 numbers = 55

Press any key to exit...


//Using do ... while loop


using System;

public class Program_Sum
{
   public static void Main()
    {
        int n=0, sum=0;

        do
        {
            n = n + 1;
            sum = sum + n;
            Console.WriteLine("Number = {0} Sum = {1}", n, sum);
        } while (n < 10);


        Console.WriteLine("\n\nSum of {0} numbers = {1}", n, sum);
     
        Console.WriteLine("\nPress any key to exit...");
        Console.ReadLine();

    }
}

OUTPUT:

Number = 1 Sum = 1
Number = 2 Sum = 3
Number = 3 Sum = 6
Number = 4 Sum = 10
Number = 5 Sum = 15
Number = 6 Sum = 21
Number = 7 Sum = 28
Number = 8 Sum = 36
Number = 9 Sum = 45
Number = 10 Sum = 55


Sum of 10 numbers = 55

Press any key to exit...


//Using for loop

using System;

public class Program_Sum
{
   public static void Main()
    {
        int sum=0;

        for(int n=1; n<=10; n++)
        {
            sum = sum + n;
            Console.WriteLine("N = {0} \t Sum = {1}", n, sum);
        }

                 
        Console.WriteLine("\nPress any key to exit...");
        Console.ReadLine();

    }
}

OUTPUT:

N = 1    Sum = 1
N = 2    Sum = 3
N = 3    Sum = 6
N = 4    Sum = 10
N = 5    Sum = 15
N = 6    Sum = 21
N = 7    Sum = 28
N = 8    Sum = 36
N = 9    Sum = 45
N = 10   Sum = 55

Press any key to exit...

P2C2: C Sharp Program - Multiplication of two numbers using function

//Multiplication of two numbers using function
using System;

public class MultiplicationProgram
{
   static long Multiply (int n1, int n2)
    {
        return (n1 * n2);
    }

    public static void Main()
    {
        int num1, num2;
        long mul=1;

        Console.WriteLine("Enter First Number:");
        num1=Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Enter Second Number:");
        num2 = Convert.ToInt32(Console.ReadLine());

        mul = Multiply(num1, num2);
     
        Console.WriteLine("\nMultiplication = {0} * {1} = {2}",num1, num2, mul);
        Console.WriteLine("\nPress any key to exit...");
        Console.ReadLine();

    }
}

OUTPUT:

Enter First Number:
10
Enter Second Number:
20

Multiplication = 10 * 20 = 200

Press any key to exit...

P2C2: C Sharp Program - Factorial using Recursion and Iteration

//Source Code using Recursion
using System;

public class Recursion_Factorial
{
   static long Factorial(int num)
    {
        if (num <= 1)
            return 1;
        return num * Factorial(num - 1);
    }

    public static void Main()
    {
        int num;
        long fact=1;

        Console.WriteLine("Enter a Number");
        num=Convert.ToInt32(Console.ReadLine());

        fact = Factorial(num);
     
        Console.WriteLine("Number = {0} and Factorial = {1}",num,fact);
        Console.WriteLine("\nPress any key to exit...");
        Console.ReadLine();

    }
}

OUTPUT:

Enter a Number
6
Number = 6 and Factorial = 720

Press any key to exit...

-------------------------------------------------------------------------------------

//Using Iteration Method
using System;

public class Program_Factorial_Iteration
{
   public static void Main()
    {
        int num;
        long ans;

        Console.WriteLine("Enter any number: ");
        num = Convert.ToInt32(Console.ReadLine());

        ans = factorial(num);
        Console.WriteLine("Factorial of " + num + " = " + ans);
       
        Console.WriteLine("\n\nPress any key to exit...");
        Console.ReadLine();
    }

   static long factorial(int n)
   {
       long fact;
       if (n <= 1)
           return 1;
       else
       {
           fact = 1;
           
           for (int i = n; i > 0; i--)
           {
               fact = fact * i;
           }
           return fact;
       }      
   }
}

OUTPUT:

Enter any number:
5
Factorial of 5 = 120


Press any key to exit...

Thursday, September 26, 2013

P2C5: Writing maintainable programs

Outlines

  1. Programming terminology:
    • A variable is a data item associated with a memory location of which the value can change.
    • A constant is a data item associated with a memory location of which the value can't change. If the value needs to change, it is changed once and the program is recompiled.
    • An identifier is a name that represents an element in a program such as a variable, a constant, a function, a procedure, a user-defined data type or an object.
    • A reserved word, or keyword, is one that is defined by the programming language, such as int or switch in C#.Net.
  2. Variables must usually be declared so that the computer knows how much space to allocate and the type of data to which they refer.
  3. The identifier name for a variable or constant must be unique and not a reserved word. It should be meaningful.
  4. Variables should be initialized appropriately before using them
  5. Variables have a scope within the program code:
    • a variable which is recognised everywhere in the code is said to be a "global" variable
    • a variable may be "local" to a procedure or function.
  6. Uses followings for good readability of source codes:
    • comments
    • indentation and formatting to show the control structures (i.e. if statements and loops)
    • white spaces



Sunday, September 8, 2013

P3C6: Databases

Databases - Outlines

  1. A flat file consists of a large number of records each of which comprises a number of fields.
  2. Each field has its own data type and holds a single item of data.
  3. Limitations of flat-file systems:
    • separation and isolation of data
    • duplication of data
    • data dependence
    • data inconsistencies
    • difficulty in changing applications programs
  4. Relational databases store data in tables.
  5. Normalization is a set of formal rules that must be followed after a set of table is designed.
  6. Normal forms (NF):
    • 1 NF: It doesn't have a repeated group of attributes
    • 2 NF: There are no non-key fields attributes which are dependent on only part of the primary key
    • 3NF: There are no dependencies between non-key attributes
  7. An entity - relationship (E-R) diagram shows the entities in the database and the relationships between them.
  8. A relational database gives the following advantages over flat files:
    • Data are contained in a single software application.
    • Duplication of data is minimized.
    • Data inconsistency is reduced.
    • The volume of data is reduced leading to faster searching and sorting of data.
    • Data structures remain the same even when tables are altered.
    • Existing programs don't need to be altered when a table design is changed or new tables created.
    • Queries and reports can be set up quickly.
  9. A primary key is used to uniquely identify a record or row in a table.
  10. A foreign key attribute links to a primary key in a second table.
  11. A secondary key is used to get fast access when searching on this attribute.
  12. Different categories of user can see different views of the data - only what is needed for their job.
  13. Terminals in public areas may be refused access to sensitive data, even if the user has access.
  14. A DBMS (Database Management System) is a piece of software that provides the following facilities:
    • a data definition language (DDL) that the database designer uses to define the tables of the database
    • a data dictionary that contains all design information
    • a data manipulation language (DML) for inserting, selecting, updating and deleting data records
    • backup of the database data
    • control of multi-user access to the data.
  15. The DML and DDL used by all modern database software is Structured Query Language (SQL).

P1C4: System software

System software - Outlines

  1. The purpose of operating system
    • controls the hardware
    • provides a platform for application software to run onto it
    • provides an HCI (Human - Computer Interaction)
    • manages the resources of the computer system
  2. A batch operating system controls the passage of jobs through the computer without user interaction.
  3. A real-time operating system reacts quickly enough to affect the next input or process that needs to be carried out. It operates a continuous cycle of input-processing-output.
  4. A single-user operating system enables only one user at a time to access the system.
  5. A multi-user operating system enables more than one user to access the system at the same time.
  6. A multi-tasking operating system gives the user the impression that they can carry out more than one task at the same time.
  7. A network operating system links a number of computers together and enables them to share peripherals.
  8. Applications that require batch processing include:
    • payroll
    • bank statements
    • utility bills
  9. Applications that require a real-time response include:
    • industrial control systems
    • robots
    • ticket-booking systems
  10. A form-based user interface provides boxes into which the user can type data. It provides the user with help (on-screen prompts and validation) in completing the data.
  11. A menu-based user interface provides the user with a set of options that restrict the available information. An information system for tourists and on-screen menus for digital television are easy for users to operate.
  12. A graphical user interface (GUI) provides windows, icons, menus and a pointer to enable the user to interact with the computer in complex ways.
  13. A natural language user interface enables the user to use a natural language such as English to interact with the computer. It may be spoken or typed input.
  14. A command line user interface requires the user to type commands to give specific instructions to the computer. It enables a technician to get close to the workings of the computer.
  15. Disk formatting software prepares a disk for use by the operating system.
  16. File handling software enables the user to move, copy and delete files.
  17. Hardware drivers enable successful communication between devices and the operating system.
  18. File compression software allows data to be stored in a smaller amount of storage space.
  19. Virus-checking software monitors input and stored data to ensure that it does not contain malicious software.

Wednesday, September 4, 2013

A Level Computing 9691

The blog concerns on A Level Computing 9691 paper.

The Computing Course consists 4 papers:-

  1. Computer systems, communications and software: 75 marks
  2. Practical programming techniques: 75 marks
  3. Systems software mechanisms, machine architecture, database theory, programming paradigms and integrated information systems: 90 marks
  4. Computing project: 60 marks