Monday, 31 July 2017

Write a C program to find sum of all odd numbers between 1 to N using for loop

Write a C program to find SUM of all ODD numbers between 1 to N using for loop.

Code:

#include <stdio.h> 
int main() { 
    int counter, N, sum = 0; 
    printf("Enter a Positive Number\n");
    scanf("%d", &N); 
    for(counter = 1; counter <= N; counter++) { 
        if(counter%2 == 1) {
            sum = sum + counter; 
        } 
    }
    printf("Sum of all Odd numbers between 1 to %d is %d", N, sum);
    return 0; 
Output: 




Try it yourself.
If you have any problem you can tell me on comment box i will help you.
thanks.

Sunday, 30 July 2017

Write a C program using array that take the value by user

Write a C program using array that take the value by user.


What is Array ?

Array is a type of data structure that contains a group of elements. Typically these elements are all of the same data type, such as an integer or string. Arrays are commonly used in computer programs to organize data so that a related set of values

Saturday, 29 July 2017

Write a C program that take the value by programmer by using array

Write a C program that take the value by programmer by using array:

What Is Array:

Array is a group of same data types.

Importance Of Array In C Programming:

Consider a scenario where in you want to store a hundred integer numbers, entered by user, in an effort to discover the average of them. To program this scenario you have got two ways:
 1) Define one hundred variable of integer type and at ultimate perform the common operation.
 2) Have a single integer array to store all values.

Which solution is better as according to you? Obviously the second one solution, it is convenient to store same data types in one single variable and later get access them using array index.

Array declaration:

data_type array-name [array_size]; 

Example:

float mark[5];

How to access the element of array?

You can use array  index to get right of entry to any element saved in array. Index starts with 0, because of this array_name[0] might be used to get right of entry to first element in an array.

Code:


#include<stdio.h>
#include<conio.h>
void main()
{
int a[5]={55,34,12,54,32},i;
for (i=0;i<5;i++)
{
printf("\nMarks of roll no %d is %d ",i+1,a[i]);
}
getch();
clrscr();
}


Output:
















Try it yourself.
If you have any problem you can tell me on comment box i will help you.
thanks.



Friday, 28 July 2017

Basic Fundamentals Of Programming Algorithms

Basic Fundamentals Of Programming Algorithms:

Introduction to Algorithms:

Algorithms for making things will often be divided into sections;

The parts/components/ingredients (inputs) required to accomplish the task actions/steps/methods (processing) to produce the required outcome (output).

For example to build a model car, the parts (inputs) are needed plus instructions on how to assemble the car (processing) and the result is the car (output).

There are two forms of Algorithm:1 -   Pseudo code2 - Flowchart

Pseudo code:


Pseudocode (which means fake code, because it’s not really programming code) specifies the steps required to accomplish the task.
Pseudocode is a type of structured English that is used to specify an algorithm.
 Pseudocode cannot be compiled nor executed, and there are no real formatting or syntax rules.
Example of Pseudocode:

Ready and open subscriber file
 Get a record
Do while more records
              If current subscriber subscription count is > 3 then
 Output the record
              Get another record
end

Advantages of Pseudocode:

• Reduced complexity.
• Increased flexibility.
• Ease of understanding. 

Why is Pseudocode Necessary ?

• The programming process is a complicated one.
• You must first understand the program specifications.
• Then you need to organize your thoughts and create the program.
• You must break the main tasks that must be accomplished into smaller ones in order to be able to eventually write fully developed code.
• Writing Pseudocode will save you time later during the construction & testing phase of a program's development. 

How to Write Pseudocode Statements ?

There are six basic operations:
1.  A computer can receive information
Read (information from a file)
Get (information from the keyboard).

2.  A computer can put out information
Write (information to a file)
Display (information to the screen).

3.  A computer can perform arithmetic
Use actual mathematical symbols or the words for the symbols
Example:
Add number to total
Total = total + number
+, -, *, /
Calculate, Compute also used.

4.  A computer can assign a value to a piece of data as
3 cases:


i. To give data an initial value

Initialize, Set.


ii. To assign a value as a result of some processing
„=‟

*x=5+y.

iii. To keep a piece of information for later use
Save, Store.

5. A computer can compare two piece of information and select one of two alternative actions:

IF condition THEN
                     some action
ELSE
                     alternative action
ENDIF

6. A computer can repeat a group of actions:

WHILE condition (is true)
some action
ENDWHILE

FOR a number of times
some action
ENDFOR

Data Dictionaries:

• The pseudo code by itself doesn't provide enough information to be able to write program code.
• Data Dictionaries are used to describe the data used in the Pseudo Code.
• The standard data types used in Pseudo Code are Integer, Double, String, Char and Boolean. 



Example 1:
Program Specification:

Write a program that obtains two integer numbers from the user. It will print out the sum of those numbers.

Pseudocode:

Prompt the user to enter the first integer Prompt the user to enter a second integer Compute the sum of the two user inputs Display an output prompt that explains the answer as the sum Display the result.

Example 2:
Find the average of any three numbers.

We might usually specify the procedure of solving this problem as “add the three numbers and divide by three”. Here, Read (or Ask) and Write (or Say) are implied. However in an algorithm, these steps have to be made explicit. Thus a possible algorithm is:

Step 1    Start
Step 2    Read values of X,Y,Z
Step 3    S=X+Y+Z
Step 4    A=S/3
Step 5    Write value of A
Step 6    Stop

Example 3:
Finding square and cube.

Step 1    Start
Step 2    Read value of N
Step 3    S=N*N
Step 4    C=S*N
Step 5    Write values of S,C
Step 6    Stop

Example 4:
Finding biggest of two numbers.

Step 1    Start
Step 2    Read A,B
Step 3    If A>B, then BIG=A, otherwise BIG=B
Step 4    Write BIG
Step 5    Stop

Example 5:
Calculate pay.

Step 1    Start
Step 2    Input hours
Step 3    Input rate
Step 4    pay=hours*rate
Step 5    Print pay
Step 6   End

Flowchart:

• A flowchart is a graphical representation of an algorithm.
• These flowcharts play an important role in programming of a problem and are quite helpful in understanding the logic of complicated and lengthy problems.
• Once the flowchart is drawn, it becomes easy to write the program in any high level language.


A flow chart can therefore be used to:


1-Define and analyze processes
2-Build a step-by-step picture of the process for analysis, discussion, or communication
3-Define, standardize or find are as for improvement in a process.


Flowchart Symbols:

Start and end symbols:


• Represented as lozenges, oval surrounded rectangles.
• Usually containing the word "Start" or "End", or another phrase signaling the start or end of a process, such as "submit enquiry "or" receive product".

Arrows:

• Showing what's called "flow of control" in computer science.
• An arrow coming from one symbol and ending at another symbol.
• Represents that control passes to the symbol the arrow points to.


Processing steps:



• Represented as rectangles.
• Examples: “Add 1 to X"; "replace identified part" ; "save changes" or      similar.


Input/Output:

• Represented as a parallelogram.
• Examples: Get X from the user; display X.

Conditional or decision:
• Represented as a diamond.
• These typically contain a Yes/No question or True/False test.

Display Symbol:


• Indicates a process flow step where information is displayed to a person (e. g., PC user, machine operator).

Rules for Flowchart:
1. Every flow chart has a START symbol and a STOP symbol.
2. The flow of sequence is generally from the top of the page to the bottom of the page .This can vary with loops which need to flow back to an entry point.
3. Use arrow-heads on connectors where flow direction may not be obvious.
4. There is only one flow chart per page.

5. A page should have a page number and a title.
6. A flow chart on one page should not break and jump to another page.
7. A flowchart should have no more than around 15 symbols (not including START and STOP).

Advantages of Using Flowcharts:

• Communication: Flowcharts are better way of communicating the logic of a system to all concerned.

• Effective analysis: With the help of flowchart, problem can be analysed in more effective way.

• Proper documentation: Program flowcharts serve as a good program documentation, which is needed for various purposes.

• Efficient Coding: The flow charts act as a guide or blue print during the systems analysis and program development phase.

• Proper Debugging: The flowchart helps in debugging process.

• Efficient Program Maintenance: The maintenance of operating program becomes easy with the help of flowchart. It helps the programmer to put efforts more efficiently on that part.

Basic Control Structures:

1- Sequence
2- Selection
3- Loop

Sequence:
• Steps that execute in sequence are represented by symbols that follow each other top to bottom or left to right.
• Top to bottom is the standard.


Selection:
• Once the condition is evaluated, the control flows in to one of two paths.
• Once the conditional execution is finished, the flows rejoin before leaving the structure.
Loop:
• Either the processing repeats or the control leaves the structure.
• Notice that the return line joins the entry line be fore the question.

Example:

Algorithm:

Input: two numbers x and y
Output: the average of x and y
Steps:
1. input x
2. input y
3. sum = x+y
4. output sum

Flowchart:

Try it yourself.
If you have any problem you can tell me on comment box i will help you.
thanks.

Thursday, 27 July 2017

Nested Loop And Types Of Nested Loop In C Programming



Nested Loop And Types Of Nested Loop In C Programming.

Nested Loop In C:

A loop inside another loop is called a nested loop. The intensity of nested loop relies upon at the complexity of a problem. We can have any number of  of nested loops as required. Don't forget a nested loop wherein the outer loop runs n times and includes some other loop inside it. The inner loop runs m times. Then, the whole number of times the inner loop runs throughout this program execution is n*m.

We also define the "Nested Loop" as:

A nested loop is a loop inside a loop, an inner loop in the body of an outer one. How this works is that the first pass of the outer loop triggers the internal loop, which executes to final touch. Then the second pass of the outer loop triggers the internal loop once more. This repeats till the outer loop finishes. Of direction, a ruin inside either the inner or outer loop might interrupt this process.

Types of nested loop:

There are three types of Nested Loop are:

1 - Nested While Loop
2 - Nested Do-While Loop
3 - Nested For Loop

Nested While Loop:

A while loop inside another while loop known as nested while loop.

Syntax of Nested while loop.


while (condition1)
{
    statement(s);
    while (condition2)
    {
        statement(s);
        ... ... ...
    }
    ... ... ...
}

Flowchart of Nested While loop:



Example of Nested While loop:

C program to print the number pattern.

Code:

#include <stdio.h>
int main()
{
    int i=1,j;
    while (i <= 5)
    {
        j=1;
        while (j <= i )
        {
            printf("%d ",j);
            j++;
        }
        printf("\n");
        i++;
    }
    return 0;
}

Output:

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

In this program, nested whilst loop is used to print the pattern. The outermost loop runs five times and for every loop, the innermost loop runs i times that is 1 at the start, which means most effective "1" is outlined, then on the next loop it is 2 numbers printing "1 2" and so forth until five iterations of the loop executes, printing "1 2 3 four 5". This manner, the given number pattern is printed.

Nested Do-While Loop:

A do-while loop inside another do-while loop is known as nested do-while loop.


Syntax of Nested Do-While Loop.

do
{
    statement(s);
    do
    {
        statement(s);
        ... ... ...
    }while (condition2);
    ... ... ...
}while (condition1);

Flowchart of Nested While loop:



Example of Nested Do-While loop:

C program to print the given star pattern.

Code:


#include <stdio.h>
int main()
{
    int i=1,j;
    do
    {
        j=1;
        do
        {
            printf("*");
            j++;
        }while(j <= i);
        i++;
        printf("\n");
    }while(i <= 5);
    return 0;
}

Output:

*
**
***
****
*****


On this program, nested do-while loop is used to print the star pattern. The outermost loop runs five times and for every loop, the innermost loop runs i times that's 1 at the beginning, which means best one "*" is outlined, then on the next loop it's 2 printing two stars and so on until five iterations of the loop executes, printing 5 stars. This way, the given celebrity pattern is printed.

Nested For Loop:

A for loop inside another for loop is called nested for loop.

Syntax of Nested For Loop.

for (initialization; condition; increment/decrement)
{
    statement(s);
    for (initialization; condition; increment/decrement)
    {
        statement(s);
        ... ... ...
    }
    ... ... ...
}

Flowchart of Nested For loop:

Example of Nested for loop:

Write a Pattern Programs Using Nested For loop

Code:
#include<stdio.h>

#include<conio.h>

void main()
{
int n,r,c;
 for(r=3;r<=6;r++)
 {
  for(c=1;c<=r;c++)
 {
  printf("*");
 }
  printf("\n");
 }
 for(n=5;n>=3;n--)
 {
 for(c=1;c<=n;c++)
 {
 printf("*");
 }
 printf("\n");
 }
 getch();
 clrscr();
}
Output:

Using these examples you are in a better position to create your desired pattern for yourself. Creating a program involves how to use nested loops properly, some pattern may involve alphabets or other special characters. Key aspect is knowing how the characters in pattern changes.

Try it yourself.
If you have any problem you can tell me on comment box i will help you.




    Wednesday, 26 July 2017

    History Of C Language And Its Importance In Programming

    What Is C ?

    C is a high-level and popular-cause programming language that is good for growing firmware or portable Applications. At the beginning intended for writing system software, C changed into developed at Bell Labs through Dennis Ritchie for the Unix working device within the early Seventies.

    History Of C Language:

    C is a programming language evolved at AT & T’s Bell Laboratories of United States in 1972. It was designed and written through a person named Dennis Ritchie. Within the past due Seventies C began to replace the greater acquainted languages of that point like PL/I, ALGOL, etc. Nobody driven C. It wasn’t made the ‘reputable’ Bell Labs language. Hence, with none commercial C’s reputation unfold and its pool of customers grew. Ritchie appears to have been as a substitute surprised that so many programmers preferred C to older languages like FORTRAN or PL/I, or the newer ones like Pascal and APL. However, that is what befell.

    Importance Of C Language:

    I accept as true with that no one can learn C++ or Java directly. This is because at the same time as gaining knowledge of these languages you've got things like classes, items, inheritance, polymorphism, templates, exception managing, references, and so forth. Do address other than understanding the actual language factors. Mastering those complex ideas while you aren't even secure with the simple language elements is like placing the cart before the horse. For this reason one need to first learn all of the language elements very thoroughly using c programming language before migrating to C++, C# or Java. Although this two steps mastering manner may additionally take extra time, however on the give up of it you'll really find it really worth the trouble.

    Important components of popular operating systems like home windows, UNIX, Linux continues to be written in C. That is because even today when it comes to overall performance (pace of execution) nothing beats C. Furthermore, if one is to increase the working device to paintings with new devices one needs to jot down device driving force applications. Those programs are exclusively written in C.


    You need to have seen several professional 3-d laptop video games where the consumer navigates a few item, like say a spaceship and fires bullets at the invaders. The essence of all such video games is pace. Pointless to say, such games might not grow to be popular if they takes a long term to transport the spaceship or to hearth a bullet. To match the expectations of the participant the game has to react fast to the person inputs. This is where C language scores over other languages. Many famous gaming frameworks have been constructed using C language.

    I am hoping that these are very convincing reasons why one have to adopt C because the first and the very crucial step on your quest for learning programming languages.

    Learning C Language:

    Communicating with a computer includes speaking the language the laptop is familiar with, which right away regulations out English because the language of verbal exchange with computer. But, there may be a near analogy between mastering English language and gaining knowledge of c program language period. The classical technique of getting to know English is to first analyze the alphabets used inside the language, then learn how to combine these alphabets to form words, which in flip are blended to form sentences and sentences are mixed to shape paragraphs. Getting to know C is comparable and simpler. As opposed to instantly-away getting to know how to write programs, we should first understand what alphabets, numbers and special symbols are utilized in C, then how the usage of them constants, variables and key phrases are built, and finally how are those combined to shape an practice. A collection of instructions could be mixed later on to form a program.