C Programming

C++ Programming

JAVA Programming

Android Language

Android-logo.png

.Net Programming

net-logo-b55c217213ae49991589ed43d7ed58a7.png

c# Programming

photo_1389636992_quiz_image_temp.png

Asp .Net

asplogo-square.png

PHP Programming

58481791cef1014c0b5e4994.png

Search Your Topic

config

This topic provides a step-by-step introduction to building, debugging, and publishing a simple .NET Core console application using C# in Visual Studio 2017. Visual Studio 2017 provides a full-featured development environment for building .NET Core applications. As long as the application doesn't have platform-specific dependencies, the application can run on any platform that .NET Core targets and on any system that has .NET Core installed.

A simple Hello World application

Begin by creating a simple "Hello World" console application. Follow these steps:

  1. Launch Visual Studio 2017. Select File > New > Project from the menu bar. In the New Project* dialog, select the Visual C# node followed by the .NET Core node. Then select the Console App (.NET Core) project template. In the Name text box, type "HelloWorld". Select the OK button.

    New Project dialog with Console App selected

  2. Visual Studio uses the template to create your project. The C# Console Application template for .NET Core automatically defines a class, Program, with a single method, Main, that takes a String array as an argument. Main is the application entry point, the method that's called automatically by the runtime when it launches the application. Any command-line arguments supplied when the application is launched are available in the args array.

    Visual Studio and the new HelloWorld project

    The template creates a simple "Hello World" application. It calls the Console.WriteLine(String) method to display the literal string "Hello World!" in the console window. By selecting the HelloWorld button with the green arrow on the toolbar, you can run the program in Debug mode. If you do, the console window is visible for only a brief time interval before it closes. This occurs because the Mainmethod terminates and the application ends as soon as the single statement in the Main method executes.

  3. To cause the application to pause before it closes the console window, add the following code immediately after the call to the Console.WriteLine(String) method:

    C#
    Console.Write("Press any key to continue...");
    Console.ReadKey(true);
    

    This code prompts the user to press any key and then pauses the program until a key is pressed.

  4. On the menu bar, select Build > Build Solution. This compiles your program into an intermediate language (IL) that's converted into binary code by a just-in-time (JIT) compiler.

  5. Run the program by selecting the HelloWorld button with the green arrow on the toolbar.

    Console window showing Hello World Press any key to continue

  6. Press any key to close the console window.

Enhancing the Hello World application

Enhance your application to prompt the user for their name and display it along with the date and time. To modify and test the program, do the following:

  1. Enter the following C# code in the code window immediately after the opening bracket that follows the static void Main(string[] args) line and before the first closing bracket:

    C#
    Console.WriteLine("\nWhat is your name? ");
    var name = Console.ReadLine();
    var date = DateTime.Now;
    Console.WriteLine($"\nHello, {name}, on {date:d} at {date:t}!");
    Console.Write("\nPress any key to exit...");
    Console.ReadKey(true);
    

    This code replaces the existing Console.WriteLine, Console.Write, and Console.ReadKey statements.

    Visual Studio Program c-sharp file with updated Main method

    This code displays "What is your name?" in the console window and waits until the user enters a string followed by the Enter key. It stores this string into a variable named name. It also retrieves the value of the DateTime.Now property, which contains the current local time, and assigns it to a variable named date. Finally, it uses an interpolated string to display these values in the console window.

  2. Compile the program by choosing Build > Build Solution.

  3. Run the program in Debug mode in Visual Studio by selecting the green arrow on the toolbar, pressing F5, or choosing the Debug > Start Debugging menu item. Respond to the prompt by entering a name and pressing the Enter key.

    Console window with modified program output

  4. Press any key to close the console window.

Wednesday, 25 April 2018 10:33

Introduction C# Language and the .NET Framework

Written by

C# is an elegant and type-safe object-oriented language that enables developers to build a variety of secure and robust applications that run on the .NET Framework. You can use C# to create Windows client applications, XML Web services, distributed components, client-server applications, database applications, and much, much more. Visual C# provides an advanced code editor, convenient user interface designers, integrated debugger, and many other tools to make it easier to develop applications based on the C# language and the .NET

     Note

The Visual C# documentation assumes that you have an understanding of basic programming concepts. If you are a complete beginner, you might want to explore Visual C# Express, which is available on the Web. You can also take advantage of books and Web resources about C# to learn practical programming skills.

C# Language

C# syntax is highly expressive, yet it is also simple and easy to learn. The curly-brace syntax of C# will be instantly recognizable to anyone familiar with C, C++ or Java. Developers who know any of these languages are typically able to begin to work productively in C# within a very short time. C# syntax simplifies many of the complexities of C++ and provides powerful features such as nullable value types, 

As an object-oriented language, C# supports the concepts of encapsulation, inheritance, and polymorphism. All variables and methods, including the Main method, the application's entry point, are encapsulated within class definitions. A class may inherit directly from one parent class, but it may implement any number of interfaces. Methods that override virtual methods in a parent class require the override keyword as a way to avoid accidental redefinition. In C#, a struct is like a lightweight class; it is a stack-allocated type that can implement interfaces but does not support inheritance.

In addition to these basic object-oriented principles, C# makes it easy to develop software components through several innovative language constructs, including the following:

  • Encapsulated method signatures called delegates, which enable type-safe event notifications.

  • Properties, which serve as accessors for private member variables.

  • Attributes, which provide declarative metadata about types at run time.

  • Inline XML documentation comments.

  • Language-Integrated Query (LINQ) which provides built-in query capabilities across a variety of data sources.

If you have to interact with other Windows software such as COM objects or native Win32 DLLs, you can do this in C# through a process called "Interop." Interop enables C# programs to do almost anything that a native C++ application can do. C# even supports pointers and the concept of "unsafe" code for those cases in which direct memory access is absolutely critical.

The C# build process is simple compared to C and C++ and more flexible than in Java. There are no separate header files, and no requirement that methods and types be declared in a particular order. A C# source file may define any number of classes, structs, interfaces, and events.

.NET Framework Platform Architecture

C# programs run on the .NET Framework, an integral component of Windows that includes a virtual execution system called the common language runtime (CLR) and a unified set of class libraries. The CLR is the commercial implementation by Microsoft of the common language infrastructure (CLI), an international standard that is the basis for creating execution and development environments in which languages and libraries work together seamlessly.

Source code written in C# is compiled into an intermediate language (IL) that conforms to the CLI specification. The IL code and resources, such as bitmaps and strings, are stored on disk in an executable file called an assembly, typically with an extension of .exe or .dll. An assembly contains a manifest that provides information about the assembly's types, version, culture, and security requirements.

When the C# program is executed, the assembly is loaded into the CLR, which might take various actions based on the information in the manifest. Then, if the security requirements are met, the CLR performs just in time (JIT) compilation to convert the IL code to native machine instructions. The CLR also provides other services related to automatic garbage collection, exception handling, and resource management. Code that is executed by the CLR is sometimes referred to as "managed code," in contrast to "unmanaged code" which is compiled into native machine language that targets a specific system. The following diagram illustrates the compile-time and run-time relationships of C# source code files, the .NET Framework class libraries, assemblies, and the CLR.

From C# source code to machine execution

Language interoperability is a key feature of the .NET Framework. Because the IL code produced by the C# compiler conforms to the Common Type Specification (CTS), IL code generated from C# can interact with code that was generated from the .NET versions of Visual Basic, Visual C++, or any of more than 20 other CTS-compliant languages. A single assembly may contain multiple modules written in different .NET languages, and the types can reference each other just as if they were written in the same language.

In addition to the run time services, the .NET Framework also includes an extensive library of over 4000 classes organized into namespaces that provide a wide variety of useful functionality for everything from file input and output to string manipulation to XML parsing, to Windows Forms controls. The typical C# application uses the .NET Framework class library extensively to handle common "plumbing" chores.

Wednesday, 25 April 2018 10:23

C#

Written by
Friday, 02 February 2018 05:04

HTML Tables

Written by

Using Tables

  • A table is defined using the <table> element, and contains a number of table cells ( <td>, for “table data” ) which are organized into table rows ( <tr>). The markup (HTML code) for a table is always based on rows, never columns.
  • Table cells which act as column headers or row headers should use the <th> (table header) element.
  • Table cells can be merged using the colspan and rowspan attributes.
  • Tables can be broken into sections using the following elements:
    • <thead> — Table header
    • <tbody> — Table body
    • <tfoot> — Table footer
  • A caption can be added to a table using the <caption> element.
  • You can use <col> and <colgroup> to define table columns for styling. However, there are a number of limitations with this practice.

Table Code Sample: Simple Table

<table>
 <tr>
  <th>Name</th>
  <th>Favorite Color</th>
 </tr>
 <tr>
  <td>Bob</td>
  <td>Yellow</td>
 </tr>
 <tr>
  <td>Michelle</td>
  <td>Purple</td>
 </tr>
</table>
Name Favorite Color
Bob Yellow
Michelle Purple

Table Code Sample: Complex Table

<table>
 <caption>A complex table</caption>
 <thead>
  <tr>
   <th colspan="3">Invoice #123456789</th>
   <th>14 January 2025
  </tr>
  <tr>
   <td colspan="2">
    <strong>Pay to:</strong><br>
    Acme Billing Co.<br>
    123 Main St.<br>
    Cityville, NA 12345
   </td>
   <td colspan="2">
    <strong>Customer:</strong><br>
    John Smith<br>
    321 Willow Way<br>
    Southeast Northwestershire, MA 54321
   </td>
  </tr>
 </thead>
 <tbody>
  <tr>
   <th>Name / Description</th>
   <th>Qty.</th>
   <th>@</th>
   <th>Cost</th>
  </tr>
  <tr>
   <td>Paperclips</td>
   <td>1000</td>
   <td>0.01</td>
   <td>10.00</td>
  </tr>
  <tr>
   <td>Staples (box)</td>
   <td>100</td>
   <td>1.00</td>
   <td>100.00</td>
  </tr>
 </tbody>
 <tfoot>
  <tr>
   <th colspan="3">Subtotal</th>
   <td> 110.00</td>
  </tr>
  <tr>
   <th colspan="2">Tax</th>
   <td> 8% </td>
   <td>8.80</td>
  </tr>
  <tr>
   <th colspan="3">Grand Total</th>
   <td>$ 118.80</td>
  </tr>
 </tfoot>
</table>
A complex table
Invoice #123456789 14 January 2025
Pay to:
Acme Billing Co.
123 Main St.
Cityville, NA 12345
Customer:
John Smith
321 Willow Way
Southeast Northwestershire, MA 54321
Name / Description Qty. @ Cost
Paperclips 1000 0.01 10.00
Staples (box) 100 1.00 100.00
Subtotal 110.00
Tax 8% 8.80
Grand Total $ 118.80

About Table-Based Layout

It was common in the early days of the web to use tables as a layout device. Before the advent of modern standards-based browsers, this was the easiest way to make sure that page elements were arranged properly on the screen.

This design pattern is now considered very bad. It is bad for the user experience, bad for SEO, and bad for developers who have to maintain pages.

However, that doesn’t mean you should avoid tables — tables should be used whenever you need to present information in a tabular format.

 




Friday, 02 February 2018 04:37

HTML

Written by

Beginners HTML Tutorials

Our HTML tutorials teach you how to create a personal website or site for your business, without forcing you to learn unnecessary theory. We’ll get you building your new website in minutes, not hours.

Our most popular beginners tutorials include:-

  • HTML tables
  • Links
  • Forms
  • Fonts
  • Images

Intermediate & Advanced Tutorials

We’ve plenty of topics for intermediate and advanced HTML learners, too:-

  • CSS tutorials, to make your HTML page look beautiful
  • Javascript for beginners
  • HTML5: What’s New?
  • Semantic Markup
  • Logical tags

 

Thursday, 21 September 2017 05:56

C -for-Program

Written by

Print the first five numbers starting from I together with their squares.

#include<stdio.h>

#include<conio.h>

Void main ( )

{

       Int  I;

       Clrscr ( );

       For (i=l; <=5; i++)

       Printf (“\n  Number:  %5d  it’ s  Square:  %5d”, I, i*i);

      getch();

}

OUTPUT:

Number:  1  it’ s  Square:  1

Number:  2  it’ s  Square:  4

Number:  3  it’ s  Square:  9

Number:  4  it’ s  Square:  16

Number:  5  it’ s  Square:  25      

#include<stdio.h>

#include<conio.h>

Valid main ( )

{

        Int  I;

        Clrscr ( ) ;

        For ( i=l; i<=15; i= i+l)

        Printf (“%5d”,i);

        getche ( );

}

OUTPUT:

1 2 3 4 5 6 7 8 9 10 11 12 13 14  15

Write a program to display even numbers from 0 to 14.  Declare the initial counter value before the for loop statement.

#include<stdio.h>

#include<conio.h>

Void main ( )

{

        Int  i=o ;

        Clrscr ( );

        For (i<=15;)

        {

                  Printf (“% 5 d “ , i) ;

                  i+=2;

        }

}

OUTPUT:

O 2 4 6 7 8 10 12 14

Write a program to count numbers between 1 to 100 not divisible by 2, 3, and 5.

#include<stdio.h>

#include<conio.h>

Void main ( )

{

     Int  x, c=0;

     Clrscr ( );

     Printf (“\n  Numbers  from  1  to  100  not  divisible  by  2, 3  &  5 \n \n”);

     For (x=0  ;x<=100 ;x ++)

     {

            If (x%2 !=0   &&  x%3!=0  && x%5!=0)

            {

                Printf (%”%d\t” ,x),

                C++;

            }

     }

     Printf (“\nTotal numbers  :  %d”, c );

}

OUTPUT:

Numbers  from  1  to  100  not  divisible  by 2, 3  &  5

1    7    11    13    17    19    23    29    31    37     37

41   43    47    49    53     59    61    67    71     73

77    79    83    89   91    97

Total  Numbers  :  26

Write a program to display the numbers in increasing and decreasing order using the infinite for loop.    

#include<stdio.h>

#include<conio.h>           

Void main ( )

{

       Int  n, a, b;

       Clrscr ( );

       Printf (“Enter  a  number  :”);

       Scanf (“, &);

       A=b=n;

       Printf (“ (++)  ( - - ) \n”);

       Printf (“============”);

       For (;  ;  (a++, b - - ) )

       {

             Printf (“\n%d\t%d”, a, b);

             If (b==0)

             Break;

       }

}

OUTPUT:

Enter  a  number  :  5

(++)         ( - - )

===========

5    5

6    4

7    3

8    2

9    1

10  0   

create an infinite for loop.  Check each value of the for loop.  If the value is even display it otherwise continue with iterations.  Print Even numbers from 1 to 21.  Use break statement to terminate the program.

#include<stdio.h>

#include<conio.h>

Void main ( )

{

       Int  i=l;

       Clrscr ( );

       Printf (“\n \t \t  Table  of  Even  numbers  from  1  to  20”);

       Printf (“\n \t \t  ===== == ==== ======= ==== = == == \n”);

       For (;  ;)

       {

           If (i==21)

           Break;

           else  if (i%2==0)

           {

                 Printf (“%d\t”, i);

                 I++;

                 Continue;

           }

           else

           {

                I++;

                Continue;

           }

    }

    getche ( );

}

OUTPUT:

Table  of  Even  numbers  from  1  to  20

====  ==  ====  =======  ===  =  ==  ==

2  4  6  8  10  12  14  16  18  20   

Calculate the sum of the first five numbers and their squares. Display their results.

Void main ( )

{

        Int  I,  sum=0, sqsum=0;

        Clrscr ( );

        For (i=l; i<-5; ++)

        {

               Sum+-I;

               Sqsum+=i*I;

               Printf (“\n  number:  %5d  it’ s  Square  :  %8d”, I, i*i);

       }

       Printf (“\n===================================”);

       Printf (“\n  The  sum  of  the  five  numbers (1  t0  5)  : -%6d”, sum);

       Printf (“\n  The  sum  of  their  Squares: -%9d”,  sqsum);

       getche ( );

}

OUTPUT:

Number:             1                 it’ s  Square:             1

Number:             2                 it’ s  Square:             4

Number:             3                 it’ s  Square:             9

Number:             4                 it’ s  Square:            16

Number:             5                 it’ s  Square:            25

=========================================

The  sum  of  the  five  numbers (1  t0  5) : -  15

The  sum  of  their  Squares: -  55       

 Write a program to find the number in between 7 and 100 which is exactly divisible by 4 and

If divided by 5 and 6 remainders obtained should be 4.

#include<stdio.h>

#include<conio.h>

#  include  <process . h>

Void main ( )

{

       Int  x;

       Clrscr ( );

       For (x=7;x<100;x++)

       {

              If (x%4==0  &&  x%5==4  &&  x%6==4)

              {

                     Printf (“\n  Number  :  %d”, x);

              }

      }

     getche ( );

}

OUTPUT:

Number  :  64   

Write a program to find the sum of the following series.

/*  1.  1+2+3+ ..n  */

/*  2.  12+22+32+..n2  */

#include<stdio.h>

#include<conio.h>

Void main ( )

{

       Int  sum=0, ssum-0, I, j;

       Clrscr ( );

       Printf (“Enter  Number  :”);

       Scanf (“%d”,  &j);

       Clrscr ( );

       Printf (“  Numbers:”);

       For (i=1; i<=j; i++)

       Printf (“%5d”, i);

       Printf (“\n \nSquares:”);

       For (i=1; i<=j; i++)

       {

            Printf (“%5d”, i*i);

            Sum=sum+I;

            Sum=ssum+i*i;

       }

       Printf (“\n \nSum  of  Numbers  from  1  to  %d  :%d”, j, sum);

       Printf (“\nSum  of  Squares  of  1  to  %d  Numbers  :%d”, j, ssum);

}

OUTPUT:

Enter  Number:  5

Numbers:  1  2  3  4  5

Squares:  1  4  9  16  25

Sum  of  Numbers  from  1  to  5:  15

Sum  of  Squares  of  1  to  5  Numbers:  55 

Write a program to find the perfect squares from 1 to 500.

#include<stdio.h>

#include<conio.h>

# include  <math . h>

Void maid ( )

{

      Int  I, count, x;

      Float  c;

      Clrscr ( );

      Printf (“\n \n”);

      Printf (“  Perfect  squares  from  1  to  500 \n”);

      For (i=1; <=500; i++)

  {

      C=sqrt (i);

       X=floor (c);  /*  For  rounding  up  floor ( )  is  used.  */

       {

              Printf (“\t%5d”, i);

              Coun ++;

      }

  }

  Printf (“\n \n  Total  Perfect  Squares  =5d\n”, conut);

  getch ( );

}

OUTPUT:

1       4      9      16      25   36   49   64   81   121   144

169   196   225    256    289   324    361    400   441

484                             

Total  Perfect  Squares  =  22

Write a program to detect the largest number out of five numbers and display it.

#include<stdio.h>

#include<conio.h>

# include<process . h>

Void main ( )

Exit (0);

{

       Int  a, b, c, e, sum=0, I;

       Clrscr ( );

       Printf (“\nEnter  Five  numbers  :”);

       Scanf (“%d  %d  %d  %d  %d”, &a, &b, &c, &d, &e);

       For (i=sum;  i<=sum; I - -)

       {

             If (i==a  ||  i==b  ||  i==c  ||  i==d  ||  i==e)

             {

                   Printf (“The  Largest  Number  :  %d”, i);

                   Exit (0);

             }

      }

}

OUTPUT:

Enter  Five  number  :  5  2  3  7  3 

The  Largest  Number  :  7        

Write a program to detect the smallest number our of five numbers and display it.

#include<stdio.h>

#include<conio.h>

Void main ( )

{

      Int  a, b, c, d, e, sum=0, I;

      Clrscr ( );

      Printf (“\nEnter  Five  numbers  :”);

      Scanf (“%d  %d  %d  %d  %d”, &a, &b, &c, &d, &e);

      Sum=a+b+c+d+e;

      For (i=l;  i<=sum; i++)

      {

            If (i==a  ||  i==b  ||  i==c  ||  i==d  ||  i==e)

            {

            Printf (“The  Smallest  Number  :  %d”, i)

            Exit (0);

            }

      }

}

OUTPUT:

Enter  Five  numbers  :  5  2  3  7  3

The Smallest  Number:  2          

Write a program to print the five entered numbers in the ascending order.

#include<stdio.h>

#include<conio.h>

Void main ( )

{

      Int  a, b, c, d, e, sum-0, I;

      Clrscr ( );

      Printf (“\nEnter  Five  numbers  :”);

      Scanf (“%d  %d  %d  %d  %d” &a, &b, &c, &d, &e);

      Printf (“\n  Numbers  in  ascending  order  :”);

      Sum=a+b+c+d+e;

      For (i=l;  i<=sum; i++)

  {

     If (i==a  ||  i==b  ||  i==c  ||  i==d  ||  i==e)

     {

             Printf (“%3d”, i);

     }

  }

}

OUTPUT:

Enter  Five  numbers  :  5  8  7  4  1

Numbers  in  ascending  order  :  1  4  5  7  8  

Perform multiplication of two integers by using the negative sign.

#include<stdio.h>

#include<conio.h>

Void main ( )

{

      Int  a, b, c, d=0;

      Clrscr ( );

      Printf (“\n  Enter  two  numbers  :”);

      Scanf (“%d  %d”,  &a, &b);

      For (c=l; c<-b; c++)

      D= (d) – ( - a);

      Printf (“Multiplication  of  %d  *  %d  :%d”, a, b, d);

      getche ( );

}

OUTPUT:

Enter  two  numbers  :  5  5

Multiplication  of  5  *  5  :  25   

 Calculate the sum and average of five subjects.

#include<stdio.h>

#include<conio.h>

Void main ( )

{

      Int  a, b, c, d, e, sum=0, I;

      Float  avg;

      Clrscr ( );

      Printf (“\nEnter  The  Marks  of  Five  Subjects”);

      For (i=l; i<=5; i++)

      {

           Printf (“\n [%d]  Student:”, i);

           If (scanf (“%d  %d  %d  %d  %d”, &a, &b, &c, &d, &e) ==5)

           {

                 Sum=a+b+c+d+e;

                 Avg=sum/5;

                 Printf (“\n  Total  Marks  of  Student [%d]  %d”, I, sum);

                 Printf (“\n  Average  Marks  of  Student [%d]  %f\n”, I, avg);

           }

           else

           {

                Clrscr ( );

                Printf (“\n  Type  Mismatcch”);

           }

     }

}

OUTPUT:

Enter  The  Marks  of  Five  Subjects

[1] Student:  58  52  52  56  78

      Total  Marks  of  Student [1]  296

      Average  Marks  of  Student [1]  59.000000

[2] Student:             

Write a program to find perfect cubes up to a given number.

/* 1, 8, 27, 64 are perfect cubes of 1, 2, 3 and 4 */.

#include<stdio.h>

#include<conio.h>

#  include<math . h>

Void main ( )

{

      Int  I,  j,  k;

      Clrscr ( );

      Printf (“Enter  a  Number  :”);

      Scanf (“%d”, &k);

      For (i=l; i<k; i++)

      {

            For (j=l; j<=I; j++)

            {

                 If (i==pow (j,3) )

                 Printf (“\nNumber  :  %d  &   it’ s  Cube  :%d:, j, i);

            }

     }

}

OUTPUT:

Enter  a  Number  :  100

Number  :  1  &  it’ s  Cube  :  1

Number  :  2  &  it’ s  Cube  :  8

Number  :  3  &  it’ s  Cube  :  27

Number  :  4  &  it’ s  Cube  :  64

Write a program to display the stars as shown below.

*

*  *

*  *  *

*  *  *  *

*  *  *  *  *

#include<stdio.h>

#include<conio.h>

Void main ( )

{

       Int  x, I, j;

       Clrscr ( );

       Printf (“How  many  lines  stars  (*)  should  be  printed  ?  :”);

       Scanf (“%d”, &x);

       For (i=l; i<=x; i++)

       {

              For (j=l; j<=I; j++)

              {

                     Printf (“*”);

              }

              Printf (“\n”);

       }

}

OUTPUT:

How  many  lines  stars  (*)  should  be  printed  ?  :  5

*

*  *

*  *  *

*  *  *  *

*  *  *  *  *

Write a program to generate the pattern of numbers as given under.

6  5  4  3  2  1  0 

5  4  3  2  1  0

4  3  2  1  0

3  2  1  0 

2  1  0 

1  0 

0

#include<stdio.h>

#include<conio.h>

Void main ( )

{

      Int  I, c=0;

      Clrscr ( );

      Printf (“Enter  a  Number  :”);

      Scanf (“%d, &i);

      For (;i>=0i - -)

      {

            C=I;

            Printf (“\n”);

            For (;  ;)

            {

                   Printf (“%3d”, c);

                   If (c==0)

                   Break;

                   C- -;

            }

      }

}

OUTPUT:

Enter  a  number:  6

6  5  4  3  2  1  0 

5  4  3  2  1  0

4  3  2  1  0

3  2  1  0 

2  1  0 

1  0 

Write a program to display the series of numbers as given below.

1

1  2

1  2  3

1  2  3  4 

4  3  2  1

3  2  1

2  1 

1

#include<stdio.h>

#include<conio.h>

Void main ( )

{

      Int  I, j, x; 

      Printf (“\nEnter  Value  of  x  :”);

      Scanf (“%d”,&x);

      Clrscr ( );

      For (j=l; j<=x; j++)

      {

             For (i=l; i<=j; i++)

             Printf (“%3d”, i);

             Printf (“\n”);

     }

     Printf (“\n”);

     For (j=x; j>=l; I - -)

     {

            For (i=j; i>=l; I- -)

            Printf (“%3d”, i);

            Printf (“\n”);

     }

OUTPUT:

1

1  2

1  2  3

1  2  3  4 

4  3  2  1

3  2  1

2  1 

1        

Write a program to display the series of numbers as given below.

1

1  2

1  2  3

1  2  3  4 

4  3  2  1

3  2  1

2  1 

1

#include<stdio.h>

#include<conio.h>

Void main ( )

{

     Int  I, j, x;

     Printf (“\nEnter  Value  of  x  :”);

     Scanf (“%d”, &x);

     Clrscr ( );

     For (j-l; j<=x; j++)

     {

           For (i=j; i>=l; I - -)

           {

                 Printf (“%3d”, i);

           }

           Printf (“\n”);

     }

}

OUTPUT:

Enter  Value  of  x  :  4

1

1  2

1  2  3

1  2  3  4 

4  3  2  1

3  2  1

2  1 

1

Write a program to generate the pyramid structure using numerical.

#include<stdio.h>

#include<conio.h>

Void main ( )

{

      Int  k, I, j, x, p=34;

      Printf (“\n  Enter  A  number  :”);

      Scanf (“%d”, &x);

      Clrscr ( );

      For (j=0; j <=x; j++)

      {

             Gotoxy(p, j+l);

             /*  position  cursor  on  screen  (x  cordinate, y  cordinate)  */

            For (i=0 – j; I <=j; i++)

            Printf (“%3d”, abs ( I ) );

            P=p-3;

     }

}

OUTPUT:

Enter  a  number  :  3

            0

        1  0  1

    2  1  0  1  2

3  2  1  0  1  2  3    

 

Tuesday, 19 September 2017 05:11

nested loops in C

Written by

C programming allows to use one loop inside another loop. The following section shows a few examples to illustrate the concept.

Syntax

The syntax for a nested for loop statement in C is as follows −

for ( init; condition; increment ) {

   for ( init; condition; increment ) {
      statement(s);
   }
	
   statement(s);
}

The syntax for a nested while loop statement in C programming language is as follows −

while(condition) {

   while(condition) {
      statement(s);
   }
	
   statement(s);
}

The syntax for a nested do...while loop statement in C programming language is as follows −

do {

   statement(s);
	
   do {
      statement(s);
   }while( condition );

}while( condition );

A final note on loop nesting is that you can put any type of loop inside any other type of loop. For example, a 'for' loop can be inside a 'while' loop or vice versa.

Example

The following program uses a nested for loop to find the prime numbers from 2 to 100 −

#include <stdio.h>
 
int main () {

   /* local variable definition */
   int i, j;
   
   for(i = 2; i<100; i++) {

      for(j = 2; j <= (i/j); j++) 
      if(!(i%j)) break; // if factor found, not prime
      if(j > (i/j)) printf("%d is prime", i);
   }
 
   return 0;
}

When the above code is compiled and executed, it produces the following result −

2 is prime
3 is prime
5 is prime
7 is prime
11 is prime
13 is prime
17 is prime
19 is prime
23 is prime
29 is prime
31 is prime
37 is prime
41 is prime
43 is prime
47 is prime
53 is prime
59 is prime
61 is prime
67 is prime
71 is prime
73 is prime
79 is prime
83 is prime
89 is prime
97 is prime
Tuesday, 19 September 2017 05:09

do...while loop in C

Written by

Unlike for and while loops, which test the loop condition at the top of the loop, the do...while loop in C programming checks its condition at the bottom of the loop.

do...while loop is similar to a while loop, except the fact that it is guaranteed to execute at least one time.

Syntax

The syntax of a do...while loop in C programming language is −

do {
   statement(s);
} while( condition );

Notice that the conditional expression appears at the end of the loop, so the statement(s) in the loop executes once before the condition is tested.

If the condition is true, the flow of control jumps back up to do, and the statement(s) in the loop executes again. This process repeats until the given condition becomes false.

Flow Diagram

do...while loop in C

Example

#include <stdio.h>
 
int main () {

   /* local variable definition */
   int a = 10;

   /* do loop execution */
   do {
      printf("value of a: %d\n", a);
      a = a + 1;
   }while( a < 20 );
 
   return 0;
}

When the above code is compiled and executed, it produces the following result −

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
Tuesday, 19 September 2017 05:07

for loop in C

Written by

for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

Syntax

The syntax of a for loop in C programming language is −

for ( init; condition; increment ) {
   statement(s);
}

Here is the flow of control in a 'for' loop −

  • The init step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears.

  • Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and the flow of control jumps to the next statement just after the 'for' loop.

  • After the body of the 'for' loop executes, the flow of control jumps back up to the increment statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition.

  • The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, the 'for' loop terminates.

Flow Diagram

for loop in C

Example

#include <stdio.h>
 
int main () {

   int a;
	
   /* for loop execution */
   for( a = 10; a < 20; a = a + 1 ){
      printf("value of a: %d\n", a);
   }
 
   return 0;
}

When the above code is compiled and executed, it produces the following result −

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
Tuesday, 19 September 2017 04:56

while loop in C

Written by

while loop in C programming repeatedly executes a target statement as long as a given condition is true.

Syntax

The syntax of a while loop in C programming language is −

while(condition) {
   statement(s);
}

Here, statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any nonzero value. The loop iterates while the condition is true.

When the condition becomes false, the program control passes to the line immediately following the loop.

Flow Diagram

while loop in C

Here, the key point to note is that a while loop might not execute at all. When the condition is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed.

Example

#include <stdio.h>
 
int main () {

   /* local variable definition */
   int a = 10;

   /* while loop execution */
   while( a < 20 ) {
      printf("value of a: %d\n", a);
      a++;
   }
 
   return 0;
}

When the above code is compiled and executed, it produces the following result −

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
Page 1 of 19