Вы находитесь на странице: 1из 20

If else statement in C Program

http://www.cyberciti.biz/faq/if-else-statement-in-c-program/

About Blog Forum Low graphics Shell Scripts RSS/Feed

INDIAN RAILWAYS PASSENGER RESERVATION ENQ


Rail Logo Linux FAQ / Howtos
Passengers are requested to carry ID proof in upper classes(First AC,Second AC,Third while undertaking the journey w.e.f. 15-02-2012

If else statement in C Program


by Vivek Gite on August 16, 2007 88 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:

1 of 20

3/6/2012 9:02 PM

If else statement in C Program

http://www.cyberciti.biz/faq/if-else-statement-in-c-program/

20 Linux System Monitoring Tools Every SysAdmin Should Know 20 Linux Server Hardening Security Tips My 10 UNIX Command Line Mistakes Linux: 20 Iptables Examples For New SysAdmins 25 PHP Security Best Practices For Sys Admins The Novice Guide To Buying A Linux Laptop 10 Greatest Open Source Software Of 2009 Top 5 Email Client For Linux, Mac OS X, and Windows Users Top 20 OpenSSH Server Best Security Practices Top 10 Open Source Web-Based Project Management Software Top 5 Linux Video Editor Software Share this with other sys admins! 1 Facebook it - Tweet it - Print it { 88 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);

2 of 20

3/6/2012 9:02 PM

If else statement in C Program

http://www.cyberciti.biz/faq/if-else-statement-in-c-program/

getch(); } Reply 4 twinkle September 2, 2011 require more explanation and examples Reply 5 Ratnesh February 12, 2012 if statements is a conditional statements that executes on a particular condition the syntax is if (true) /* Here true means if(12>10) ,this is true so it will execute. */ { execute this code } else { else execute this code } Reply 6 affrin January 2, 2012 i need the answer for small and greatest number in 10 numbers Reply 7 nazmul January 27, 2009 I need more if else program.i have simple problem.i dont know your wbsite is active or not.next time i give a problem please solve it. Reply 8 ankita May 5, 2010 I need more program about if else Reply 9 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");

3 of 20

3/6/2012 9:02 PM

If else statement in C Program

http://www.cyberciti.biz/faq/if-else-statement-in-c-program/

} Reply 10 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 11 tyaiba August 25, 2011 why we use y%4.show me answer Reply 12 adil August 26, 2011 y%4 that means which value is store in y that is divided by 4 % is a modular Reply 13 azizur rahman October 7, 2011 the year divided by 4, get remainder zero ,is leap year.

4 of 20

3/6/2012 9:02 PM

If else statement in C Program

http://www.cyberciti.biz/faq/if-else-statement-in-c-program/

Reply 14 Ratnesh February 12, 2012 void main() { int num,grenum; grenum=10; printf(Enter the number: ); scanf(%d,&num); if(num>gernum) { printf(The number is Greater); } else{ printf(The number is smaller); } } Reply 15 aldrin March 22, 2009 can you convert this to java program? Reply 16 saviye.saviyola April 21, 2009 I just study programming . Reply 17 alyssa mae cruz July 6, 2009 hello all..exiting ang programming nuh?haha Reply 18 markjasonarceo September 21, 2009 how to analyze the problem to make c program?tnx Reply 19 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 20 Pooja Sree December 3, 2009 Super Example For If Statement Reply 21 allen joyce December 15, 2009 im excited to try this program

5 of 20

3/6/2012 9:02 PM

If else statement in C Program

http://www.cyberciti.biz/faq/if-else-statement-in-c-program/

Reply 22 allen joyce December 15, 2009 i need to create program right now for my project .help me hehehe using IF ELSE STATEMENT Reply 23 allen joyce December 15, 2009 Give me another example of IF ELSE STATEMENT Reply 24 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=Agrade 60-74=Bgrade 50-59=cgrade 40-49=Dgrade Below 40=Fail thanks . please send it to my Email bayan321@live.com Reply 25 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 26 sujesha October 2, 2010 did you manage to get the answer? did anyone mailed you? thanks sujesha Reply 27 riyas October 31, 2010 void main() { float mark; clrscr();

6 of 20

3/6/2012 9:02 PM

If else statement in C Program

http://www.cyberciti.biz/faq/if-else-statement-in-c-program/

printf(Enter the total mark out of 100); scanf(%f,&mark); if(mark<==75) { print{"Student has secured A Grade "; } else { . . . . . . getch(); } Reply 28 myrabelle cabriana March 8, 2011 thank u.. Reply 29 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 30 ali March 3, 2011 Hello make sure you use the proper braces before and after else statement. Reply 31 Glenn February 3, 2010 can you give more examples :-) Reply 32 Levi February 18, 2010 Thanks I love you!! Reply 33 mabel March 6, 2010 pls. give me a program example of official receipt of sm using if else statement pls. we need it badlyplsthanx

7 of 20

3/6/2012 9:02 PM

If else statement in C Program

http://www.cyberciti.biz/faq/if-else-statement-in-c-program/

Reply 34 Darren March 9, 2010 why cant I place another if statement inside and if statement? Reply 35 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 36 venkata sai November 12, 2010 can you give me the def of if family statements Reply 37 Muneeb zargar June 1, 2010 Use indentations ur prblm wl be solved Reply 38 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 39 IT_Specialist June 26, 2010 #include main() { int x,y,z; clrscr(); printf(Enter the first number:);

8 of 20

3/6/2012 9:02 PM

If else statement in C Program

http://www.cyberciti.biz/faq/if-else-statement-in-c-program/

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 40 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.

9 of 20

3/6/2012 9:02 PM

If else statement in C Program

http://www.cyberciti.biz/faq/if-else-statement-in-c-program/

Statusdescription 0On-Schedule 1 Checked-In 2 Boarded 3 Arrived Reply 41 Dyulokdyuti July 8, 2010 Thanx for help my study Reply 42 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 43 jane krizia hailar August 23, 2010 i need more explanation of if-esle statement also example. Reply 44 chetan gupta September 14, 2010 how to find square root of a number by using if else in c program? Reply 45 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 46 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))

10 of 20

3/6/2012 9:02 PM

If else statement in C Program

http://www.cyberciti.biz/faq/if-else-statement-in-c-program/

printf("Nice"); else printf("Poor"); getche(); return 0; } Reply 47 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 48 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 49 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:

11 of 20

3/6/2012 9:02 PM

If else statement in C Program

http://www.cyberciti.biz/faq/if-else-statement-in-c-program/

Printf(sunday); Default: Printf(enter appropriate number); } } Reply 50 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 51 joy March 22, 2011 please give me a code using array and function Reply 52 renu April 2, 2011 i need more informations of c programs. Reply 53 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
12 of 20

3/6/2012 9:02 PM

If else statement in C Program

http://www.cyberciti.biz/faq/if-else-statement-in-c-program/

octal.can anyone please help me this, Reply 54 tejas May 27, 2011 can you help me in making calc with frem Reply 55 sagal paudel June 2, 2011 i want more in formation about the if else statement. Reply 56 ramadhan kabange June 28, 2011 i want to know more abt if else Reply 57 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 58 anver va July 21, 2011 even though compiler says succes .i cant run the program it says elseif statement error Reply 59 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 60 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??? im begging you guys..!!!

13 of 20

3/6/2012 9:02 PM

If else statement in C Program

http://www.cyberciti.biz/faq/if-else-statement-in-c-program/

Reply 61 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 62 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 63 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 64 harisha September 14, 2011 i require a c program by using if else statement to print smallest of given four numbers Reply 65 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 66 divya s September 19, 2011 i want more in formation about the if else statement. Reply 67 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

14 of 20

3/6/2012 9:02 PM

If else statement in C Program

http://www.cyberciti.biz/faq/if-else-statement-in-c-program/

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 68 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 69 mutuzo ruth October 7, 2011 good and interesting Reply 70 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 71 Maria Moosa October 12, 2011 write a c program that will get 4 integers and will print out the maximum integercan anyone solve this? Reply 72 sumit October 24, 2011 i have some problems in if else statements or give me suggestion how i read about it Reply 73 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 :

15 of 20

3/6/2012 9:02 PM

If else statement in C Program

http://www.cyberciti.biz/faq/if-else-statement-in-c-program/

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 74 Patricia December 8, 2011 help me please. I need to make a program entering someones birthday and the current date today and it will identify your age. How?? Reply 75 prabhat December 8, 2011 thankz a lot Reply 76 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 77 hansel January 22, 2012 Plss. .help me about this problem. . . . Reply 78 sangeetha January 25, 2012 i want the program to fing greatest among 4 numbers using nested if else ladder Reply 79 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 80 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..

16 of 20

3/6/2012 9:02 PM

If else statement in C Program

http://www.cyberciti.biz/faq/if-else-statement-in-c-program/

Reply 81 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 82 skret February 9, 2012 can Reply 83 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 84 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 85 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 86 hassan February 10, 2012 write a program that inputs movie and displays his movie according ti following character ; s sorrow movie r romentic movie a action movie s scientific movie f funny movie Reply 87 swapna yadav February 17, 2012 can anyone say me y do v use == instead =

17 of 20

3/6/2012 9:02 PM

If else statement in C Program

http://www.cyberciti.biz/faq/if-else-statement-in-c-program/

Reply 88 Ahsin Khan February 21, 2012 I want to make a program to find out largest and smallest numbers out of 5 numbers using only 10 if statements and 7 printf statementsis there any one who can solve itplease let make it solve quickly its assignment!!!! Reply Leave a Comment Name * E-mail * Website

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title=""> Notify me of followup comments via e-mail Security Question:
What is 9 + 9 ?
Solve the simple math so we know that you are a human and not a bot.

Ads by Google

C Programming

Example

Turbo C

C Programs

Tagged as: c program, gnu linux, if else syntax, linux gcc, tc

Previous post: Redhat enterprise Linux / CentOS installing kernel source code Next post: Linux / Unix: Checking Free Disk Space

GET FREE TIPS


Make the most of Linux Sysadmin work!

42000+ Subscribers | Twitter | Google +

18 of 20

3/6/2012 9:02 PM

If else statement in C Program

http://www.cyberciti.biz/faq/if-else-statement-in-c-program/

Find us on Facebook

nixCraft
Like 19,641 people like nixCraft.

Stephen

Niraj

Sanjeev

Asim

Shu

Naveen

Okkar

Chandra

Dylan

Ngoc

Facebook social plugin

19 of 20

3/6/2012 9:02 PM

If else statement in C Program

http://www.cyberciti.biz/faq/if-else-statement-in-c-program/

Related FAQs
HowTo: Use Oracle / MySQL SQL Commands In UNIX Shell Scripts Howto: Call Shell Script function In echo statement Bash While Loop Example BIND: Stop Recursion DNS Under Linux / UNIX How do I enable SQL *Plus Up and down arrow keys?

2006-2012 nixCraft. All rights reserved. Cannot be reproduced without written permission. Privacy Policy | Terms of Service | Questions or Comments | Sitemap

20 of 20

3/6/2012 9:02 PM