0% found this document useful (0 votes)
19 views12 pages

Branching and Looping Programs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views12 pages

Branching and Looping Programs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Program 1: C program to calculate Compound Interest

#include<stdio.h>
#include<conio.h>
#include<math.h>

int main()
{
float principal;
float rate;
float time;
float Amount, CI;

printf("Enter Pricipal, ROI and time\n");


scanf("%f%f%f", &principal, &rate, &time);

Amount = principal * ((pow((1 + rate / 100), time)));


CI = Amount - principal;

printf("Compound Interest is : %.2f",CI);


getch();
return 0;
}

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);

if(ch >= 48 && ch <= 57)


{
printf("%c is a digit.", ch);
}
else if(ch >= 65 && ch <= 90)
{
printf("%c is a upper case letter.", ch);
}
else if(ch >= 97 && ch <= 122)
{
printf("%c is a lower case letter.", ch);
}
else
{
printf("%c is a special character.", ch);
}

getch();
}

Method 2:
#include<stdio.h>
#include<conio.h>

void main()
{
char ch;
clrscr();

printf("Enter a character\n");
scanf("%c", &ch);

if(ch >= '0' && ch <= '9')


{
printf("%c is a digit.", ch);
}
else if(ch >= 'A' && ch <= 'Z')
{
printf("%c is a upper case letter.", ch);
}
else if(ch >= 'a' && ch <= 'z')
{
printf("%c is a lower case letter.", ch);
}
else
{
printf("%c is a special character.", 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();

printf("\n\t\tMENU\n\n1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n5.


Modulus\n\n");
printf("Enter your choice(1-5):\n");
scanf("%d", &ch);

if(ch >= 1 && ch <= 5)


{
printf("Enter two numbers\n");
scanf("%d%d", &a, &b);

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 == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '%')


{
printf("Enter two numbers\n");
scanf("%d%d", &a, &b);

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();
}

Program 5: Concept of Local and Global Variables

#include<stdio.h>
#include<conio.h>

int a =5; //Global Variable


void main()
{
int a = 10; //Local Variable
{ //BLOCKS
int a =20;
{ //Inner block
int a =30;
printf("\nValue of a = %d", a);
}
printf("\nValue of a = %d", a);
}
printf("\nValue of a = %d", a);
getch();
}

Program 6: Concept of Implicat and Explicit Type Casting

#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);

avg = (m1 + m2 + m3) / 3 ; //m1= 43, m2, 65, m3=89,avg = 65.00


printf("Average marks of three subjects: %.2f", avg);

//Implicit Type Casting


avg = (m1 + m2 + m3) / 3.0 ; //m1= 43, m2, 65, m3=89,avg = 65.67
printf("Average marks of three subjects: %.2f", avg);

//Explicit Type Casting


avg = (float)(m1 + m2 + m3) / 3 ; //m1= 43, m2, 65, m3=89,avg = 65.67
printf("Average marks of three subjects: %.2f", avg);

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();

printf("\n\t\tMENU\n\n1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n5.


Modulus\n\n");
printf("Enter your choice(1-5):\n");
scanf("%d", &ch);

if(ch >= 1 && ch <= 5)


{
printf("Enter two numbers\n");
scanf("%d%d", &a, &b);

switch(ch) //can be only int or char


{
case 1:
result = a + b;
printf("%d + %d = %d", a, b, result);
break;
case 2:
result = a - b;
printf("%d - %d = %d", a, b, result);
break;
case 3:
result = a * b;
printf("%d * %d = %d", a, b, result);
break;
case 4:
result = a / b;
printf("%d / %d = %d", a, b, result);
break;
case 5:
result = a % b;
printf("%d %% %d = %d", a, b, result);
}
}
else
{
printf("ERROR: WRONG CHOICE ENTERED");
}

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);

/*Using while loop


i = 1; //Initialization
while(i <= n) //Condition : n>=1
{
printf("i = %d\t", i);
i++; //Update
}
*/
/*Using do_while loop
i = 1; //Initialization
do
{
printf("i = %d\t", i);
i++; //Update
}while(i <= n); //Condition : i<=n
*/

//Using for loop


for(i = 1; i <= n; i++) //Condition : i<=n
{
printf("i = %d\t", i);
}
getch();
return 0;
}

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);

/*Using while loop


i = n; //Initialization
while(i >= 1) //Condition : i>=1
{
printf("i = %d\t", i);
i--; //Update
}
*/

/*Using do_while loop


i = n; //Initialization
do
{
printf("i = %d\t", i);
i--; //Update
}while(i >= 1); //Condition : i>=1
*/

//Using for loop


for(i=n; i >= 1; i--) //Condition : i>=1
{
printf("i = %d\t", i);
}

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);

for( i=1, a=1 ; i <= n; i++)


{
printf("%d\t", a);
sum = sum + a;
a = a + 3;
}
printf("\n\nSum of the above series = %d", sum);
getch();
}
While Loop do while Loop:- for Loop:-

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.

# include<stdio.h> # include<stdio.h> # include<stdio.h>


# include<conio.h> # include<conio.h> # include<conio.h>
int main( ) int main( ) int main( )
{ { {
int n,sum=0,i; int n,sum=0,i=1; int n,sum=0,i;
clrscr( ); clrscr( ); clrscr( );

printf(“Enter the number”); printf(“Enter the number”); printf(“Enter the number”);


scanf(“%d”, &n); scanf(“%d”, &n); scanf(“%d”, &n);

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);

Example: WAP to calculatefactorial of a getch( );


number. }

# include<stdio.h>
# include<conio.h>
void main( )
{
intn, fact=1, i;
clrscr( );

printf(“Enter the number \n”);


scanf(“%d”,&n);

for (i=1; i<=n; i++)


{
fact = fact * i;
}

printf(“Factorial of %d is %d”, n, fact);

getch( );
}

You might also like