Branching and Looping Programs
Branching and Looping Programs
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
float principal;
float rate;
float time;
float Amount, CI;
Program 2: WAP to check whether the entered character is a digit, uppercase, smallcase
letter or a special symbol
Method 1:
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf("Enter a character\n");
scanf("%c", &ch);
getch();
}
Method 2:
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf("Enter a character\n");
scanf("%c", &ch);
getch();
}
Program 3: Simulate a calculator to perform all arithmetic operations using integer choice
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, result;
int ch;
clrscr();
if(ch == 1)
{
result = a + b;
printf("%d + %d = %d", a, b, result);
}
else if(ch == 2)
{
result = a - b;
printf("%d - %d = %d", a, b, result);
}
else if(ch == 3)
{
result = a * b;
printf("%d * %d = %d", a, b, result);
}
else if(ch == 4)
{
result = a / b;
printf("%d / %d = %d", a, b, result);
}
else if(ch == 5)
{
result = a % b;
printf("%d %% %d = %d", a, b, result);
}
}
else
{
printf("ERROR: WRONG CHOICE ENTERED");
}
getch();
}
Program 4: Simulate a calculator to perform all arithmetic operations using character choice
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, result;
char ch;
clrscr();
printf("\n\t\tMENU\n\n");
printf("Enter your choice as (+, - , *, /, %):\n");
scanf("%c", &ch);
if(ch == '+')
{
result = a + b;
printf("%d + %d = %d", a, b, result);
}
else if(ch == '-')
{
result = a - b;
printf("%d - %d = %d", a, b, result);
}
else if(ch == '*')
{
result = a * b;
printf("%d * %d = %d", a, b, result);
}
else if(ch == '/')
{
result = a / b;
printf("%d / %d = %d", a, b, result);
}
else if(ch == '%')
{
result = a % b;
printf("%d %% %d = %d", a, b, result);
}
}
else
{
printf("ERROR: WRONG CHOICE ENTERED");
}
getch();
}
#include<stdio.h>
#include<conio.h>
#include<stdio.h>
#include<conio.h>
void main()
{
int m1, m2, m3;
float avg;
clrscr();
printf("Enter the marks of three subjects\n");
scanf("%d%d%d", &m1, &m2, &m3);
getch();
}
Program 7: Simulate a calculator to perform all arithmetic operations using integer choice
using switch case
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, result;
int ch;
clrscr();
getch();
}
Program 8: WAP to print Ist 'n' natural numbers, For example if n=10, then output will be
1,2,3,4, …… , 10
#include<stdio.h>
#include<conio.h>
int main()
{
int i, n;
clrscr();
printf("Enter the number of natural values to be printed:\n");
scanf("%d", &n);
Program 9: WAP to print Ist 'n' natural numbers in reverse order, For example, if n=10, then
output will be 10,9,8,7,...,1
#include<stdio.h>
#include<conio.h>
int main()
{
int i, n;
clrscr();
printf("Enter the number of natural values to be printed:\n");
scanf("%d", &n);
getch();
return 0;
}
Program 10: WAP to program to print the following series alongwith the sum of all terms:
1, 4, 7, 10, ..... upto 'n' terms
#include<stdio.h>
#include<conio.h>
void main()
{
int i, a, sum=0, n;
clrscr();
printf("Enter the number of terms:\n");
scanf("%d", &n);
Loops generally consist of two Unlike while loop, which A for loop is a repetition
parts: one or more control test the loop condition at the top of control structure that allows you to
expressions which (not the loop, the do...while loop in C efficiently write a loop that needs
surprisingly) control the execution programming language checks its to execute a specific number of
of the loop, and the body, which is
condition at the bottom of the loop. times. This is also known as entry
the statement or set of statements
which is executed over and over. That’s why this loop is also known controlled loop.
While loop is a entry controlled as exit controlled loop. A
do...while loop is similar to a while Syntax:-
loop because condition is checked
before entering into the loop. loop, except that a do...while loop The syntax of a for loop in C
is guaranteed to execute at least programming language is:
The general syntax of a while loop one time.
is for(initialization; condition;
Syntax:- update )
Initialization; {
while(condition ) Initialization; statement(s);
{ do }
Statement1; {
Statement2; statement(s);
Statement3; update; Example:-
Update; }while( condition ); #include <stdio.h>
} void main ()
{
Notice that the conditional int a;
Example:- expression appears at the end of the /* for loop execution */
#include <stdio.h> loop, so the statement(s) in the loop for( a = 1; a <= 10; a = a + 1 )
void main () {
execute once before the condition
{ printf("value of a: %d \n", a);
int a; is tested.
}
Example:- getch();
a=1; }
#include <stdio.h>
/* while loop execution */
void main ()
while( a <= 10 )
{
{
int a = 1;
printf("value of a: %d\n", a);
/* do loop execution */
a++;
do
}
{
getch();
printf("value of a: %d\n", a);
}
a = a + 1;
}while( a <= 10 );
getch();
}
Example: Program to print first ‘n’ Example: Program to print first ‘n’ Example: Program to print first ‘n’
natural numbers using while. Also print natural numbers using do_while. Also natural numbers using for. Also print
the sum of numbers. print the sum of numbers. the sum of numbers.
printf(“First %d natural numbers are\n”, printf(“First %d natural numbers are\n”); printf(“First %d natural numbers are\n”);
n); do for (i=1; i<=n; i++)
{ {
i=1; printf(“%d \t”, i); printf(“%d \t”, i);
while (i<=n) sum=sum+i; sum=sum+i;
{ i=i+1; }
printf(“%d \t”, i); }while (i<=n);
sum=sum+i; printf(“Sum of first %d natural numbers is
i=i+1; printf(“Sum of first %d natural numbers is %d\n”, n, sum);
} %d\n”, n, sum);
getch( );
printf(“Sum of first %d natural numbers is getch( ); }
%d\n”, n, sum); } Example: Program to print first ‘n’
Example: Program to print first ‘n’ natural numbers in reverse order using
getch( ); natural numbers in reverse order using for. Also print the sum of numbers.
} dowhile. Also print the sum of numbers. # include<stdio.h>
Example: Program to print first ‘n’ # include<stdio.h> # include<conio.h>
natural numbers in reverse order using # include<conio.h> void main( void)
while. Also print the sum of numbers. void main( void) {
# include<stdio.h> { int n,sum=0,i;
# include<conio.h> int n,sum=0,i; clrscr( );
void main( void) clrscr( );
{ printf(“Enter the number”);
int n,sum=0,i; printf(“Enter the number”); scanf(“%d”, &n);
clrscr( ); scanf(“%d”, &n);
i = n; printf(“First %d natural numbers in
printf(“Enter the number”); reverse order are\n”);
scanf(“%d”, &n); printf(“First %d natural numbers in for (i = n; i>= 1; --i)
i = n; reverse order are\n”); {
printf(“First %d natural numbers in do{ printf(“%d \t”, i);
reverse order are\n”); printf(“%d \t”, i); sum=sum+i;
while (i>=1) sum=sum+i; }
{ i = i - 1;
printf(“%d \t”, i); }while (i>=1); printf(“Sum of first %d natural numbers is
sum=sum+i; %d\n”, n, sum);
i--; printf(“Sum of first %d natural numbers is
} %d\n”, n, sum); getch( );
}
printf(“Sum of first %d natural numbers is getch( );
%d\n”, n, sum); }
getch( );
}
Example: WAP to accept a number and Example: WAP to calculatefactorial of a
print table of the given no. number.
# include<stdio.h> # include<stdio.h>
# include<conio.h> # include<conio.h>
void main( ) void main( )
{ {
inti,t; intn, i;
clrscr( ); longint fact=1,
clrscr( );
printf(“which table u want to print \n”);
scanf(“%d”,&t); printf(“Enter the number \n”);
scanf(“%d”,&n);
for (i=1; i<=10; i++) i = n;
{ while ( i>= 1)
printf(“\n%d*%d=%d”,t,i,i*t); {
} fact = fact * i;
i--;
getch( ); }
}
printf(“Factorial of %d is %ld”, n, fact);
# include<stdio.h>
# include<conio.h>
void main( )
{
intn, fact=1, i;
clrscr( );
getch( );
}