Pages

Showing posts with label For loop examples code. Show all posts
Showing posts with label For loop examples code. Show all posts

Write A PROGRAMME TO FIND THE FECTORIAL NUM OF THE GIVEN INPUT.(USING FOR LOOP)

Solution:

Pseudo Code:

  1. Initialize variable "num" to 1
  2. Initialize  variable "factorial" to 1
  3. Print message on console to ask user for a number to know its factorial
  4. Read user's input and convert to Integer and store that input into variable "num" that is necessary for factorial calculation 
  5. Start While loop till variable "num" value is grater then 1 (because initial value would be the user's input number)
  6. In each while loop  add the product of   "factorial" and "number" into "factorial"
  7. In each while after adding the factorial value , reduce the variable "num" by 1 so that once variable "num" value would reach to 1 then while loop will end. 
  8. Print the variable "factorial" on console to show the calculated factorial..

Code:

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

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            int num, factorial = 1;
            Console.WriteLine("please enter the num  to whom u want to know the factorial");
            num = Convert.ToInt32(Console.ReadLine());
            while (num > 1)
            {
                factorial = factorial * num;

                num = num - 1;
            }

            Console.WriteLine("the factorial of the num is {0}", factorial);
        }

    }
}


OUTPUT:


WRITE A PROGRAME TO CALCULATE AND PRNT THE TOTAL SECTION in a CHAPTER:

INPUT:

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

namespace Chapter_calculator
{
    class Program
    {
        static void Main(string[] args)
        {
            int chapter, section;
            Console.WriteLine("Enter The Chapter ");
            chapter = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter The Section");
            section = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("chapter no");
            for (int i = 1; i <= chapter; i++)
            {
                Console.WriteLine(" {0}", i);
                for (int j = 1; j <= section; j++)
                {
                    Console.WriteLine(" {0}.{1}", i, j);
                }
            }
        }
    }
}


OUTPUT:


WRITE A PROGRAM TO PRINT A TABLE OF ANY NUMBER (USING FOR LOOP)

INPUT:

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

namespace TABLE_PRINT
{
    class Program
    {
        static void Main(string[] args)
        {
            int num;
            Console.WriteLine("Write the table num");
            num = Convert.ToInt32(Console.ReadLine());
            for (int i = 1; i <= 10; i++)
            {
                Console.WriteLine("{0}*{1}={2}", num, i, i * num);
            }

        }
    }
}

OUTPUT:


WRITE A PROGRAMME ((USING FOR LOOP)) TO CALCULATE EVEN AND ODD NUMBER BETWEEN GIVEN RANGE INPUT FROM THE USRE AND ALSO FIND THEIR SUM.

Input:


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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int num1, num2, num, t_even=0, t_odd=0;
            Console.WriteLine("enter range of initial value");
            num1 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("enter range of final value");
            num2 = Convert.ToInt32(Console.ReadLine());
            for (int i = num1; i <= num2; i++)
            {
                num = i % 2;
                if (num == 0)
                {
                    Console.WriteLine("number is even = {0}", i);
                    t_even = i+ t_even;

                }
                else if (num == 1)
                {
                    Console.WriteLine("nubers are odd = {0}", i);
                    t_odd = t_odd+i;

                }
            }

            {
                Console.WriteLine("even numbers are\n{0}\nodd num are\n{1}", t_even, t_odd);
            }

        }

    }
}

Output: