Showing posts with label c program examples. Show all posts
Showing posts with label c program examples. Show all posts

Friday, 25 October 2013

Finding largest two numbers from N numbers

Finding largest two numbers from N numbers:


 This C program example finds the two largest numbers from the list of 'N' numbers.

Mechanism:
In this program, we have declared the variable of:
  • Size
  • Input
  • First largest
  • Second largest
 First, we take input of size and then we enter a for loop which executes for 'size' times. This program is designed in such a way that it checks all negative and positive integers. This program uses an if else and else if structure and for loop structure structure to execute the logic of the program. This program also works on boundary lines. Time complexity of this program is O(size). Once the loop terminates, it prints the first largest and second largest value from the N numbers.

Code:

/*              Join us at
http://shapesinc.blogspot.com 
for more codes of shapes                            
*/
#include<iostream>
using namespace std;
int main()
{
cout << "Join http://shapesinc.blogspot.com for more codes of shapes\n\n";
cout<<"CAUTION:   The input should not be other than numerical values \n\n\a";
int i, num, largest = 0, seclargest = 0, size; cout << "Enter the amount of numbers you want to enter\n"; cin>>size; if (size<=0) { cout << "No values entered\n"; goto finish; } cout << "Please enter the numbers from the keyboard.\n" << endl; for (int i=0; i<size; i++ ) { cin >> num; if(i==0) largest=num; else if(i==1) { if(num>largest) { seclargest = largest; largest = num; } else seclargest=num; } else if ( num > largest ) { seclargest = largest; largest = num; } else if ( num > seclargest ) seclargest = num; } cout << "\nThe largest of the numbers entered is " << largest << endl ; cout << "\nThe second largest of the numbers entered is " << seclargest << endl ; finish: return 0; }

Final Words:
By this post, you can learn C online practically. We have tried our best to make this program understandable and optimized.

Sunday, 6 October 2013

Creating A C++ Program

How to Create a C++ program??

In this C tutorial for beginners, we are going to learn how to create C++ programs with output. After studying previous tutorials, we now have the basic and required concepts needed to create a C++ program. A C++ program is a collection of function, one of which is the function main. A function is a set of instructions designed to accomplish specific task. We’ll deal with functions in further tutorials in detail.

The syntax of the function main is:

int main()
{
    Statement 1
    Statement 2
    .
    .
    .
    Statement n

return 0;
}

This main function body contains two type of statements:

1. Declaration statement:

Declaration statements are used to declare things as    variable.

2. Executable statements:

Executable statements perform calculations, manipulate data, create output, accept input and so on. Some executable statements that we came across with are assignment, input and output statements.

 The return 0 function is necessary and is always the last statement. In C++, return is reserved keyword.
 
This main function should be saved in .cpp extension file. 

The file containing this main function or source code is called the source code file or source file.

Things stated step by step needed for creating C++ program examples:

  • Comments if needed to introduce some information about your program
  • Use preprocessor directives to include header filese.g: #include<iostream>
    using namespace std;
  • Then we introduce any global scope variables if necessary.
  • Then we introduce main body as explained above with syntax.


Now, we’ll show you  simple c program examples to explain everything that we mentioned above.

//******************************************************
// Made by shapesinc.blogspot.com
//******************************************************

#include<iostream>    //preprocessor directive
using namespace std;

int main()
{
    int a, b, c;    //declarative statement
    double x, y    //declarative statement

    a=4;        //assignment statement
    cin >> b;    //input statement
    cout << a << “ ” << b <<endl;    //output statement
    return 0;
}