C-Programming

Control statement

C program consists of set of instructions which are executed in sequentially order as written in any high level programming language. But many cases may arise where we have to choose the statement to be executed. At this instant we require the instruction which transfer the control of the program from one point to another point in the program and sometimes we have to repeat the same task for certain number or times. C has this type of facility. This type of statement (instruction) which transfers the control of the execution of the program from one point to another point is called control statement.

C supports following types of control statements:

  1. a) Sequential control structure
  2. b) Decision making control statement
  • simple if statement
  • if – else statement
  • if-else-if statement
  • nested if statement
  • conditional operator statement
  • switch statement
  1. c) Iteration / looping statements
    1. while loop
    2. do-while loop
    3. for loop
  2. special control statement
    1. goto statement
    2. break statement
    3. continue statement

 

Sequential control statement:

It is the flow of the statements in sequential order that means as written in the source code. The sequence control statement ensures that the instructions in the program are executed in the same order as they appeared in the program.

Flow chart of sequential control statement is:

Decision making control statement:

Those statements or instructions which are used to make some decisions are called decision making statement. These statements are also called as selection statement. Using these statements we can select the required statement to be executed. In this type of structure the test condition decides the flow of the execution. Here we describe the different types of decision making statements in detail:

a) Simple if statement:

By name of this statement, we can guess that this statement is used for making decision and therefore this statement is also known as decision making statement. Here if is key word (reversed word). The general syntax of this function is:

if(condition)    // Here if should be in lower case.

{

true statement(s);

}

next statement;

fig: flow chart of simple if statement

Explanation of the syntax and flow chart:

Here, initially condition (which is logical condition) is checked. If the condition is true then true statement enclosed by a pair of braces is executed and then next statement will be executed otherwise (i.e. if the condition is false then) without executing the true statement directly next statement will be executed as indicated in the syntax or flow chart. Here if the condition is false then it leaves the some statement and transfers the control of the program from one point to another point. Therefore this statement is called control statement.

Examples:

/*write a program to check whether the number entered by the user is even or odd*/

Before writing the code for this problem we should know what type of number is called even number and what type of number is called odd number. The number which is perfectly divisible by 2 is called even number and remaining other are called odd number. On the basis of this clue we have to write a complete program. Before writing the code, let us write algorithm and flow chart of the program.

Algorithm of the program:

  1. START the program.
  2. Read a number.
  3. If number is perfectly divisible by 2 then print “the number is even.”
  4. If number is not divisible by 2 then print “the number is odd.”
  5. STOP the program.

Flowchart of the program:

 

Source code:

#include<stdio.h>

#include<conio.h>

void main()

    {

int number;

printf(“enter a number:   “);

scanf(“%d”,&number);

if(number%2==0)

printf(“\nentered number is even.”);

if(number%2!=0)

printf(“\nentered number is odd);

getch();

    }

Sample output of the program:

enter a number:    12

enterd number is even.

enter a number:     3

entered number is odd.

b) if-else statement:

“if– else” statement is the real decision making statement. In simple if statement  there is no alternative i.e. if the condition is false then there is no any alternative but in case of if else statement, there is alternative. Here else is key word.  The general syntax of this statement is:

          if(condition)

                   {

                             true_statement(s);

         }

    else

         {

             false_statement(s);

         }

    next_sstatement;

Flow chart of this statement:

Explanation of syntax and flow chart:

          Here, firstly the condition is checked. If the condition is true then the true statement(s) will be executed as in the simple if statement otherwise false statement(s) will be executed. That means either true or false, only one statement will execute not both at a time. After executing one of the statements, then next statement will be executed.

/*draw a flow chart and write source code of a program which finds out the whether the entered number is odd or even.*/

Answer:

   

Flowchart of the program:

Source code for program:

#include<stdio.h>

#include<conio.h>

void main()

{

int number;

printf(“please enter a number:   “);

scanf(“%d”,&number);

if(number%2==0)

printf(“\nEntered number is even.”);

else

printf(“\nEntered number is odd.”);

getch();

}

Sample output:

please enter a number: 13

Entered number is odd.

please enter a number:  18

Entered number is even.

  1. if-else-if statement (if-else ladder):

In this form of if statement, conditions are checked first. If the condition is found to be true then the statements associated with the condition will be executed and other conditions will be skipped. If none of the conditions are true, then it will execute the statement of else side.

          It takes the following form of syntax:

if(condition1)

    {

statement1;

}                      

else if(condition2)

    {

statement2;

    }

else if(condition3)

    {

statement3;

    }

else ………..

else if(condition_n)

    {

statement_n;

    }

else

    {

statement_n+1;

    }

next_statement;

Flow chart of if-else-if statement:

Statement 3

 

 

Next statement

 

Statement n+1

 

Write a program that takes average marks obtained by a student. Your program should display the result and division of that student.

If average marks >=80, distinction

If average marks>=60 but <80, first division

If average marks>=45 but <60, second division

If average marks>=35 but <45, first division

To pass the average marks should be greater or equal to 35.

Solution:

#include<stdio.h>

#include<conio.h>

void main()

{

float ave;

printf(“enter the average marks obtained by the student:\t”);

scanf(“%f”,&ave);

if(ave>=80)

printf(“\nresult=  pass.\ndivision= distinction.”);

else if(ave<80&&ave>=60)

printf(“\nresult=  pass.\ndivision= first division.”);

else if(ave<60&&ave>=45)

printf(“\nresult=  pass.\ndivision= second division.”);

else if(ave<45&&ave>=35)

printf(“\nresult=  pass.\ndivision= third division.”);

else

printf(“\nresult= fail.\ndivision= no division.”);

getch();

}

d) Nested if statement:

If an “if” –statement contains if statement within the statement then such type of condition is called nested if condition. Nested if is an if that has another if in its if’s body or in its else’s body. In other word, “if else” statement within another “if-else” body is known as nested if. The general syntax of this type of statement is:

if(condition)

{

    if(condition)

         {

             statement(s);

         }

    else

         {

             statement(s);

         }

}

else

{

statement(s);

}

Examples:

Design a program in C with flow chart that reads three numbers a, b, c and prints the largest number among them using nested if structure.

Source program:

#include<stdio.h>

#include<conio.h>

void main()

{

int a,b,c;

printf(“plese enter any three integer numbers:\t”);

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

if(a>b)

    {

    if(a>c)

         printf(“\n%d is largest”,a);

    else

         printf(“\n%d is largest.”,c);

    }

else if(b>a)

printf(“\n%d is largest.”,b)

else

printf(“%d is largest.”, c);

getch();

}

e)  Conditional operator statement:

The name of the statement type suggests that it is that type of statement which uses conditional operator for the operation. Simply this is the combination of simple if else statement. Syntax of this type of statement is:

Expression1 ? expression 2: expression 3;

In this if the expression1 is true then expression2 is evaluated other wise expression3 will be evaluated. This is equivalent to:

if(expression1)

expression2;

else

expression3;

 

Example:

Write a program that finds out the greatest number among two numbers using conditional operator statement.

#include<stdio.h>

#include<conio.h>

void main()

    {
int a,b;

int greater;

printf(“Please enter any two numbers:   “);

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

(a>b)? greater = a : greater = b;

pritnf(“\n\ngreater number is %d”,greater);

getch();

    }

 

f) Switch statement:

If we have to create a program of menu system then we can use switch statement. This is similar to if-else-if statement. Use of this statement makes the program more users friendly and more standard. The general syntax of this statement is:

switch( choice)

{

case 1:

    statement(s);

    break;

case 2:

    statements(S);

    break;

……

……

……

default:

    statement(S);

    break;

}

if the choice is of character type i.e. alphabet then incase of writing case 1, case 2,………… we have to write case ‘a’, case ‘b’, etc.

Explanation of the syntax of the switch statement:

If the choice is 1, then it only executes the statements written in case 1 block, then it directly transfers the control of the program out of the switch statement. If choice is not matched then it executes the statements written in default block and transfers the control out of statement.

Example: write a menu type program that reads two integer’s value and asks for the choice. If you press 1 then your program should add them and display,  and similarly on pressing 2 subtraction , on pressing 3 multiplication , on pressing 4 division and on pressing 5 remainder should display. If any other than 1-5 is pressed then your program should display an appropriate message.

#include<stdio.h>

#include<conio.h>

void mian()

{

    int a,b,res, choice;

flaot div;

printf(“please enter any two integer numbers:    “);

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

printf(“\n\n\n\n\n”);

printf(“\n1. addition\n2. subtraction\n3. multiplication\n4. division\n5. remainder”);

printf(“\n\nplease enter your choice(1-5):\t”);

scanf(“%d”,&choice);

switch(choice)

{

case 1:

    res=a+b;

    printf(“\naddition of given numbers is %d”,res);

    break;

case 2:

    res=a-b;

    printf(“\n subtraction of given numbers is %d”,res);

    break;

case 3:

    res=a*b;

    printf(“\n multiplication of given numbers is %d”,res);

    break;

case 4:

    res1=(float)a/b; //type casting

    printf(“\ndivision of given numbers is %d”,res1);

case 5:

    res=a%b;

    printf(“\nremainder after dividing %d by %d is %d”,a,b,res);

    break;

default:

    printf(“\n your choice id out of range. Please try again.”);

}

getch();

}

sample output of the program:

please enter any two integer numbers:  12 4

 

 

  1. addition
  2. subtraction
  3. multiplication
  4. division
  5. remainder

please enter your choice(1-5):   3

multiplication of given numbers is 48

 

please enter any two integer numbers:  12 4

 

 

  1. addition
  2. subtraction
  3. multiplication
  4. division
  5. remainder

please enter your choice(1-5):   7

your choice is out of range. Please try again.

Write a program to calculate area of triangle, circle and sphere using switch-case statements.

#include<stdio.h>

#include<conio.h>

void main()

{

char ch;

float area;

printf("a. area of triangle.\nb. area of circle.\nc. area ofrectangle\n\n");

printf("\n enter your choice (a-c):");

ch=getchar();

switch(ch)

{

case 'a':

float base, height;

    printf("enter base and height of the triangle:  ");

     scanf("%f%f",&base,&height);

     area=.5*base*height;

     printf("\narea of the triangle is : %f", area);

     break;

case 'b':

float radius;

printf("\nenter the radius of the circle:  ");

scanf("%f",&radius);

area=3.14159*radius*radius;

printf("\narea of the circle is %f",area);

break;

case 'c':

float length,breadth;

printf("\nenter the lenght and breadth of the rectangle:  ");

scanf("%f%f",&length,&breadth);

area=length*breadth;

printf("\narea of the rectanlge is:  %f",area);

break;

default:

printf("\n Wrong entry. Please enter valid choice.");

break;

}

getch();

}

 

sample output:

  1. area of triangle
  2. area of circle
  3. area of rectangle

please enter your choice (a-c): 2

Wrong entry. Please enter valid choice.

 

 

  1. area of triangle
  2. area of circle
  3. area of rectangle

please enter your choice (a-c): b

enter radius of the circle 12.15

area of the circle is  490.873437

 

Explanation of the program:

There is confusion in the program which is that we are saying that no declaration can be done on the execution part of the program but here we are declaring the various variables inside the execution part of the program. We can do this but it should take care that they work only inside that block only.

Write algorithm, flow chart and program to fin roots of a quadratic equation. Suppose that (b2-4ac) has three different conditions. <2062Baishakh DCT> [10]

Answer:

Algorithm of the program:

  1. START the program.
  2. Read the coefficient of x2, x and constant and store them in a,b,c.
  3. calculate d=b2-4ac
  4. If d is greater than 0, then do this otherwise go to step 5.
    1. root1=(-b+d)/(2*a)
    2. root2=(-b-d)/(2*a)
    3. Print the message roots are real and unequal.
    4. Print root1 and root2
  5. If d is lesser than 0 then do this otherwise go to step 6.
    1. Imag=sqrt(abs(d))
    2. Print the roots are imaginary and they are:
      1. Root1=-b/(2*a)+(imag/(2*a))i
      2. Root2=-b/(2*a)-(imag/(2*a))i
    3. if d is equal to 0 then do this:
      1. root1=-b/(2*a)
      2. root1=root2
      3. print the message roots are equal and real
      4. print root1 and root2
    4. STOP the program.

Flow chart of the program:

 

SOURCE CODE OF THE PROGRAM:

#include<stdio.h>

#include<conio.h>

void main()

{

flaot a,b,c;

flaot root1,root2,imag;

pritnf(“\nPlease enter the coefficient of x^2, x and constant:\t”);

scanf(“%\f%f%f”,&a,&b,&c);

    d=b*b-4*a*c;

    if(d>0)

{

    root1= (-b+sqrt(d))/(2*a);

    root2= (-b-sqrt(d))/(2*a);

    printf(“\n\nroots are real and unequal. They are %f and %f”,root1,root2);

}

else if(d<0)

{

    imag=sqrt(abs(d))/(2*a);

    real=-b/(2*a)

printf(“\n\nroots are imaginary and they are %f+%fi and %f-%fi”,real,imag,real,imag);

}

else

    {

root1=(-b/(2*a));

root1=root2;

printf(“\n\nroots are real and equal. They are %f and %f.”,root1,root2);

    }

getch();

}

  1. c) Iteration / looping statements:

If we have to do same task for several times then use of sequential programming approach becomes more difficult, time consuming and less use friendly. In this situation we need a type of statement which can perform the same task for specified times. This type of statement is called looping statement and the process is called iteration. In general, these types of statements include four things: 1. setting or initializing a condition variable. 2. Execution of the statements inside the loop. 3. Updating the condition variable and 4. Test the specified value of the condition variable. Here the condition may be either to determine whether the loop has been repeated the specified number of times or to determine whether a particular condition has been reached.

There are mainly three types of looping statements used in C and they are:

  1. while loop/statement
  2. do-while statement
  3. for statement

  1. The while statement:

This is a looping statement where the statements that written inside the loop executed till the condition is true. The general syntax of this looping statement is:

Initiliazation;

while(test_expression/condtion)

    {

statements;      //body of the loop

expression;     

}

    next_statement;

Flow chart of this statement:

 

 

 

 

            The “while statement” is an entry-controlled loop statement. That means the execution of the loop is wholly depends upon the entry condition. At first the test condition is evaluated and if the condition is true, then the body of the loop is executed. After the execution of the body, the test condition is again tested and again it yields true then again the loop will be executed. This process will go on continuously until the test condition yields result as false. When the test condition become false then the control of the program will transfer just outside the loop and then next_statement will be executed. The body of the loop may or may not contain more than one statement. The pair of braces will only needed of the body of the loop contains two or more than two statement i.e. if the body of the loop is compound statement. But for good practice it is better to put pair of braces even for a single statement.

Example:   write a program to print the numbers from 0 to 100.

Ans:

 Flow chart of the program:

Source code:

#include<stdio.h>

#include<conio.h>

void main()

     {

int i;

printf(“the required series is given below:\n\n\n”);

i=0;         //initialization

while(i<=100)    //test condition

     {

printf(“%d\t”,i);// body of the loop

i++;             //expression

     }

getch();

}

Using while loop only, write a program to print the following series untill the term value is less than 750. The series is: 1,2,5,10,17,26,……………<BEX 2063, Baishakh> [8]

Answer:

#include<stdio.h>

#include<conio.h>

void main()

{

int i=0,term;

term=0;

while(term<=750)

    {

term=i*i+1;

i++;

printf(“%d\t”,term);

    }

getch();

}

Using while statement, write a program to find out the factorial value of a number entered by the user.

ANSWER:

#include<stdio.h>

#incldue<conio.h>

void main()

{

int num,i;

long int fact=1;

pritnf(“\n please enter the number whose factorial value is to be calculated:\t”);

scanf(“%d”,&num);

i=1;

while(i<=num)

    {

fact=fact*i;

i++;

    }

printf(“\n\n\nfactorial value of %d is %ld.”,num,fact);

getch();

}

Sample output:

please enter the number whose factorial value is to be calculated:    10

factorial value of 10 is 3628800.

Using while statement, write a program to evaluate the equation y=xn where n is non-negative integer.

Answer:

#include<stdio.h>

#include<conio.h>

void main()

{

int x,y,n;

int i;

printf(“\nenter the value of x and n:\t”);

scanf(“%d%d”,&x,&n);

 i=1;

y=1;

while(i<=n)

    {

y=y*x;

i++;

    }

printf(“\n\n\nRequired result is %d”,y);

getch();

}

  1. do-while statement:

This statement is also used to repeat some statements for specified no of  times. In many cases even the condition is failed, we have to do that work at least one time. This problem is fascinated by do-while statement. In the name of this statement, “do” means perform the work or execute the statements and “while” means until the condition is satisfied. That means in “do-while” statement, statements are executed first and then condition is checked. Therefore this statement is exit controlled statement, because the condition for the loop is evaluated after the execution of the statements. The general syntax of this looping structure is:

    do

{

statement(s);

}

while(condition);

         next_statement;

The flow chart for this statement is:

               

       

In the syntax of do-while statement it should be take care that, the while statement should be end with semicolon. This is also the difference between while and do-while loop.

The differences between the while and do-while statement:

While loop Do-while loop
1. It is an entry controlled loop. That means the entry point decides whether to enter into the loop or not. 1. It is exit controlled loop. That means the exit point of the loop decides whether to again enter into the loop or not.
2. The test condition is tested outside the loop before entering into the loop. 2. The test condition is tested within the loop before exiting the loop.
3. If the condition is not satisfied then the statements inside the loop will not be executed. 3. Even the condition is not satisfied at least one time the loop will be executed.

Examples of do-while statement:

Write a program that prints the given series and their sum.

1,4,9,16,25,…………………………… up to the term value less than 1500.

Answer:

#include<stdio.h.

#include<conio.h.

void main()

{

int i,sum=0,term;

printf(“\nThe required series is:\n\n”);

i=1;

do

{

term=i*i;

sum+=term;

i=i+1;

printf(“%d\t”,term);

}while(term<=1500);

printf(“\n\nsum of the series is:  %d”,sum);

getch();

}

  1. The For loop/statement:

This “for” loop is one of the most popular loops among other two loops in C programming. As in the while loop, it also allows to define condition to repeat the block of statements for number of times. In while loop we can not initialize the value of condition variable but in for loop we can do it. For many cases while and for loop are very similar to each other. This is also entry controlled statement but in some cases it is neither entry controlled nor exit controlled. This is also called as counter controlled statement. The general syntax of for loop is:

for(initialization;test_condition;expression)

     {

 

body_of_the_loop(statements);

 

   }

next_statement;

The flow chart of this statement is:

         

Explanation of the syntax and flow chart of for loop:

When the control of the program entries in for loop, at first it initializes a value on a variable and then checks the condition. If the condition is true then it enters into the loop and body of loop (i.e. blocks of statements) will be executed. After the execution of body of loop, the expression will be executed and then again checks the condition. And same job will repeat until the condition is failed. If the condition is failed then the control of the program doesn’t reach the body of loop, and directly transfer to the next_statement.

Examples:

Write a program that generates the following series using for loop.

1,2,5,10,17,26,……………up to 20th term.

#include<stdio.h>

#include<conio.h>

void main()

{

int i,term=20,value;

for(i=0;i<term;i++)

{

value=i*i+1;

printf(“%d\t”,value);

}

getch();

}

 

WRITE A PROGRAM THAT FINDS OUT THE GREATEST NUMBER AMONG 100 NUMBERS.

#include<stdio.h>

#include<conio.h>

void main()

{

int i,num,greatest;

for(i=1;i<=100;i++)

{

printf(“\nPlease enter %dth number:\t”);

scanf(“%d”,&num);

if(i==1)

{

greatest=num;

continue;

}

if(greatest<num)

greatest=num;

}

printf(“\n\nthe greatest number is %d.”,greatest);

getch();

}

 

Nested loops:

If a looping statement contains another looping statement in its body, then this type form of loop is called nested loop. The occurance of one loop within another loop is known as nested loop.

Rules for costructing the nested loop:

  1. As outer loop and inner loop can not have same control variable.
  2. The outer loop starts first and then only inner loop starts.
  3. The inner loop must terminate before the termination of the outer loop.
Flowchart Syntax:
 

Statement block

 

Statement block

 

        No

Inner loop

                     yes

                                 no

 

 

yes

 

 

Outer loop

 

 

 

For(condition)

{

for(condition)

{                                inner loop

statement_block;

}                                  outer loop

statement_block;

}

  1. Special control statement/ Jumping statement:

By the name of this statement we can understand, what does this statement? Simply these statements are used to jump the statements as our requirement. Therefore this is called as jumping statement.  There are mainly three types of jumping statements which are:

  1. goto statement
  2. break statement
  3. continue statement

  1. The goto statement

This statement is used to transfer the control of the program anywhere within the program. Using this statement we can break the sequence of execution of the program without having any test condition and transfers the control to the statement having the statement label rather than any other statements. On the basis of transference of the control within the program, we can assume the two types of goto statement. They are:

  1. forward goto statement
  2. backward goto statement
Syntax of forward goto statement Syntax of backward goto statement
goto label;

…………………

……………..

………………..

………………..

…………………

label:

………….

label:

…………………

………………..

………………….

…………………..

…………………

goto label;

…………………….

While progamming it is better not to use goto statement because use of this statement makes the program poor. The use of goto statement in the program is considered as poor programming. Use of this statement is accepted in only few statement. In the examination you are requested to don’t use this statement, if you use this statement then you will not awarded full marks. 

Example:

Write a program that generates the multiplication table of a number entered by the user without using any looping structure. (Use goto statement).

#include<stdio.h>

#incldue<coni.h>

void main()

{

int i=1,num,mul;

printf(“\nplease enter the number:\t”);

scanf(“%d”,&num);

up:

mul=i*num;

i++;

printf(“\n%d*%d=%d”,num,i,mul);

if(i<=10)

goto up;

getch();

}

 

  1. The break statement:

This statement is used to break or stop the loop or to escape from switch ( ) statement. Especially the break statement is used to end the infinite loop whose number of execution is not known. Generally this statement comes after if statement because in which condition we have to stop the loop that condition is must be checked and if the condition is satisfied then loop is to be stopped. The general syntax of the break statement is:

Syntax: Flow-chart
Looping statement

{

statement_1;

………

………           

if(condition2)

break;

…………

……………

statement_2;

}

next_statement;

 

 

                       

 

 

true

 

 

      false

 

                                                                                  

 

                                       

 

Explanation of the syntax or flow chart:

When the execution of the program reach the loop firstly condition1 is checked, if the condition is true only then it enters into the loop. After entering into the loop it executes the statement_1 and other statements. In flow-chart, the dashed arrow head indicates that there may be other set of statements.  Then it checks the condition2. If this condition is true then it executes the break statement which transfers the control of execution directly outside the loop i.e. to next_statement with out executing the remaining statements after the break statement. If the condition is failed then it executes the statements up to the statement_2, after the break statement. And then again condition1 is evaluated and similar task will be repeated. Those looping structures in which break statement is used, there the number of times of execution of loop depends up on the loop condition i.e. condition1 or break condition i.e. condition2.

Example:

Write a program that displays the first natural numbers until a number is encountered which is multiple of both 5 and 4.

#include<stdio.h>

#include<conio.h>

void main()

{

int i;

for(i=1 ; ;i++ )

{

if(i%4==0&&i%5==0)

break;

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

}

getch();

}

Output of the program:

 

1       2       3       4       5       6       7       8       9       10

11      12      13      14      15      16      17      18      19

  1. The continue statement:

This is also another jumping statement. The “continue” statement and the “break” statement are seemed to be similar but there is huge difference between them. The “continue” statement is used to escape the certain statement if some specified condition is satisfied and again return to the loop. The break statements stops the loop if the condition for the break statement is true but in the case of continue statement loop will not be stopped. This is the major difference between these two statements. The general syntax and flow chart of the continue statement is:

Syntax Flowchart
Loop statement

{

statement_1;

…………

…………

if(condition2)

continue;

………………..

………………..

statement_2;

}

next_statement;

 

             no                   yes

 

 

Next_statement

 

      yes

 

 

                                                 no

Explanation of the flow chart or syntax:

As in the break statement when the control of the execution reaches the loop statement, firstly it evaluates condition1. If the condition1 is true only then it enters into the loop. After entering into the loop, it executes the statement1 block and checks the condition2. If the condition2 is true then it executes the continue statement and transfers the control directly to the loop without executing the remaining statements after continue statement. And again loop will be continued. If the condition2 is false then the statements after the continue statements will be executed only then loop will be continued. In this type of looping where only continue statement is used the number is execution time of loop is depends only on the loop condition i.e. condition1.

Example:

Write a program that prints the numbers from 1 to 15 except 7 and 11.

#include<stdio.h>

#include<conio.h>

void main()

{

int i;

for(i=1;i<=15;i++)

{

if(i==7||i==11)

continue;

else

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

}

getch();

}

There are also other jump statements such as return and exit. The detail on return statement will be studied in function and exit statement is used to terminate the whole program at our required point.

Write a program that reads two numbers. If the numbers are multiple of both 3 and 4 terminate the program with an appropriate message other wise display the sum and difference between the numbers.

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>         //for exit() statement

void main()

{

int a,b;

int sum,dif;

printf(“\nplease enter any two integer number:\t”);

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

if(a%3==0&&a%4==0&&b%3==0&&b%4==0)

{

printf(“\nYou have entered the numbers which are multiple both 3 and 4”);

printf(“\n\nPlease enter any to exit the program:  “);

getch();

exit();

}

else

{

sum=a+b;

dif=a-b;

printf(“\nsum of the numbers is:  %d”,sum);

printf(“\ndifference between the entered numbers is %d”,dif);

getch();

}

Data input and output (Prev Lesson)
(Next Lesson) Program Solutions
Back to C-Programming

No Comments

Post a Reply

Teacher
Bhim Gautam
Role : Lecturer
Read More
error: Content is protected !!