Our Blog Contain Detail about Some Technical Aspect like Programming, Blogger, Tools and Tip, Suggestion, Motivational, Health, Program in C and Java, Html

List the various types of loop available in C and explain any one with the syntax and suitable example.

 List the various types of loop available in C and explain any one with the syntax and suitable example.



Index

1. What is Loop

2. Various Type of Loop

3. Example of Loop

Ans: 

What is Loop

Loops:-Loops are the statement used to execute a sequence of instruction repeatedly. 

Various Type of Loop

There are three types of loop statements in C:
1.    for loop
2.    while loop
3.    do while loop

 

(1)    for loop:- for loop is used to execute statement multiple times .

            Syntax of for loop:-

            for(initialization ; condition ; increment/decrement){

                                    //body of loop

                        }

 

            Example:-

#include <stdio.h>

int main()

{

for(    int i = 5; i<10;i++)

 {

printf("%d\n",i);

    }

    return 0;

}

 

           

(2)    while loop:-The while loop is a control flow statement which is used to execute the statement repeatedly till the condition statement is true.

 

Syntax for while loop:-

while (boolean condition)

{

   loop statements...

}

 

Example:-

#include <stdio.h>

int main()

{

    int i = 5;

      while (i< 10) {

printf("%d\n",i);

i++;

    }

    return 0;

}

 

(3)    do-while:- Do while loop is similar to while loop with a difference that do-while loop execute the state then after that is check the condition.

Syntax for do-while loop:-

do

{

    statements..

}

while (condition);

 

Example:-

#include <stdio.h>

int main()

{

    int i = 5;

      do {

printf("%d\n",i);

i++;

    } while (i< 10)

    return 0;

}

Share:

What do you mean by an Array || How to declare and initialize 1-D array in C

What do you mean by an Array ? How to declare and initialize 1-D array in C? What is a limitation of an array? Write a program to perform the addition of 3x3 matrices using an array.



Index

1. What do you mean by an Array 

2. How to declare and initialize 1-D array in C

3. What is a limitation of an array

4. Write a program to perform the addition of 3x3 matrices using an array

What do you mean by an Array 

Array:- An array is a non primitive linear type data structure. An array is basically a collection of similar type of data structure which aims to bind similar data together. It is a simple data structure where data items are accessed using indexing.

How to declare and initialize 1-D array in C

Syntax for declaration of array :-

 

                        1D array:-        data_type[no_of_element];

                        2D array:-        data_type[no_of_row][no_of_column];

                        ND array:-        datatype[number]…….n times;

 

Iinitialization of array :-

                        int arr[5] = {1,4,6,3,8};

 What is a limitation of an array

limitation of array:-

  1. Their must be a prior knowledge of number of element to be stored.
  2. An array is a static structure, means the size of the array can not be modified after its declaration.
  3. Insertion and deletion of element is complex in an array.
  4. Allocating memory more than required causes wastage of memory.

 program to perform the addition of 3x3 matrices using an array

A program to perform the addition of 3x3 matrices using an array:-

#include<stdio.h>

#include<conio.h>

int main()

{

 int matrixA[3][3],matrixB[3][3],matrixC[3][3];

 int r,c,k;

 for(r=0; r<3; r++)

 {

  for(c=0; c<3; c++)

  {

printf("Enter first matrix : ");

scanf("%d", &matrixA[r][c]);

  }

 }

 for(r=0; r<3; r++)

 {

  for(c=0; c<3; c++)

  {

printf("Enter second matrix : ");

scanf("%d", &matrixB[r][c]);

  }

 }

 for(r=0; r<3; r++)

 {

  for(c=0; c<3; c++)

  {

matrixC[r][c]=0;

    for(k=0; k<3;k++)

matrixC[r][c] = matrixA[r][c] + matrixB[r][c];

  }

 }

printf("\n New addition matrix : \n");

 for(r=0; r<3; r++)

 {

  for(c=0; c<3; c++)

printf(" %d\t",matrixC[r][c]);

printf("\n");

 }

getch();

 return 0;

}

Share:

What do you mean by user defined Function | Explain the function definition, function declaration and function call with syntax and suitable example?

Q What do you mean by user defined Function | Explain the function definition, function declaration and function call with syntax and suitable example? 



Ans: A Function is a set of instruction which perform specific task. We can create a function according to our need using C. that function will be a user defined function. So, a function which is created by user to solve a particular problem is called user defined function.

Function Definition: -  In function definition, the user writes the code into the body of the function.

the syntax of function definition: -

                        return_Typefunction_name(argument_1, argument_2,....){

//statements

//body of the function

}

for example:-

                        int fact( int n){

                                    if(n<=1){

                                                return 1;

                                    }

                                    else{

                                                return fact( n-1 );

                                    }

                        }


Function declaration :- A function declaration is simply creating a frame for a function. It contains function name , list of arguments and its return type.

The syntax for function declaration:-

           return_Type function_Name(argument_1,argument_2,......);

For example:-

                        int swap( int a , int b);


Funtion calling :- When a function is called, the the code written in the body of that function is executed. Basically after the function is called the control of flow goes to the function and it start executing.

Syntax for function calling:-

                        function_name(argument_1,argument_2,……);

For example:-

                        swap(a,b);


Simple Example For Function Definition , Declaration  And Function Calling 

#include<stdio.h>

#include<coinio.h>

void Display(); // here we declare the function

void main()

{

Display(); // here we call the function

}

void Display()  //here we define the function 

{

printf("Hello Friend");

}

There are following type of user defined Function

1. function with no return type and no Parameter

2. function with no return type but have Parameter

3. function with return type and Parameter.

Share:

What is flow chart || List the symbols that are used to draw a flow chart || Develop a flowchart to find out the minimum of the given three numbers.

FLOW CHART

Index

1. What is flow chart
2. List the symbols that are used to draw a flow chart.
3. Flowchart to find out the minimum of the given three numbers?

After reading block you can able to answer these Question, What is flow chart? List the symbols that are used to draw a flow chart. Develop a flowchart to find out the minimum of the given three numbers?    

What is flow chart?

A flowchart is basically a symbolic or graphical representation of any process. It is also called a diagrammatic representation of an algorithm, a step by step procedure to solve a task. A flowchart uses various boxes to represent the steps and these boxes are connected with arrows.

A flowchart plays an important role in writing programs as well as to explain that programs to others.



Float Chart


List the symbols that are used to draw a flow chart.

In Flow Chart, we use Symbols and Shape, those are connected with each other to indicate the processing and flow of control in the program.

Here are given some most common shapes along with their names and meanings used in drawing a flowchart:



Terminals :  

Terminals represent starting and Ending of the flow chart and the shape used is Ovals as shown in above diagram

Process: 

To represent processing in flow Chart we use rectangle box in which we can represent mathematical and Logical calculation.
This is also known as Action symbol.

Input/Output:

it is used to represent the input that is taken from user and output calculated by the program. Input/output statement is represented by Parallelogram

Flow Line:

the flow line are the directed lines those represent the direction of flow of control in flow chart

Connective:

In case of a lengthy flow chart So it has several parts on different pages to connect them all we use the circle shape labeled with an alphabets as a connector.

Flowchart to find out the minimum of the given three numbers









Share:

What do you mean by decision making in C | Which are the statements in C that provides the facilities of decision making and branching

Q What do you mean by decision making in C? Which are the statements in C that provides the facilities of decision making and branching? Explain nested if else statement with suitable Example?

Solution:

Decision Making is related to choosing one out of two statement on the basis of some condition like suppose we want to take decision that is Ram eligible for Job? then in this situation we are going to check for age of Ram. If the age of Ram is greater than 18 then yes otherwise no. Such a situation in c is handled using Conditional Statement. Conditional statements in C are used for decision making. Based on the result of a set of conditions, decisions are made. One or more conditions are required to be evaluated by the program, having statement(s) to be executed if the condition evaluates to true and  statement(s) to be executed if the condition evaluates to false.



Statements which provide the facility of decision making are:

·         if statements

·         if else statements

·         nested if else statements

·         if-else-if ladder

·         switch statements


Statements which provide the facility of branching are:

·         break

·         continue

·         return

·         goto

Nested if else statement- Using a if else statement inside another if else statement is called a nested if…else statement. It is basically used to test when there are multiple conditions.

Syntax:

If(test condition)

{

            if(test condition)

            {

            statements;

            }

            else

            {

            statements;

            }

}

else

{

            statement;

}

 

Example: Program to find the greatest of three numbers entered by the user.

In this example we have to think about our self suppose I have three number in order to find greatest of three numbers we perform following steps

let A, B, C are three variable which are going to hold the value of three number we have to compare.

Step 1: we first compare any two number suppose A and B

Step 2: in this step we are going to compare biggest number out of A and B with 3rd number.  

Step 3: print the greatest number out of A, B, C. 

#include<stdio.h>

void main()

{

            int x, y, z;

            printf(“Input three numbers:\n ”);

            scanf(“%d%d%d”, &x, &y, &z);

            if(x>y)

            {

                if(x>z)

                {

                        printf(“%d is the greatest number”, x);

                }

                else

                {

                printf(“%d is the greatest number”, z);  

                }

            }

            else

            {

                if(y>z)

                {

                        printf(“%d is the greatest number”, y);

                }

                else

                {

                        printf(“%d is the greatest number”, z);

                }

}

}

Share:

Translate

Followers

Email Subscription

Enter your email address:

Delivered by FeedBurner

Recent Posts

Theme Support

Need our help to upload or customize this blogger template? Contact me with details about the theme customization you need.