If else statement in C Program

by Vivek Gite on August 16, 2007 · 83 comments

Q. Can you give me example for If else statement in C Program under Linux or TC?

A. C follows universal syntax for if..else. You can use any complier GNU Linux gcc or UNIX or old good TC.

if..else syntax

General if..else syntax is as follows:

if ( condition ) {
   expr_set1;
}
else  {
   expr_set2;
}

If given condition is TRUE, expr_set1 will get executed.
If given condition is FALSE (not TRUE), expr_set2 will get executed.

if..else example

Following example will find out large number from given input:

#include<stdio.h>
int main(){
        int x,y;
        printf("Enter value for x :");
        scanf("%d",&x);
        printf("Enter value for y :");
        scanf("%d",&y);
        if ( x > y ){
                printf("X is large number - %d\n",x);
        }
        else{
                printf("Y is large number - %d\n",y);
        }
        return 0;
}

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

{ 83 comments… read them below or add one }

1 ferdinand v. baguingay December 10, 2008
#include
int main(){
        int x,y;
        printf("Enter value for x :");
        scanf("%d",&x);
        printf("Enter value for y :");
        scanf("%d",&y);
        if ( x > y ){
                printf("x is large number - %d\n",x);
        }
        else{
                printf("y is large number - %d\n",y);
        }
        return 0;
}

Reply

2 genji July 2, 2010

i need more information about “if else”

Reply

3 merc velasco August 2, 2011

#include
main()
{
int grade;
char student[25],section[25];

clrscr();
printf(“Enter the name of a student: “);
scanf(“%s”, &student);
printf(“Enter the section of a student: “);
scanf(“%s”, &section);
printf(“Enter the grade of a student: “);
scanf(“%d”, &grade);

if(grade>75)
printf(“PASSED”);
else
print(“FAILED”);

getch();
}

Reply

4 twinkle September 2, 2011

require more explanation
and examples

Reply

5 affrin January 2, 2012

i need the answer for small and greatest number in 10 numbers

Reply

6 nazmul January 27, 2009

I need more if else program.i have simple problem.i don’t know your wbsite is active or not.next time i give a problem please solve it.

Reply

7 ankita May 5, 2010

I need more program about if else

Reply

8 Meera. M May 25, 2010

#include
#include
main()
{
int age;
clrscr();
printf(“Enter the age:”);
scanf(“%d”, &a);
if(a<18)
printf("can't vote");
else
printf("can vote");
}

Reply

9 Sharad Puri May 26, 2011

PROGRAM To find whether a given year is leap or not
main()

{

int y,r1,r2;

printf(“Enter a year”);

scanf(“%d”,&y);

r1=y%4;

if (r1 = = 0)

{

r2=y%100;

if(r2 = = 0)

printf(“The given year is not a leap year”);

else

printf(“The given year is a leap year”);

}

else

printf(“The given year is not a leap year”);

}

Reply

10 tyaiba August 25, 2011

why we use y%4.show me answer

Reply

11 adil August 26, 2011

y%4

that means which value is store in y that is divided by 4
% is a modular

Reply

12 azizur rahman October 7, 2011

the year divided by 4, get remainder zero ,is leap year.

Reply

13 aldrin March 22, 2009

can you convert this to java program?

Reply

14 saviye.saviyola April 21, 2009

I just study programming .

Reply

15 alyssa mae cruz July 6, 2009

hello all..exiting ang programming nuh?haha

Reply

16 markjasonarceo September 21, 2009

how to analyze the problem to make c program?tnx

Reply

17 carrys September 24, 2009

hi i am a n IT student. i just want to ask if you could pls give me some simple problems regarding if else , if else if else, and i/o. and also those more complicated ones. thanks.

Reply

18 Pooja Sree December 3, 2009

Super Example For If Statement

Reply

19 allen joyce December 15, 2009

i’m excited to try this program

Reply

20 allen joyce December 15, 2009

i need to create program right now for my project …………….help me …………………hehehe using IF ELSE STATEMENT

Reply

21 allen joyce December 15, 2009

Give me another example of IF ELSE STATEMENT

Reply

22 bayan December 23, 2009

can you help me to write this home work in C programs

*wirte the programsin C about if else
*the program should ask the user to enter the total marks out of 100, and then display the letter grade on the screen
*Grade Rules:
75 and above=”A”grade
60-74=”B”grade
50-59=”c”grade
40-49=”D”grade
Below 40=”Fail”

thanks .
please send it to my Email bayan321@live.com

Reply

23 Kenny June 19, 2010

Your homework is very easy just use the
if(user>=75)
printf(“A”);
else if(user>=60 && user<=74)
printf("B");

and so on.

Reply

24 sujesha October 2, 2010

did you manage to get the answer?
did anyone mailed you?
thanks
sujesha

Reply

25 riyas October 31, 2010

void main()
{
float mark;
clrscr();
printf(“Enter the total mark out of 100″);
scanf(“%f”,&mark);
if(mark<==75)
{
print{"Student has secured A Grade ";
}
else
{
.
.
.
.
.
.
getch();
}

Reply

26 myrabelle cabriana March 8, 2011

thank u..

Reply

27 emxif January 22, 2010

i need some help guys
i am making a function oriented program and i used if else statement, i know that my statement is right but there is only 1 error and it says misplaced else…..
how can i fix that?
pls help me….
plss notify me on my email
thanks

Reply

28 ali March 3, 2011

Hello
make sure you use the proper braces before and after else statement.

Reply

29 Glenn February 3, 2010

can you give more examples :-)

Reply

30 Levi February 18, 2010

Thanks I love you!!

Reply

31 mabel March 6, 2010

pls. give me a program example of official receipt of sm using if else statement pls. we need it badly…pls…thanx

Reply

32 Darren March 9, 2010

why can’t I place another “if statement” inside and “if statement”?

Reply

33 KAJI April 14, 2010

using visual basic net i have to write a program using if-else statement based on the following condition the grdes of the students
0-49 D
50-55 C
56-60 C+
61-70 B
71-75 B+
76-85 A
86-100 A+

The program should repeat itself unless the user enters a negative value to terminate the program

Reply

34 venkata sai November 12, 2010

can you give me the def of if family statements

Reply

35 Muneeb zargar June 1, 2010

Use indentations ur prblm wl be solved

Reply

36 reyjohn June 24, 2010

PLEASE HELP ME!!!!

Can you answer the code of this?
The output is:

Enter the first number:
Enter the second number:
Enter the third number:

The smallest number is:
The middle number is:
The largest number is:

Reply

37 IT_Specialist June 26, 2010

#include
main()
{
int x,y,z;
clrscr();
printf(“Enter the first number:”);
scanf(“%d”,&x);
printf(“Enter the second number:”);
scanf(“%d”,&y);
printf(“Enter the third number:”);
scanf(“%d”,&z);
z=x+y+z;
y=x+y+z;
x=x+y+z;
printf(“The smallest number is:%d\n”,z);
printf(“The middle number is:%d\n”,y);
printf(“The largest number is:%d\n”,x);
getch();
return 0;
}

Reply

38 arlyn July 5, 2010

can you make a program in this question?

plss reply at my email address, asap!!!

Objective:To learn how to write a C++ program using switch and do-while loop as applied to a simple airline application.

Procedure:

Write a program that will validate the status code so that only values from 0 to 3 will be accepted.If an invalid status code is entered, display the message “Invalid Status” and repeatedly accept a status code until a valid value is entered.Repeatedly accept passenger data prompting for “More Entries? (Y/N):” each time a set of data is entered.Terminate processing when response to this prompt is ‘N’.

Note: for string values you need to use strcpy function to assign the value entered to a character array variable (refer to your manual).

Name: XXXXXXXXXXXXXXXXX

Status:XChecked-in

Fare:9,999.99

Specifications:

1.The name of student and exercise no. should appear on screen.

2.Segregate input, process, validation and output statements by introducing void functions

3.Maintain the same display format as follows taking consideration of the error message and the “More Entries” prompt.

Name: XXXXXXXXXXXXXXXXX

Status :XBoarded

Fare:9999.99

More Entries? (Y/N):

4.Use the switch statement to display the corresponding descriptive status.

Statusdescription

0On-Schedule

1 Checked-In

2 Boarded

3 Arrived

Reply

39 Dyulokdyuti July 8, 2010

Thanx for help my study

Reply

40 angel August 16, 2010

Make a C program that will accept any integers from 1-12 and displays equivalent month, Example if 3 “March”.

Reply

41 jane krizia hailar August 23, 2010

i need more explanation of if-esle statement also example….

Reply

42 chetan gupta September 14, 2010

how to find square root of a number by using if else in c program?

Reply

43 Jason October 15, 2010

can you make a program that converts the value of binary to decimal, binary to-octal binary-hexa and decimal-binary, decimal-octal, decimal-hexa, and octal-decimal,octal-binary,octal-hexa, and hexa-binary,hexa-decimal,.hexa-octal;
your prompt reply is highly appreciated. thank you in advance.!
god bless..

Reply

44 Queen13 December 14, 2010

-SAMPLE “IF ELSE” PROGRAM-

#include
#include

int main(){
int grade;
clrscr();
printf(“Enter Grade:\n”);
scanf(“%d”, &grade);

if((grade>=80)&&(grade=75)&&(grade<=79))
printf("Nice");

else
printf("Poor");

getche();
return 0;
}

Reply

45 edison layco January 10, 2011

can you make a program in this output?

output
MODE OF PAYMENT
[1] CASH 10% DISCOUNT
[2] TWO-INSTALLMENT 5% INTEREST
[3] THREE-INSTALLMENT 10% INTEREST

ENTER YOUR TUITION FEE: 20,000
CHOOSE MODE OF PAYMENT: 1
THE AMOUNT OF PAYMENT:18,000

this is the output plsss. help me

Reply

46 emi January 10, 2011

can any one solve this:
write a c program tht accept n integear from the user and pass it to the function that print the crossponding day 1=saturday
2=sunday
3=monday
4=tuesday
5=wednesday
6=thursday
7=friday

Reply

47 ratnesh February 6, 2011

void main(){
int i;
switch(i){
case 1:
Printf(”monday”);
case 2:
Printf(”tuesday”);
case 3:
Printf(”wednesday”);
Case 4:
Printf(”thursday”);
.
.
.
Case 7:
Printf(”sunday”);
Default:
Printf(”enter appropriate number”);
}
}

Reply

48 wiwie69 March 3, 2011

can you help me with this…

ADDI, is one of the courier company that provide express service in delivering goods to customers. The tariff are based on total weight and delivery destination. Table below shows the pricelist.
Table 1 : Weight
Total Weight Price per kg

1st kilogram 15.00

The next 4kg ( 2-5kg) 13.00

The next 20kg (6-25 kg) RM 11.00

More than 25 kg RM 10.00

Table 2 : Destination
Destination Rate
Within a state
i. 1-25km
i. RM 5
ii. More than 25 km ii. RM 15
Domestic RM 30

For express delivery (within 1 day delivery), extra RM 25.00 is add.
You are required to write a program that will help customer to calculate the tariff based on information given. Your program shall display:
• Type of goods – documents or parcel
• Quantity
• Total weight
• Destination
• And delivery type – normal or express
• Total price to be paid

Reply

49 joy March 22, 2011

please give me a code using array and function

Reply

50 renu April 2, 2011

i need more informations of c programs.

Reply

51 aklima akther April 21, 2011

can anyone help me to convert binary to octal by using for loop and bit wise operator.
what does bit wise (n>>i &7) mean and in last for loop what does i -= 3 mean.how is it related to convert octal.can anyone please help me this,

Reply

52 tejas May 27, 2011

can you help me in making calc with frem

Reply

53 sagal paudel June 2, 2011

i want more in formation about the if else statement.

Reply

54 ramadhan kabange June 28, 2011

i want to know more abt if else

Reply

55 arpit mishra August 8, 2011

#include
int main(){
int x,y;
printf(“Enter value for x :”);
scanf(“%d”,&x);
printf(“Enter value for y :”);
scanf(“%d”,&y);
if ( x > y ){
printf(“x is large number – %d\n”,x);
}
else{
printf(“y is large number – %d\n”,y);
}
return 0;
}

Reply

56 anver va July 21, 2011

even though compiler says “succes” .i can’t run the program it says elseif statement error

Reply

57 jheremie rochina July 23, 2011

can u help me to get the c code of this….
enter the first no:20
enter the second no:30
selection operation:+,-,/,%

Reply

58 mj calago August 12, 2011

can you give me a program for votation?? dat would also count the votes and declare who will win !!! please??? i’m begging you guys..!!!

Reply

59 Hanna August 22, 2011

Kindly please help me with this:

Write a program that ask the user to input item name, item unit per price and item quantity. Determine the following:

Range(Quantity) Discount
0-50 0%
51-90 1%
90-100 2%
101-500 3%
501-1000 5%
1001-5000 7%
5801-above 10%

Reply

60 vaishali August 27, 2011

1 scan four values from user and find maximum number from four numbers.

2.read any character from user and check whether that character is vowel or not . using conditional operator. (a,e,i,o,u are called vowel)

Reply

61 leecel August 29, 2011

Can you make a c program on this problem please:
-make a c program using “if else statements” where when you enter a month(in small letters), it will display the holidays on that month!
please, email me!

Reply

62 harisha September 14, 2011

i require a c program by using if else statement to print smallest of given four numbers

Reply

63 harisha September 18, 2011

i require a c program by using if else statement to print smallest of given four numbers
please email me

Reply

64 divya s September 19, 2011

i want more in formation about the if else statement.

Reply

65 Monica September 20, 2011

I need help with writing a program that prompts the user to enter two dates and then indicate which date comes earlier on the calendar

Enter first date (mm/dd/yy): 3/6/08
Enter second date (mm/dd/yy): 5/17/07
5/17/07 is earlier than 3/6/08

Your help would be greatly appreciated!!!!!

Reply

66 Peter B. September 26, 2011

Reading everything but the original post made me feel dumber. To everyone:
1) Learn how to spell. and when and when not to capitalize
2) Actually learn something about C before coming here.

Reply

67 mutuzo ruth October 7, 2011

good and interesting

Reply

68 Andrei de Luna October 11, 2011

can you make a program in this output?

output
MODE OF PAYMENT
[1] CASH 10% DISCOUNT
[2] TWO-INSTALLMENT 5% INTEREST
[3] THREE-INSTALLMENT 10% INTEREST

ENTER YOUR TUITION FEE: 20,000
CHOOSE MODE OF PAYMENT: 1
THE AMOUNT OF PAYMENT:18,000

this is the output plsss. help me

Reply

69 Maria Moosa October 12, 2011

write a c program that will get 4 integers and will print out the maximum integer…can anyone solve this?

Reply

70 sumit October 24, 2011

i have some problem’s in if else statements or give me suggestion how i read about it

Reply

71 Utku October 29, 2011

Help me plss i dont run. idont make. pls explore . . .

1. The proper divisors of a positive integer n are those integers, excluding n itself,
that
divide n exactly. For example, the proper divisors of 12 are 1, 2, 3, 4 and 6.
Write a program that inputs a positive integer and outputs its proper divisors.
Sample run :
Input: 8
Output: 1 2 4
2. A program that accepts a set of integers and displays the number of positive and negative
numbers. The program stops when the user enters 0 for the integer value.

Reply

72 Patricia December 8, 2011

help me please. I need to make a program entering someone’s birthday and the current date today and it will identify your age. How??

Reply

73 prabhat December 8, 2011

thankz a lot

Reply

74 hansel January 22, 2012

The number 153 has interesting property, it equals to the sum of the cubes of its digit, that is 13+53+33=1+125+27=153. Construct a C Program that reads in a positive three integer number and determine if the number satisfy the property or not. Accordingly, display the number with a message “SATISFY THE PROPERTY” or “DOES NOT SATISFY THE PROPERTY”.

Reply

75 hansel January 22, 2012

Plss. .help me about this problem. . . .

Reply

76 sangeetha January 25, 2012

i want the program to fing greatest among 4 numbers using nested if else ladder

Reply

77 Anna January 30, 2012

Please help me guys!!!!!!!!!!!!!
I need a program for the sales and inventory system…….ASAP!!!!!!!!!!!
if ever who have a knowledge about the said system please email me at my yahoo account….. annarah_aquipan@yahoo.com
“May God will pay back you for your kindness”……..
Thank you so much!!!!!!!!!!!

Reply

78 enkeli February 4, 2012

pls help me

pls create a program that will accept 5 integers and display the lowest integer

pls use nested if for c++/turbo c

thanks..

Reply

79 enkeli February 4, 2012

pls create a program that will accept 5 integers and display the highest and the lowest number

any conditional statement in c++/turbo c

thanks..

Reply

80 skret February 9, 2012
81 skret February 9, 2012

some one please help me on my task, i need to write a program that can read 10 integer and then display the:

>biggest number
>smallest number
>total
>average

Reply

82 Reccalyn Castillo February 9, 2012

–>> i badly need the answer regarding this program, can you help me:))

Write a program that complete the users prompt a services of numbers the program must be determine the odd and even number.. prompt by the users ; also sum the odd and even numbers.

Reply

83 shiva February 9, 2012

Nice to see this site.
am learning c language from this cyberciti.biz web site. its really good to learn c for freshers.

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 8 + 3 ?
Please leave these two fields as-is:
Solve the simple math so we know that you are a human and not a bot.




Previous post:

Next post: