Home | Lab 1 | Lab 2 | Lab 3 | Lab 4 | Lab 5 | Lab 6 | Lab 7 | Lab 8 | Lab 9 | Lab 10 | Lab 11 | Lab 12

Linked at EntranceNepal.com, i-geek blog & Ekendra's Share World

Object: I. To write a program to identify the greater number between any 2 given numbers.

Object: II. To write a program to find the greatest of the three given numbers

Download this full program | Lab 3 | in pdf format

Background Theory:

Conditionals and Statements: Conditionals are logical operations involving comparison of quantities (of the same type) using the conditional operators and the Boolean operators. How C can control the flow of logic in a program is stated by conditionals. Programs are generally written in three formats, namely

a.     Sequential- for unconditional cases

b.     Selective- conditioned statements

c.     Iterative- repeated statements

 

It is sometimes necessary to perform repeated actions or skip some statements during programming. For these actions, certain control statements are available. These statements alter flow of execution of the programs. Control statements are of two types:

1)      Decision Making Statements:
a. if statement
b. if-else statement
c. switch statement

 

2)      Loop Constructs
a. for-loop
b. while-loop
c. do-while loop

The if  statement

The if statement has the same function as other languages. It has three basic forms:

  if  (condition)
                         statement

...or:

  if  (expression)
                         statement1
             else
                         statement2

...or:

  if  (expression)
                         statement1
             else if (expression)
                         statement2
             else
                         statement3

 
Logical or Boolean Operators: 
Logical operators are usually used with conditional statements. The two basic logical operators are:
&&                          logical AND
||                          logical OR 
 
Conditional Operators: 
        <                             smaller than
               <=                           smaller than or equal to
               ==                           equal to
               !=                            not equal to
               >=                           greater than or equal to
               >                             greater than

 

Code:

// Program to identify the greater number between any 2 given numbers //

# include <stdio.h>                                     

# include <conio.h>

void main()

{

            int a, b;

            clrscr();

            printf (“input a= “);

            scanf (“%d”, &a);    

            printf (“input b= “);

            scanf (“%d”, &b);

            if (a>b)

            printf (“\n\t Greater number = %d”, a);

            else

            printf (“\n\t Greater number = %d”, b);

            printf(“\n\n\t\t Press any key to continue…”);

            getch();

}

// Program to find the greatest of the three given numbers //

# include <stdio.h>                         

# include <conio.h>

void main()

{

int a, b, c;

clrscr();

printf (“Input any three numbers = ”);

scanf (“%d”, &a);

scanf (“%d”, &b);

scanf (“%d”, &c);

if (a>b)

{

            if (a>c)

            printf(“Greatest number= %d”, a);

            else

            printf(“Greatest number= %d”, b);

}

else if (b>c)

printf(“Greatest number= %d”, b);

else

printf(“Greatest number= %d”, c);

printf(“\n\n\t\t Press any key to continue…”);

getch();

}

 

 

*** Alternatingly, this program can be written in Logical code as

 

            // Program to find the greatest of the three given numbers //

# include <stdio.h>                             

# include <conio.h>

void main()

{

int a, b, c;

clrscr();

printf (“Input any three numbers = ”);

scanf (“%d”, &a);

scanf (“%d”, &b);

scanf (“%d”, &c);

if (a>b)

{

            if (a>c)

            printf(“Greatest number= %d”, a);

            else

            printf(“Greatest number= %d”, b);

}

else if (b>c)

printf(“Greatest number= %d”, b);

else

printf(“Greatest number= %d”, c);

printf(“\n\n\t\t Press any key to continue…”);

getch();

}

 

To Download this full program | Lab 3 | in pdf format you must have PDF reader as Adobe Acrobat Reader 5 and above in your computer,

click here to download now.

Search yourself for more such codes

Google

Home | Lab 1 | Lab 2 | Lab 3 | Lab 4 | Lab 5 | Lab 6 | Lab 7 | Lab 8 | Lab 9 | Lab 10 | Lab 11 | Lab 12

© Protected under Creative Commons License.
Written by Ekendra Lamsal, www.ekendralamsal.com
for comments email at gmail address:
gearShifts@gmail.com