Case Conversion
Programs
Illustrative Program
for Lower Case to Upper Case.
Program
#include
<stdio.h>
void main()
{
char ch;
printf("\nEnter
any lower case character : ");
scanf("%c",
&ch);
if(ch >= 97
&& ch <= 122)
printf("The
Capital Letter of %c is %c", ch, ch - 'a' + 'A');
else
printf("Sorry! %c
is already in Capitals.", ch);
}
Illustrative Program
for Upper Case to Lower Case.
Program
#include
<stdio.h>
void main()
{
char ch;
printf("\nEnter
any upper case character: ");
scanf("%c",
&ch);
if(ch >= 65
&& ch <= 90)
printf("\nThe
Lower Letter of %c is %c", ch, ch - 'A' + 'a');
else
printf("\nSorry!
%c is already in Lower Case.", ch);
}
Nested ‘if’
Statements
In Nested ‘if’
statements the TRUE State of the ‘if statement’ is followed by
another ‘if
statement’ immediately.
The depth can
continue to any levels as per the requirement.
Nested ‘if’ Syntax:
if (condition)
if (condition)
{
if (condition)
{
Executional
Statements.
}
Else
{
Executional
Statements.
}
}
Sample Programs
Checking the given
Year is a Leap Year or not
Program
#include <stdio.h>
void main()
{
int Year;
printf("\nEnter
the Year : ");
scanf("%d",
&Year);
if(Year % 4 == 0)
{
if(Year % 100 != 0)
{
printf("\n%d is
a Leap Year.\n",Year);
}
else
{
if(Year % 400 == 0)
{
printf("%d is a
Leap Year.\n",Year);
}
else
{
printf("%d is not
a Leap Year.\n",Year);
}
}
}
else
{
printf("\n%d is
not a Leap Year.\n", Year);
}
}
else.. if Ladders or
else.. if Chains:
In this a False state
of the ‘if’ is followed by an anpther ‘if condition’.
else .. if Syntax:
if (condition)
{
Executable Statements.
}
else
if (condition)
Calulator Simulation
Program
#include
<stdio.h>
void main()
{
char op;
int a, b;
printf("\nEnter
the Operator (+, -, *, /) : ");
scanf("%c",
&op);
printf("\nEnter
the values of a and b : ");
scanf("%d%d",
&a, &b);
if(op == '+')
printf("\n%d +
%d = %d", a, b, a + b);
else if(op == '-')
printf("\n%d -
%d = %d", a, b, a - b);
else if(op == '*')
printf("\n%d *
%d = %d", a, b, a * b);
else if(op == '/')
printf("\n%d /
%d = %d", a, b, a / b);
else
printf("\nSorry!
Invalid Operators.");
}
Income Calculation
Program
#include
<stdio.h>
void main()
{
float Mon_Sales,
Income = 0;
printf("\nEnter
Monthly Sales : ");
scanf("%f",
&Mon_Sales);
if(Mon_Sales >=
50000.00)
Income = 375.00 +
(0.16 * Mon_Sales);
else if (Mon_Sales
>= 40000.00)
Income = 350.00 +
(0.14 * Mon_Sales);
else if (Mon_Sales
>= 30000.00)
Income = 325.00 +
(0.12 * Mon_Sales);
else if (Mon_Sales
>= 20000.00)
Income = 300.00 +
(0.09 * Mon_Sales);
else if (Mon_Sales
>= 10000.00)
Income = 250.00 +
(0.05 * Mon_Sales);
else
Income = 200.00 +
(0.03 * Mon_Sales);
printf("\nThe
Income Earned by You %f", Income);
}
Let us take a break
with some Real Cases
Problem Scenario:
Program to calculate electricity charges.
Consumed Units
Charges Applied
0 – 200 - 0.75 Per
Unit.
201 – 400 - 100 + 1 *
Exceeding Units of 200.
401 - 600 - 250 +
1.25 * Exceeding Units of 400.
601 - - 450 + 1.5 *
Exceeding Units of 600.
Program
#include<stdio.h>
void main()
{
int custnum,units;
float amount;
printf("\nEnter
the Customer Number and Number of Units Consumed : ");
scanf("%d%d",
&custnum, &units);
if(units <= 200)
amount = 0.75 *
units;
else if (units <=
400)
amount = 100 + 1 *
(units - 200);
else
if(units<=600)
amount = 250 + 1.25 *
(units - 400);
else
amount = 400 + 1.5 *
(units - 600);
printf("\nCustomer
Number is : %d and Bill Amount is : %f", custnum, amount);
}
Category-1 Category-2
Category-3
Consumed Units and
Charges
0 - 50 →1.0 0 – 100 →3.0
0 – 1000 →3.0
51-100 →2.0 101-200 →6.0
> 1000 →4.0
201-300 →4.0 > 200
→7.0
301-400 →5.0
> 400 →6.0
Program
#include<stdio.h>
void main()
{
int cat,units;
float amount;
printf("\nEnter
Category (1,2,3) and Number of Units Consumed : ");
scanf("%d%d",
&cat, &units);
if(cat == 1)
{
if(units > 0
&& units <= 50)
amount = 1 * units;
else if (units >
50 && units <= 100)
amount = 1 * 50 +
(units - 50) * 2;
else if(units >=
101 && units <= 200)
amount = 1 * 50 + 2 *
50 + (units - 100) * 3;
else if(units >=
201 && units <= 300)
amount = 1 * 50 + 2 *
50 + 3 * 100 +4 * (units - 200);
else if(units >=
301 && units <= 400)
amount = 1 * 50 + 2 *
50 + 3 * 100 + 4 * 100 + (units - 300) * 5;
else if(units >
400)
amount = 1 * 50 + 2 *
50 + 3 * 100 + 4 * 100 + 5 * 100 + (units - 400) * 6;
}
else if(cat==2)
{
if(units > 0
&& units <= 100)
amount = units * 3;
else if(units >=
101 && units <= 200)
amount = 3 * 100 +
(units - 100) * 6;
else if(units >
200)
amount = 3 * 100 + 6 *
100 +(units -200) * 7;
}
else if(cat==3)
{
if(units > 0
&& units <= 1000)
amount = units * 3;
else if(units >
1000)
amount = 3 * 1000
+(units - 1000) * 4;
}
printf("\n\nYour
Bill Amount : %f", amount);
}
Problem Scenario:
Program to calculate Employee Loan.
Category Marital
Status Service Loan
Permanent(P)
Unmarried(U) >=30 50,000
Else 25,000
Married(M) >=30 40,000
Else 20,000
Temporary
Married/Unmarried -N.A.- 10,000
Program 33
#include<stdio.h>
void main()
{
char cat, marital;
int service;
float loan;
printf("\nEnter
Category (p,t), Marital Status (m,u): ");
scanf("%c
%c", &cat, &marital);
if(cat == 'p')
{
printf("\nEnter
tha Number of Years of Serice : ");
scanf("%d",
&service);
if(marital == 'u')
{
if(service >= 30)
loan = 50000;
else
loan = 25000;
}
else if (marital ==
'm')
{
if(service >= 30)
loan = 40000;
else
loan = 20000;
}
}
else if(cat == 't')
loan = 10000;
printf("\nThe
amount of loan sanctioned to the Employee is : %f", loan);
}
Problem Scenario:
Program to calculate Sales Commission.
Item Salesamount Rate
of commission
Cpu <10,000
NIL>=10,000 and <25,000 8% on Sale Amount >25,000
Rs2000+10% on sales
Amount in exess of 25,000 Moniter <10,000 5% on Sale Amount>=10,000 5%
upto 10,000+ 8% above
Program
#include<stdio.h>
void main()
{
char item;
float sale_amount,
comm;
printf("\nEnter
the Type of Item (C = CPU, M = MONITOR) : ");
scanf("%c",
&item);
printf("\nPlease
Enter the Sale Amount : ");
scanf("%f",
&sale_amount);
if(item == 'C')
{
if(sale_amount <
10000)
comm = 0;
else if(sale_amount
>= 10000 && sale_amount < 25000)
comm = sale_amount *
0.08;
else
if(sale_amount>=25000)
comm = 2000 + (sale_amount
- 25000) * 0.1;
}
else if(item == 'M')
{
if( sale_amount <
10000)
comm = sale_amount *
0.05;
else
comm = 10000 * 0.05 +
(sale_amount - 10000) * 0.05;
}
printf("\nItem
\t\t Sale Amount \t\t Commission\n");
if(item == 'C')
printf("\nCPU");
else if(item == 'M')
printf("\nMonitor");
printf("\t\t%0.2f\t\t%0.2f",
sale_amount, comm);
}
Selection Principle
in Detail
switch-case Statement
The switch statement
causes a particular group of statements to be chosen
from available groups
of statements.
The SELECTION depends
upon the value provided under ‘switch’.
Each value provided
under the switch is associated with a proper ‘case’
Label.
Which ever Case Label
is matched, the associated statements under that
Case Label are
executed.
Syntax:
switch (expression/condition/constant)
{
case Label1 :
Statement1;
case Label2 :
Statement2;.
case Label3 :
Statement3;
case LabelN :
Statementn;
default :
DefaultStatement;
}
For each alternative,
the first statement written in the group must be
preceded by one or
more ‘case’ labels.
The case labels must
be unique within a given switch statement.
The ‘default’ is not
compulsory but is recommended.
Break Statement
It is a statement
used in Control Structures to terminate a Brach or Exit
from switch or Loop.
Syntax: break;
Illustrative Programs
Program
#include<stdio.h>
void main()
{
char ch;
printf("\nEnter
the Color Choice ('r' or 'R', 'w' or 'W', 'b' or 'B') : ");
scanf("%c",
&ch);
switch(ch)
{
case 'r':
case 'R':
printf("\nYour
Choice was Red.");
break;
case 'w':
case 'W':
printf("\nYour
Choice was White.");
break;
case 'b':
case 'B':
printf("\nYour
Choice was Blue.");
break;
}
}
Calculator simulator
program
Program
#include<stdio.h>
void main()
{
int a,b;
char op;
printf("\nEnter
the Operator (+, -, /, *) : ");
scanf("%c",
&op);
printf("\nEnter
any two numbers:");
scanf("%d%d",
&a, &b);
switch(op)
{
case '+':
printf("\n%d +
%d = %d", a, b, a + b);
break;
case '-':
printf("\n%d -
%d = %d", a, b, a - b);
break;
case '*':
printf("\n%d *
%d = %d", a, b, a * b);
break;
case '/':
printf("\n%d /
%d = %0.2f", a, b, (float)a / (float)b);
break;
default :
printf("\nSorry!
Invalid Operator.");
}
}
Problem Scenario:
Program to calculate Employee Loan.
Category Marital
Status Service Loan
Permanent(P)
Unmarried(U) >=30 50,000
Else 25,000
Married(M) >=30
40,000
Else 20,000
Temporary
Married/Unmarried -N.A.- 10,000
Program
#include<stdio.h>
void main()
{
char cat, marital;
int service;
float loan = 0;
printf("\nEnter
Category (p, t), Marital Status (m, u) : ");
scanf("%c %c",
&cat, &marital);
switch(cat)
{
case 'p':
printf("\nEnter
Number of Years of Service : ");
scanf("%d",
&service);
if(marital == 'u')
{
if(service >= 30)
loan = 50000;
else
loan = 25000;
}
else if(marital ==
'm')
{
if(service >= 30)
loan = 40000;
else
loan = 20000;
}
break;
case 't':
loan = 10000;
break;
default :
printf("\nInavalid
Category");
break;
}
printf("\nThe
Amount of Loan Sanctioned to the Employee is : %0.2f", loan);
}
Unconditional
Branching
Unconditional
Branching is a mechanism of forcing the program to branch
from one place to
another place without ant specific condition.
Unconditional
Branching is implemented in ‘C’ Language with the use of
‘goto’ Statement.
‘goto’ Statement is
used to alter the normal sequence of program execution
by transfering
control to some other parts of the program.
Syntax: goto
<label>
Label: it is an
Identifier that is used to target the statement to which control
has to be transferred
by ‘goto’.
Syntax: label-name:
Satements to be
Executed;
No two statements under
execution can have same label in a program block.
Check the Nuisence of
the ‘goto’
Program
#include<stdio.h>
void main()
{
int num;
printf("\nEnter
any Number : ");
scanf("%d",
&num);
if(num % 2 == 0)
goto LabelEven;
else
goto LabelOdd;
LabelEven:
printf("\nYou
Supplied Even Number.");
LabelOdd:
printf("\nYou
Supplied Odd Number.");
}
Check the
Intelligence of the ‘goto’
Program
#include<stdio.h>
void main()
{
int num = 0;
mainpara:
printf("\nEnter
any Number : ");
scanf("%d",
&num);
if(num < 0)
goto stop;
else
{
printf("\nThe
Given Number is : %d", num);
goto mainpara;
}
stop:
printf("\nNow
Stopping Your Process.");
}
Iterative Principle
If any PROCESS has to
be repeated for FINITE or INFINITE number of times
respective to a
SPECIFIC CONDITION until the condition evaluates to FALSE
is considered as
LOOPING or ITERATION.
Even though INFINITE
ITERATIONS are POSIIBLE, but these types of Iterative situations are
discouraged.
The different types
of iterations used in programming languages are:
A. While
Loops.
B. Do-While
Loops.
C. For
Loops.
Every iteration
process will have four properties
1) Initialization
2) Condition
3) Updation
4) Action
Initialization:
Initialization
indicates the stsrating state of the system process before the
commencement of the iterative
process.
Condition:
Condition gives an
idea regarding where the process is going to be
terminated.
Updation:
it is the change that
Executes between the two successive operations in an
Iteration.
Action:
Set of instructions
that are performed related to each iteration.
Any iterative process
has two Parts:
1) Control Statement.
2) Body of the loop.
Control Statement:
It tests some
condition and then controls the repeated execution of the
statement present in
the body of the loop.
Body of the Loop
It is any action or
actions that are executed when the condition is executed
to TRUE.
Properties of control
statements:
1) Pre-tested loops
2) Post-tested loops
Pre-Tested Loop
In this control
statement the CONTROL CONDITION is tested BEFORE the
EXECUTION of the LOOP
BEGINS.
The Minimum number of
times the loops gets executed is Zero.
Pre-Tested Loop
In this control
statement the CONTROL CONDITION is tested AFTER the
EXECUTION of the LOOP
at least once.
The Minimum number of
times the loops gets executed is One.
While Loop Construct:
It is a PRE-TESTED
Loop constructThe test condition is evaluated before the
body of the loop gets
executed.
Body of the loop can
have more than one statement.
{} braces should be
provided for multi-statement Execution.
Including Braces for
individual statements increases readability.
Syntax:
while (test
condition)
{
BODY OF THE LOOP
}
Illustrative Programs
Program to find Sum
of the 1st 10 Natural Number s
Program
#include<stdio.h>
void main()
{
int x, sum;
x = 1;
sum = 0;
while(x <= 10)
{
sum = sum + x;
x = x + 1;
}
printf("\nSum of
1 st 10 Numbers is : %d", sum);
}
Program to find sum
of First N Numbers
Program
#include<stdio.h>
void main()
{
int x = 1, sum = 0,
n;
printf("\nEnter
the value of n : ");
scanf("%d",
&n);
while(x <= n)
{
sum += x;
x++;
}
printf("\n Sum
of First %d Numbers is : %d.", n, sum);
}
Program to find the
value of ‘m’ to power of ‘n’
Loop test
TRUE
EXECUTE
FALSE
TERMINATE
Program
#include<stdio.h>
void main()
{
int m, n, product =
1, counter = 1;
printf("\nEnter
Two Numbers : ");
scanf("%d%d",
&m, &n);
while(counter <=
n)
{
product = product *
m;
counter++;
}
printf("\n The
%d to the Power of %d is %d.", m, n, product);
}
No comments:
Post a Comment