C Programming If, If..Else And Nested If...Else Statement
Decision Making Are Needed When, The Program Encounters The Situation To Choose A Particular Statement Among Many Statements. In C, Decision Making Can Be Performed With Following Two Statements.
1. If...Else Statement
2. Switch Statement
If Statement Syntax
If (Test Expression){
Statement/S To Be Executed If Test Expression Is True;
}
If The Test Expression Is True Then, Statements For The Body If, I.E, Statements Inside Parenthesis Are Executed. But, If The Test Expression Is False, The Execution Of The Statements For The Body Of If Statements Are Skipped.
Flowchart Of If Statement
Example Of If Statement
Write A C Program To Print The Number Entered By User Only If The Number Entered Is Negative.
#include<stdio.h>
#include<conio.h>
Int Main(){
Int Num;
Printf("Enter A Number To Check.\N");
Scanf("%D",&Num);
If(Num<0) /* Checking Whether Number Is Less Than 0 Or Not. */
Printf("Number=%D\N",Num);
/*If Test Condition Is True, Statement Above Will Be Executed, Otherwise It Will Not Be Executed */
Printf("The If Statement In C Programming Is Easy.");
Return 0;
}
Output 1
Enter A Number To Check.
-2
Number=-2
The If Statement In C Programming Is Easy.
When User Enters -2 Then, The Test Expression (Num<0) Becomes True. Hence, Number=-2 Is Displayed In The Screen.
Output 2
Enter A Number To Check.
5
The If Statement In C Programming Is Easy.
When The User Enters 5 Then, The Test Expression (Num<0) Becomes False. So, The Statement For Body Of If Is Skipped And Only The Statement Below It Is Executed.
If...Else Statement
The If...Else Statement Is Used, If The Programmer Wants To Execute Some Code, If The Test Expression Is True And Execute Some Other Code If The Test Expression Is False.
Syntax Of If...Else
If (Test Expression)
Statements To Be Executed If Test Expression Is True;
Else
Statements To Be Executed If Test Expression Is False;
Flowchart Of If...Else Statement
Example Of If...Else Statement
Write A C Program To Check Whether A Number Entered By User Is Even Or Odd
#include<stdio.h>
#include<conio.h>
Int Main(){
Int Num;
Printf("Enter A Number You Want To Check.\N");
Scanf("%D",&Num);
If((Num%2)==0) //Checking Whether Remainder Is 0 Or Not.
Printf("%D Is Even.",Num);
Else
Printf("%D Is Odd.",Num);
Return 0;
}
Output 1
Enter A Number You Want To Check.
25
25 Is Odd.
Output 2
Enter A Number You Want To Check.
2
2 Is Even.
Nested If...Else Statement (If...Elseif....Else Statement)
The If...Else Statement Can Be Used In Nested Form When A Serious Decision Are Involved.
Syntax Of Nested If...Else Statement.
If (Test Expression)
Statements To Be Executed If Test Expression Is True;
Else
If(Test Expression 1)
Statements To Be Executed If Test Expressions 1 Is True;
Else
If (Test Expression 2)
.
.
.
Else
Statements To Be Executed If All Test Expressions Are False;
How Nested If...Else Works?
If The Test Expression Is True, It Will Execute The Code Before Else Part But, If It Is False, The Control Of The Program Jumps To The Else Part And Check Test Expression 1 And The Process Continues. If All The Test Expression Are False Then, The Last Statement Is Executed.
The Ansi Standard Specifies That 15 Levels Of Nesting May Be Continued.
Example Of Nested If Else Statement
Write A C Program To Relate Two Integers Entered By User Using = Or > Or < Sign.
#include<stdio.h>
#include<conio.h>
Int Main(){
Int Numb1, Numb2;
Printf("Enter Two Integers To Check".\N);
Scanf("%D %D",&Numb1,&Numb2);
If(Numb1==Numb2) //Checking Whether Two Integers Are Equal.
Printf("Result: %D=%D",Numb1,Numb2);
Else
If(Numb1>Numb2) //Checking Whether Numb1 Is Greater Than Numb2.
Printf("Result: %D>%D",Numb1,Numb2);
Else
Printf("Result: %D>%D",Numb2,Numb1);
Return 0;
}
Output 1
Enter Two Integers To Check.
5
3
Result: 5>3
Output 2
Enter Two Integers To Check.
-4
-4
Result: -4=-4
More Examples On If...Else Statement
• - Check Whether An Alphabet Is Vowel Or Consonant
• - Program To Find Largest Number Among Three Numbers
• - Program To Compute Roots Of Quadratic Equation
• - Program To Check Leap Year
C Programming Loops
Loops Causes Program To Execute The Certain Block Of Code Repeatedly Until Some Conditions Are Satisfied, I.E.,Loops Are Used In Performing Repetitive Work In Programming.
Suppose You Want To Execute Some Code/S 100 Times. You Can Perform It By Writing That Code/S Only One Time And Repeat The Execution 100 Times Using Loop.
There Are 3 Types Of Loops In C Programming:
1. For Loop
2. While Loop
3. Do...While Loop
For Loop Syntax
For(Initial Expression; Test Expression; Update Expression)
{
Code/S To Be Executed;
}
How For Loop Works In C Programming?
The Initial Expression Is Initialized Only Once At The Beginning Of The For Loop. Then, The Test Expression Is Checked By The Program. If The Test Expression Is False, For Loop Is Terminated. But, If Test Expression Is True Then, The Codes Are Executed And Update Expression Is Updated. Again, The Test Expression Is Checked. If It Is False, Loop Is Terminated And If It Is True, The Same Process Repeats Until Test Expression Is False.
This Flowchart Describes The Working Of For Loop In C Programming.
For Loop Example
Write A Program To Find The Sum Of First N Natural Numbers Where N Is Entered By User. Note: 1,2,3... Are Called Natural Numbers.
#include<stdio.h>
#include<conio.h>
Int Main(){
Int N, Count, Sum=0;
Printf("Enter The Value Of N.\N");
Scanf("%D",&N);
For(Count=1;Count<=N;++Count) //For Loop Terminates If Count>N
{
Sum+=Count; /* This Statement Is Equivalent To Sum=Sum+Count */
}
Printf("Sum=%D",Sum);
Return 0;
}
Output
Enter The Value Of N.
19
Sum=190
In This Program, The User Is Asked To Enter The Value Of N. Suppose You Entered 19 Then, Count Is Inatialized To 1 At First. Then, The Test Expression In The For Loop,I.E., (Count<= N) Becomes True. So, The Code In The Body Of For Loop Is Executed Which Makes Sum To 1. Then, The Expression ++Count Is Executed And Again The Test Expression Is Checked, Which Becomes True. Again, The Body Of For Loop Is Executed Which Makes Sum To 3 And This Process Continues. When Count Is 20, The Test Condition Becomes False And The For Loop Is Terminated.
C For Loop Examples
• - Program To Generate Multiplication Table
• - Program To Display All Factors Of A Number
• - Program To Find G.C.D Or H.C.F Of Two Numbers
• - Program To Calculate Factorial Of A Number
C Programming Loops
Loops Causes Program To Execute The Certain Block Of Code Repeatedly Until Some Conditions Are Satisfied, I.E., Loops Are Used In Performing Repetitive Work In Programming.
Suppose You Want To Execute Some Code/S 10 Times. You Can Perform It By Writing That Code/S Only One Time And Repeat The Execution 10 Times Using Loop.
There Are 3 Types Of Loops In C Programming:
1. For Loop
2. While Loop
3. Do...While Loop
Syntax Of While Loop
While (Test Expression)
{
Statements To Be Executed.
}
In The Beginning Of While Loop, Test Expression Is Checked. If It Is True, Codes Inside The Body Of While Loop,I.E, Code/S Inside Parentheses Are Executed And Again The Test Expression Is Checked And Process Continues Until The Test Expression Becomes False.
Example Of While Loop
Write A C Program To Find The Factorial Of A Number, Where The Number Is Entered By User. (Hints: Factorial Of N = 1*2*3*...*N
/*C Program To Demonstrate The Working Of While Loop*/
#include<stdio.h>
#include<conio.h>
Int Main(){
Int Number,Factorial;
Printf("Enter A Number.\N");
Scanf("%D",&Number);
Factorial=1;
While (Number>0){ /* While Loop Continues Util Test Condition Number>0 Is True */
Factorial=Factorial*Number;
--Number;
}
Printf("Factorial=%D",Factorial);
Return 0;
}
Output
Enter A Number.
5
Factorial=120
Do...While Loop
In C, Do...While Loop Is Very Similar To While Loop. Only Difference Between These Two Loops Is That, In While Loops, Test Expression Is Checked At First But, In Do...While Loop Code Is Executed At First Then The Condition Is Checked. So, The Code Are Executed At Least Once In Do...While Loops.
Syntax Of Do...While Loops
Do {
Some Code/S;
}
While (Test Expression);
At First Codes Inside Body Of Do Is Executed. Then, The Test Expression Is Checked. If It Is True, Code/S Inside Body Of Do Are Executed Again And The Process Continues Until Test Expression Becomes False(Zero).
Notice, There Is Semicolon In The End Of While (); In Do...While Loop.
Example Of Do...While Loop
Write A C Program To Add All The Numbers Entered By A User Until User Enters 0.
/*C Program To Demonstrate The Working Of Do...While Statement*/
#include<stdio.h>
#include<conio.h>
Int Main(){
Int Sum=0,Num;
Do /* Codes Inside The Body Of Do...While Loops Are At Least Executed Once. */
{
Printf("Enter A Number\N");
Scanf("%D",&Num);
Sum+=Num;
}
While(Num!=0);
Printf("Sum=%D",Sum);
Return 0;
}
Output
Enter A Number
3
Enter A Number
-2
Enter A Number
0
Sum=1
In This C Program, User Is Asked A Number And It Is Added With Sum. Then, Only The Test Condition In The Do...While Loop Is Checked. If The Test Condition Is True,I.E, Num Is Not Equal To 0, The Body Of Do...While Loop Is Again Executed Until Num Equals To Zero.
C While Loops Examples
• - Program To Reverse A Integer
• - Program To Calculate The Power Of A Number
• - Program To Check Armstrong Number
C Programming Break And Continue Statement
There Are Two Statement Built In C, Break; And Continue; To Interrupt The Normal Flow Of Control Of A Program. Loops Performs A Set Of Operation Repeately Until Certain Condition Becomes False But, It Is Sometimes Desirable To Skip Some Statements Inside Loop And Terminate The Loop Immediately Without Checking The Test Expression. In Such Cases, Break And Continue Statements Are Used.
Break Statement
In C Programming, Break Is Used In Terminating The Loop Immediately After It Is Encountered. The Break Statement Is Used With Conditional If Statement.
Syntax Of Break Statement
Break;
The Break Statement Can Be Used In Terminating All Three Loops For, While And Do...While Loops.
The Figure Below Explains The Working Of Break Statement In All Three Type Of Loops.
Example Of Break Statement
Write A C Program To Find Average Of Maximum Of N Positive Numbers Entered By User. But, If The Input Is Negative, Display The Average(Excluding The Average Of Negative Input) And End The Program.
/* C Program To Demonstrate The Working Of Break Statement By Terminating A Loop, If User Inputs Negative Number*/
#include<stdio.h>
#include<conio.h>
Int Main(){
Float Num,Average,Sum;
Int I,N;
Printf("Maximum No. Of Inputs\N");
Scanf("%D",&N);
For(I=1;I<=N;++I){
Printf("Enter N%D: ",I);
Scanf("%F",&Num);
If(Num<0.0)
Break; //For Loop Breaks If Num<0.0
Sum=Sum+Num;
}
Average=Sum/(I-1);
Printf("Average=%.2f",Average);
Return 0;
}
Output
Maximum No. Of Inputs
4
Enter N1: 1.5
Enter N2: 12.5
Enter N3: 7.2
Enter N4: -1
Average=7.07
In This Program, When The User Inputs Number Less Than Zero, The Loop Is Terminated Using Break Statement With Executing The Statement Below It I.E., Without Executing Sum=Sum+Num.
In C, Break Statements Are Also Used In Switch...Case Statement. You Will Study It In C Switch...Case Statement Chapter.
Continue Statement
It Is Sometimes Desirable To Skip Some Statements Inside The Loop. In Such Cases, Continue Statements Are Used.
Syntax Of Continue Statement
Continue;
Just Like Break, Continue Is Also Used With Conditional If Statement.
For Better Understanding Of How Continue Statements Works In C Programming. Analyze The Figure Below Which Bypasses Some Code/S Inside Loops Using Continue Statement.
Example Of Continue Statement
Write A C Program To Find The Product Of 4 Integers Entered By A User. If User Enters 0 Skip It.
//Program To Demonstrate The Working Of Continue Statement In C Programming
#include<stdio.h>
#include<conio.h>
Int Main(){
Int I,Num,Product;
For(I=1,Product=1;I<=4;++I){
Printf("Enter Num%D:",I);
Scanf("%D",&Num);
If(Num==0)
Continue; / *In This Program, When Num Equals To Zero, It Skips The Statement Product*=Num And Continue The Loop. */
Product*=Num;
}
Printf("Product=%D",Product);
Return 0;
}
Output
Enter Num1:3
Enter Num2:0
Enter Num3:-5
Enter Num4:2
Product=-30
C Programming Switch....Case Statement
Decision Making Are Needed When, The Program Encounters The Situation To Choose A Particular Statement Among Many Statements. If A Programmar Has To Choose One Among Many Alternatives If...Else Can Be Used But, This Makes Programming Logic Complex. This Type Of Problem Can Be Handled In C Programming Using Switch...Case Statement.
Syntax Of Switch...Case
Switch (Expression)
{
Case Constant1:
Codes To Be Executed If Expression Equals To Constant1;
Break;
Case Constant2:
Codes To Be Executed If Expression Equals To Constant3;
Break;
.
.
.
Default:
Codes To Be Executed If Expression Doesn't Match To Any Cases;
}
In Switch...Case, Expression Is Either An Integer Or A Character. If The Value Of Switch Expression Matches Any Of The Constant In Case, The Relevant Codes Are Executed And Control Moves Out Of The Switch...Case Statement. If The Expression Doesn't Matches Any Of The Constant In Case, Then The Default Statement Is Executed.
Example Of Switch...Case Statement
Write A Program That Asks User An Arithmetic Operator('+','-','*' Or '/') And Two Operands And Perform The Corresponding Calculation On The Operands.
/* C Program To Demonstrate The Working Of Switch...Case Statement */
/* Program To Create A Simple Calculator For Addition, Subtraction, Multiplication And Division */
#include<stdio.h>
#include<conio.h>
Int Main(){
Char Operator;
Float Num1,Num2;
Printf("Enter Operator +, - , * Or / :\N");
Operator=Getche();
Printf("\Nenter Two Operands:\N");
Scanf("%F%F",&Num1,&Num2);
Switch(Operator)
{
Case '+':
Printf("Num1+Num2=%.2f",Num1+Num2);
Break;
Case '-':
Printf("Num1-Num2=%.2f",Num1-Num2);
Break;
Case '*':
Printf("Num1*Num2=%.2f",Num1*Num2);
Break;
Case '/':
Printf("Num2/Num1=%.2f",Num1/Num2);
Break;
Default:
/* If Operator Is Other Than +, -, * Or /, Error Message Is Shown */
Printf(Error! Operator Is Not Correct");
Break;
}
Return 0;
}
Output
Enter Operator +, -, * Or / :
/
Enter Two Operators:
34
3
Num2/Num1=11.33
Notice Break Statement At The End Of Each Case, Which Cause Switch...Case Statement To Exit. If Break Statement Are Not Used, All Statements Below That Case Statement Are Also Executed.
C Programming Goto Statement
In C Programming, Goto Statement Is Used For Altering The Normal Sequence Of Program Execution By Transferring Control To Some Other Part Of The Program.
Syntax Of Goto Statement
Goto Label;
.............
.............
.............
Label:
Statement;
In This Syntax, Label Is An Identifier. When, The Control Of Program Reaches To Goto Statement, The Control Of The Program Will Jump To The Label: And Executes The Code/S After It.
Example Of Goto Statement
/* C Program To Demonstrate The Working Of Goto Statement.*/
#include<stdio.h>
#include<conio.h>
Int Main(){
Float Num,Average,Sum;
Int I,N;
Printf("Maximum No. Of Inputs: ");
Scanf("%D",&N);
For(I=1;I<=N;++I){
Printf("Enter N%D: ",I);
Scanf("%F",&Num);
If(Num<0.0)
Goto Jump; /* Control Of The Program Jumps To Label Jump */
Sum=Sum+Num;
}
Jump:
Average=Sum/(I-1);
Printf("Average: %.2f",Average);
Return 0;
}
Output
Maximum No. Of Inputs: 4
Enter N1: 1.5
Enter N2: 12.5
Enter N3: 7.2
Enter N4: -1
Average: 7.07
Though Goto Statement Is Included In Ansi Standard Of C, Use Of Goto Statement Should Be Reduced As Much As Possible In A Program.
Reasons To Avoid Goto Statement
Though, Using Goto Statement Give Power To Jump To Any Part Of Program, Using Goto Statement Makes The Logic Of The Program Complex And Tangled. In Modern Programming, Goto Statement Is Considered A Harmful Construct And A Bad Programming Practice.
The Goto Statement Can Be Replaced In Most Of C Program With The Use Of Break And Continue Statements. In Fact, Any Program In C Programming Can Be Perfectly Written Without The Use Of Goto Statement. All Programmer Should Try To Avoid Goto Statement As Possible As They Can.
Source Code To Check Whether A Number Is Even Or Odd
/*C Program To Check Whether A Number Entered By User Is Even Or Odd. */
#include<stdio.h>
#include<conio.h>
Int Main(){
Int Num;
Printf("Enter An Integer You Want To Check: ");
Scanf("%D",&Num);
If((Num%2)==0) /* Checking Whether Remainder Is 0 Or Not. */
Printf("%D Is Even.",Num);
Else
Printf("%D Is Odd.",Num);
Return 0;
}
Output 1
Enter An Integer You Want To Check: 25
25 Is Odd.
Output 2
Enter An Integer You Want To Check: 12
12 Is Even.
In This Program, User Is Asked To Enter An Integer Which Is Stored In Variable Num. Then, The Remainder Is Found When That Number Is Divided By 2 And Checked Whether Remainder Is 0 Or Not. If Remainder Is 0 Then, That Number Is Even Otherwise That Number Is Odd. This Task Is Performed Using If...Else Statement In C Programming And The Result Is Displayed Accordingly.
This Program Also Can Be Solved Using Conditional Operator[ ?: ] Which Is The Shorthand Notation For If...Else Statement.
/* C Program To Check Whether An Integer Is Odd Or Even Using Conditional Operator */
#include<stdio.h>
#include<conio.h>
Int Main(){
Int Num;
Printf("Enter An Integer You Want To Check: ");
Scanf("%D",&Num);
((Num%2)==0) ? Printf("%D Is Even.",Num) : Printf("%D Is Odd.",Num);
Return 0;
}
The Output Of This Program Same As Above Program.
Source Code To Check Whether A Character Is Vowel Or Consonant
#include<stdio.h>
#include<conio.h>
Int Main(){
Char C;
Printf("Enter An Alphabet: ");
Scanf("%C",&C);
If(C=='A'||C=='A'||C=='E'||C=='E'||C=='I'||C=='I'||C=='O'||C=='O'||C=='U'||C=='U')
Printf("%C Is A Vowel.",C);
Else
Printf("%C Is A Consonant.",C);
Return 0;
}
Output 1
Enter An Alphabet: I
I Is A Vowel.
Output 2
Enter An Alphabet: G
G Is A Consonant.
In This Program, User Is Asked To Enter A Character Which Is Stored In Variable C. Then, This Character Is Checked, Whether It Is Any One Of These Ten Characters A, A, E, E, I, I, O, O, U And U Using Logical Or Operator ||. If That Character Is Any One Of These Ten Characters, That Alphabet Is A Vowel If Not That Alphabet Is A Consonant.
This Program Also Can Be Solved Using Conditional Operator Which Is Shorthand Notation For If Else Statement.
/* C Program To Check Whether A Character Is Vowel Or Consonant Using Conditional Operator */
#include<stdio.h>
#include<conio.h>
Int Main(){
Char C;
Printf("Enter An Alphabet: ");
Scanf("%C",&C);
(C=='A'||C=='A'||C=='E'||C=='E'||C=='I'||C=='I'||C=='O'||C=='O'||C=='U'||C=='U') ? Printf("%C Is A Vowel.",C) : Printf("%C Is A Consonant.",C);
Return 0;
}
Program To Find The Largest Number Among Three Numbers
In This Program, User Is Asked To Enter Three Numbers And This Program Will Find The Largest Number Among Three Numbers Entered By User. This Program Can Be Solved In More Than One Way.
Source Code 1
/* C Program To Find Largest Number Using If Statement Only */
#include<stdio.h>
#include<conio.h>
Int Main(){
Float A, B, C;
Printf("Enter Three Numbers: ");
Scanf("%F %F %F", &A, &B, &C);
If(A>=B && A>=C)
Printf("Largest Number = %.2f", A);
If(B>=A && B>=C)
Printf("Largest Number = %.2f", B);
If(C>=A && C>=B)
Printf("Largest Number = %.2f", C);
Return 0;
}
Source Code 2
/* C Program To Find Largest Number Using If...Else Statement */
#include<stdio.h>
#include<conio.h>
Int Main(){
Float A, B, C;
Printf("Enter Three Numbers: ");
Scanf("%F %F %F", &A, &B, &C);
If (A>=B)
{
If(A>=C)
Printf("Largest Number = %.2f",A);
Else
Printf("Largest Number = %.2f",C);
}
Else
{
If(B>=C)
Printf("Largest Number = %.2f",B);
Else
Printf("Largest Number = %.2f",C);
}
Return 0;
}
Source Code 3
/* C Program To Find Largest Number Using Nested If...Else Statement */
#include<stdio.h>
#include<conio.h>
Int Main(){
Float A, B, C;
Printf("Enter Three Numbers: ");
Scanf("%F %F %F", &A, &B, &C);
If(A>=B && A>=C)
Printf("Largest Number = %.2f", A);
Else If(B>=A && B>=C)
Printf("Largest Number = %.2f", B);
Else
Printf("Largest Number = %.2f", C);
Return 0;
}
Program To Check Whether A Number Is Positive Or Negative Or Zero.
This Program Takes A Number From User And Checks Whether That Number Is Either Positive Or Negative Or Zero.
Source Code
#include<stdio.h>
#include<conio.h>
Int Main()
{
Float Num;
Printf("Enter A Number: ");
Scanf("%F",&Num);
If (Num<=0)
{
If (Num==0)
Printf("You Entered Zero.");
Else
Printf("%.2f Is Negative.",Num);
}
Else
Printf("%.2f Is Positive.",Num);
Return 0;
}
This Program Also Can Be Solved Using Nested If Else Statement.
/* C Programming Code To Check Whether A Number Is Negative Or Positive Or Zero Using Nested If...Else Statement. */
#include<stdio.h>
#include<conio.h>
Int Main()
{
Float Num;
Printf("Enter A Number: ");
Scanf("%F",&Num);
If (Num<0) /* Checking Whether Num Is Less Than 0*/
Printf("%.2f Is Negative.",Num);
Else If (Num>0) /* Checking Whether Num Is Greater Than Zero*/
Printf("%.2f Is Positive.",Num);
Else
Printf("You Entered Zero.");
Return 0;
}
Output 1
Enter A Number: 12.3
12.30 Is Positive.
Output 2
Enter A Number: -12.3
-12.30 Is Negative.
Output 3
Enter A Number: 0
You Entered Zero.
Program To Display Factors Of A Number
This Program Takes A Positive Integer From An User And Displays All The Factors Of That Number.
Source Code To Display Factors Of A Number
/* C To Find And Display All The Factors Of A Number Entered By An User.. */
#include<stdio.h>
#include<conio.h>
Int Main()
{
Int N,I;
Printf("Enter A Positive Integer: ");
Scanf("%D",&N);
Printf("Factors Of %D Are: ", N);
For(I=1;I<=N;++I)
{
If(N%I==0)
Printf("%D ",I);
}
Return 0;
}
Output
Enter A Positive Integer: 60
Factors Of 60 Are: 1 2 3 4 5 6 12 15 20 30 60
Explanation
In This Program, An Integer Entered By User Is Stored In Variable N. Then, For Loop Is Executed With Initial Conditioni=1 And Checked Whether N Is Perfectly Divisible By I Or Not. If N Is Perfectly Divisible By I Then, I Will Be The Factor Of N. In Each Iteration, The Value Of I Is Updated(Increased By 1). This Process Goes Not Until Test Condition I<=Nbecomes False,I.E., This Program Checks Whether Number Entered By User N Is Perfectly Divisible By All Numbers From 1 To N And All Displays Factors Of That Number.
Program To Check Whether A Character Is An Alphabet Or Not
This Program Takes A Character From User And Checks Whether That Character Is An Alphabet Or Not.
Source Code To Check Character Is An Alphabet Or Not
/* C Programming Code To Check Whether A Character Is Alphabet Or Not.*/
#include<stdio.h>
#include<conio.h>
Int Main()
{
Char C;
Printf("Enter A Character: ");
Scanf("%C",&C);
If( (C>='A'&& C<='Z') || (C>='A' && C<='Z'))
Printf("%C Is An Alphabet.",C);
Else
Printf("%C Is Not An Alphabet.",C);
Return 0;
}
Output 1
Enter A Character: *
* Is Not An Alphabet
Output 2
Enter A Character: K
K Is An Alphabet
Explanation
When A Character Is Stored In A Variable, Ascii Value Of That Character Is Stored Instead Of That Character Itself. For Example: If 'A' Is Stored In A Variable, Ascii Value Of 'A' Which Is 97 Is Stored. If You See The Ascii Table, The Lowercase Alphabets Are From 97 To 122 And Uppercase Letter Are From 65 To 90. If The Ascii Value Of Number Stored Is Between Any Of These Two Intervals Then, That Character Will Be An Alphabet. In This Program, Instead Of Number 97, 122, 65 And 90; We Have Used 'A', 'Z', 'A' And 'Z' Respectively Which Is Basically The Same Thing.
This Program Also Can Be Performed Using Standard Library Function Isalpha().
Program To Calculate Sum Of Natural Numbers
Positive Integers 1, 2, 3, 4... Are Known As Natural Numbers. This Program Takes A Positive Integer From User( Suppose User Entered N ) Then, This Program Displays The Value Of 1+2+3+....N.
Source Code To Calculate Sum Of Natural Numbers
/* This Program Is Solved Using While Loop. */
#include<stdio.h>
#include<conio.h>
Int Main()
{
Int N, Count, Sum=0;
Printf("Enter An Integer: ");
Scanf("%D",&N);
Count=1;
While(Count<=N) /* While Loop Terminates If Count>N */
{
Sum+=Count; /* Sum=Sum+Count */
++Count;
}
Printf("Sum = %D",Sum);
Return 0;
}
Source Code To Calculate Sum Using For Loop
/* This Program Is Solve Using For Loop. */
#include<stdio.h>
#include<conio.h>
Int Main()
{
Int N, Count, Sum=0;
Printf("Enter An Integer: ");
Scanf("%D",&N);
For(Count=1;Count<=N;++Count) /* For Loop Terminates If Count>N */
{
Sum+=Count; /* Sum=Sum+Count */
}
Printf("Sum = %D",Sum);
Return 0;
}
Output
Enter An Integer: 100
Sum = 5050
Both Program Above Does Exactly The Same Thing. Initially, The Value Of Count Is Set To 1. Both Program Have Test Condition To Perform Looping Iteration Until Condition Count<=N Becomes False And In Each Iteration ++Count Is Executed. The Only Difference Between For Loop And While Loop Is The Way Of Writing Code.
This Program Works Perfectly For Positive Number But, If User Enters Negative Number Or 0, Sum = 0 Is Displayed But, It Is Better Is Display The Error Message In This Case. The Above Program Can Be Made User-Friendly By Adding If Else Statement As:
/* This Program Displays Error Message When User Enters Negative Number Or 0 And Displays The Sum Of Natural Numbers If User Enters Positive Number. */
#include<stdio.h>
#include<conio.h>
Int Main()
{
Int N, Count, Sum=0;
Printf("Enter An Integer: ");
Scanf("%D",&N);
If ( N<= 0)
Printf("Error!!!!");
Else
{
For(Count=1;Count<=N;++Count) /* For Loop Terminates If Count>N */
{
Sum+=Count; /* Sum=Sum+Count */
}
Printf("Sum = %D",Sum);
}
Return 0;
}
This Program Also Can Be Solved Using Recursive Function.
Factorial Of A Number
For Any Positive Number N, Its Factorial Is Given By:
Factorial = 1*2*3*4....N
If A Number Is Negative, Factorial Does Not Exist And Factorial Of 0 Is 1.
This Program Takes An Integer From A User. If User Enters Negative Integer, This Program Will Display Error Message And If User Enters Non-Negative Integer, This Program Will Display The Factorial Of That Number.
Source Code To Find Factorial Of A Number
/* C Program To Display Factorial Of An Integer If User Enters Non-Negative Integer. */
#include<stdio.h>
#include<conio.h>
Int Main()
{
Int N, Count;
Unsigned Long Long Int Factorial=1;
Printf("Enter An Integer: ");
Scanf("%D",&N);
If ( N< 0)
Printf("Error!!! Factorial Of Negative Number Doesn't Exist.");
Else
{
For(Count=1;Count<=N;++Count) /* For Loop Terminates If Count>N */
{
Factorial*=Count; /* Factorial=Factorial*Count */
}
Printf("Factorial = %Lu",Factorial);
}
Return 0;
}
Output 1
Enter An Integer: -5
Error!!! Factorial Of Negative Number Doesn't Exist.
Output 2
Enter An Integer: 10
Factorial = 3628800
Program To Make A Simple Calculator To Add, Subtract, Multiply Or Divide Using Switch...Case
This Program Takes An Arithmetic Operator (+, -, *, /) And Two Operands From An User And Performs The Operation On Those Two Operands Depending Upon The Operator Entered By User. This Program Can Be Solved Usingswitch....Case Statement As:
Source Code To Make Simple Calculator In C Programming
/* Source Code To Create A Simple Calculator For Addition, Subtraction, Multiplication And Division Using Switch...Case Statement In C Programming. */
#include<stdio.h>
#include<conio.h>
Int Main()
{
Char Operator;
Float Num1,Num2;
Printf("Enter Operator Either + Or - Or * Or Divide : ");
Scanf("%C",&Operator);
Printf("Enter Two Operands: ");
Scanf("%F%F",&Num1,&Num2);
Switch(Operator)
{
Case '+':
Printf("Num1+Num2=%.2f",Num1+Num2);
Break;
Case '-':
Printf("Num1-Num2=%.2f",Num1-Num2);
Break;
Case '*':
Printf("Num1*Num2=%.2f",Num1*Num2);
Break;
Case '/':
Printf("Num2/Num1 = %.2f",Num1/Num2);
Break;
Default:
/* If Operator Is Other Than +, -, * Or /, Error Message Is Shown */
Printf("Error! Operator Is Not Correct");
Break;
}
Return 0;
}
Output
Enter Operator Either + Or - Or * Or Divide: /
Enter Two Operands: 13.456
4.56
Num2/Num1 = 2.95
Explanation
This Program Takes An Operator And Two Operands From User. The Operator Is Stored In Variable Operator And Two Operands Are Stored In Num1 And Num2 Respectively. Then, Switch...Case Statement Is Used For Checking The Operator Entered By User. If User Enters + Then, Statements For Case: '+' Is Executed And Program Is Terminated. If User Enters - Then, Statements For Case: '-' Is Executed And Program Is Terminated. This Program Works Similarly For * And / Operator. But, If The Operator Doesn't Matches Any Of The Four Character [ +, -, * And / ], Default Statement Is Executed Which Displays Error Message.
No comments:
Post a Comment