C Programmming: 1. Distinguish Between The Following: A. While and Do-While B. Break and Continue

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

C programmming

1. Distinguish between the following:


a. while and do-while
b. Break and Continue
a)While loop do-while loop

1.In while loop condition is 1.in while loop condition is


written and checked at the checked at the end of loop
starting of loop

2.executes the statements 2.executes the statements


within the while block if within the while block at
only the condition is true. least once.
3.syntax: 3.syntax:
while(expression) do
{ {
statement1; statement1;
statement2; }while(expression);
}
4. In while loop, condition is 4. In do-while loop,
checked first and then statements in the loop
statements in the loop executes first and then
executes. condition is checked..
5.after the condition or end 5.after the condition in do-
of the loop semicolon is not while semicolon is required.
required.
6.It is an entry-controlled 6.It is an exit controlled loop
loop
7.example: 7.example:
void main() void main()
{ {
int i=1; int i=1;
while(i<=10) do
{ {
printf(“%d”,i); printf(“%d”,i);
i++; i++;
} } while(i<=10);
getch(); getch();
} }

Rekha k b ,Dept of CSE,GST,B’lore Page 1


C programmming

b) Break and Continue


Break Continue

1. . Exits from current block/loop , that 1.When continue is encountered, the


is,When break is encountered the switch or statements after it are skipped and the loop
loop execution is immediately stopped. control jump to next iteration.

2.control passes to next statement 2.Control passes to beginning of loop

3.break statement is used in switch and loops. 3.continue statement is used in loops only

4.terminates the program 4.never terminates the program

5.Syntax: 5.Syntax:

while(condition) while(condition)
{ {
Statement 1; Statement 1;
Statement 2; continue;
Statement 2;
Statement n;
}
break;
}
6.Example: 6.Example:
#include<stdio.h> #include<stdio.h>
void main( ) void main ( )
{ {
int i; int i;
for(i=0;i<5;++i) for(i=0;i<5;++i)
{ {
if(i==3) if(i==3)
break; continue;
printf(“%d “,i); printf(“%d “,i);
printf(“%d “,i); printf(“%d “,i);
} }
getch( ); getch( );
} }
Output: 0 1 2
Output:0 1 2 4

2. What are the control statements? Explain with an example?

C SUPPORTS MAINLY THREE TYPES OF CONTROL STATEMENTS


1) Decision making statements

Rekha k b ,Dept of CSE,GST,B’lore Page 2


C programmming

i) if statement
ii) if else statement
iii) nested if statement
iv) else if ladder
v) switch statement
2) Loop control statements
i) while loop
ii) for loop
iii) do-while loop
3) Unconditional control statements
i) break statement
ii) continue statement

refer Xerox note for diagram:

1) Decision making statements


i) if statement
ii) if else statement
iii) nested if statement
iv) else if ladder
v) switch statement

i) if statement
refer notes: syntax, flowchart for if , example.

ii) if else statement


• This is basically a “two-way” decision statement.
• This is used when we must choose between two alternatives.

• The syntax is shown below:


if(expression)
{
statement1;
}
else
{
statement2;
}
• Firstly, the expression is evaluated to true or false.
If the expression is evaluated to true, then statement1 is executed.
If the expression is evaluated to false, then statement2 is executed.

Rekha k b ,Dept of CSE,GST,B’lore Page 3


C programmming

• The flow diagram is shown below:

Example1: Program to illustrate the use of if else statement.


#include<stdio.h>
void main()
{
int n;
printf(“Enter any non-zero integer: \n”) ;
scanf(“%d”, &n)
if(n>0)
printf(“Number is positive number”);
else
printf(“Number is negative number”);
}
Output:
Enter any non-zero integer:
7
Number is positive number

2) Loop control statements


i) while loop
ii) for loop
iii) do-while loop

i) while loop
• A while loop statement can be used to execute a set of statements repeatedly as long as a given
condition is true.
• The syntax is shown below:
while(expression)

Rekha k b ,Dept of CSE,GST,B’lore Page 4


C programmming

{
statement1;
statement2;
}
• Firstly, the expression is evaluated to true or false.
• If the expression is evaluated to false, the control comes out of the loop without executing the
body of the loop.
• If the expression is evaluated to true, the body of the loop (i.e. statement1) is executed.
• After executing the body of the loop, control goes back to the beginning of the while statement and
expression is again evaluated to true or false. This cycle continues until expression becomes
false.
• The flow diagram is shown below:

Example1: Program to display a message 5 times using while statement.


#include<stdio.h>
void main()
{
inti=1;
while(i<=5)
{
printf(“Welcome to C language \n”);
i=i+1;
}
}
Output:
Welcome to C language
Welcome to C language
Welcome to C language

Rekha k b ,Dept of CSE,GST,B’lore Page 5


C programmming

Welcome to C language
Welcome to C language

3) Unconditional control statements

i) break statement
ii) continue statement
i) Break statements

This control statement can be used in loops and switch statements. It is used to terminate the loop
when a specified condition is satisfied.

The control comes out of the loop and the following statements are executed. In switch statements,
the control comes out of the switch statements and the following statements are executed.
• The break statement is jump statement which can be used in switch statement and loops.
• The break statement works as shown below:
1) If break is executed in a switch block, the control comes out of the switch block and the
statement following the switch block will be executed.
2) If break is executed in a loop, the control comes out of the loop and the statement following
the loop will be executed.
• The syntax is shown below:

Example: Write the program given in switch statement.


A C program to illustrate the working of break statement
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
clrscr();

Rekha k b ,Dept of CSE,GST,B’lore Page 6


C programmming

while(i<=10)
{
if(i==6)
break;
printf("\n%d",i);
}
getch();
}
3. Explain the following iterative statements/looping statements with syntax and example.
While, Do-while&For
i) While –refer 2ndque above
ii) Do-while :

THE do while STATEMENT


• When we do not know exactly how many times a set of statements have to be repeated, do-while
statement can be used.
• The syntax is shown below:
do
{
statement1;
}while(expression);
• Firstly, the body of the loop is executed. i.e. the body of the loop is executed at least once.
• Then, the expression is evaluated to true or false.
• If the expression is evaluated to true, the body of the loop (i.e. statement1) is executed
• After executing the body of the loop, the expression is again evaluated to true or false. This cycle
continues until expression becomes false.
• The flow diagram is shown below:

Rekha k b ,Dept of CSE,GST,B’lore Page 7


C programmming

Example1: Program to display a message 5 times using do while statement.


#include<stdio.h>
void main()
{
inti=1;
do
{
printf(“Welcome to C language \n”);
i=i+1;
}while(i<=5);
}
Output:
Welcome to C language
Welcome to C language
Welcome to C language
Welcome to C language
Welcome to C language

iii) The for loop:


Syntax:
for (initialization expression; test expression; update expression)
{
statement/s;
}

The for loop is useful while executing the statement/s a number of times. For example, A program that
displays the first 10 multiples of 3 on a single line, is given below:

#include<stdio.h>
#include<conio.h>
void main()
{
inti;
clrscr();
for(i=1;i<=10;i++)
printf(“\t%d”,3*i);
getch();
}

Rekha k b ,Dept of CSE,GST,B’lore Page 8


C programmming

The for keyword is followed by three components within round braces, ( and ). These three components
are separated by semicolons. In the above example, the three components are:

i=1
i<=10
i++

The first component, i=1 is executed only once prior to the statements within the for loop. This is called
the initialization expression.

The second component i<=10 is evaluated once before every execution of the statements within the
loop. It is called the test expression. If this expression is true, the statements within the loop executes. If
it is false, the loop terminates and the control of execution is transferred to the statement following the
for loop.

The third component i++ is executed once after every execution of the statements within the loop. It is
called as Update expression.

4. Explain Nested loops with an example?

types of nested loops

 Nested while loop


 Nested do-while loop
 Nested for loop

Nested for loop


A for loop inside another for loop is called nested for loop.

Syntax of Nested for loop


for (initialization; condition; increment/decrement)
{
statement(s);
for (initialization; condition; increment/decrement)
{
statement(s);
... ... ...
}
... ... ...
}

Rekha k b ,Dept of CSE,GST,B’lore Page 9


C programmming

Example of Nested while loop


Example 1: C program to print the number pattern.

1
12
123
1234
12345

#include <stdio.h>
int main()
{
inti=1,j;
while (i<= 5)
{
j=1;
while (j <= i )
{
printf("%d ",j);
j++;
}
printf("\n");
i++;
}
return 0;
}

Rekha k b ,Dept of CSE,GST,B’lore Page 10


C programmming

In this program, nested while loop is used to print the pattern. The outermost loop runs 5 times and for
every loop, the innermost loop runs i times which is 1 at first, meaning only "1" is printed, then on the
next loop it's 2 numbers printing "1 2" and so on till 5 iterations of the loop executes, printing "1 2 3 4
5". This way, the given number pattern is printed.

5. Define loop? Explain with an example how infinite loop can be created.

Loop- loop is a control structure that repeats a group of any kind of statements in a program.
A loop becomes an infinite loop if a condition never becomes false.
The for loop is traditionally used for this purpose. Since none of the three expressions that form the ‘for’
loop are required, you can make an endless loop by leaving the conditional expression empty

Infinity for loop


Syntax:
for (;;)
{
statement;
}

Example :
void main()
{
for(;;)
{
Printf(“computer”);
}
getch();
}

Example2:
#include<stdio.h>
voidmain()
{
while(1)
printf("Hello");
}
Output :
Infinite Time "Hello" word

Explanation :
1. We can specify any non-zero positive number inside while loop
2. Non zero number is specified in the while loop which means that while loop will remains true
forever.

Rekha k b ,Dept of CSE,GST,B’lore Page 11


C programmming

6.Differentiate between for and while loop. Discuss the usage of each.

For loop While loop

1.the condition statement in FOR is optional 1.the condition statement is mandatory.if


.If condition statement is omitted then FOR condition statement is omitted then WHILE
will execute infinitely will give you compilation error! .
2.the while statement continually executes a 2.for statement provides a compact way to
block of statements while a particular iterate over a range of values. We often call
condition is true. Its syntax can be expressed it as the "for loop" because of the way in
as: which it repeatedly loops as long as a
particular condition is satisfied.
initialization;
while (expression) The syntax of for loop is:
{ for(initialization; condition;
statement; increment/decrement)
increment counter; {
} // body
}
For loop is used where we already know While loop is used in situations where we do
about the number of times loop needs to be not know how many times loop needs to be
excuted. Typically for a index used in excuted beforehand.
iteration.
Ex: Ex:
for(i=0;i<10;i++) int i=0;
{ while(i<10)
statement; {
} statement;
i++;
}

7. Write a C program to check input alphabet is vowel or consonant.


#include <stdio.h>
void main()
{
char ch;
printf("Enter a character\n");
scanf("%c", &ch);

Rekha k b ,Dept of CSE,GST,B’lore Page 12


C programmming

if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch =='o' || ch=='O' || ch == 'u'
|| ch == 'U')
{
printf("%c is a vowel.\n", ch);
}
else
{
printf("%c is a consonant.\n", ch);
}
getch();
}

Output:
Enter a character
A
A is a vowel

8.Develop a c program to print whether the given number is perfect (for a perfect number, the
sum of divisors-except the number itself-will be equal to that number; Ex: 6,28,496, etc.).

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,sum=0;
clrscr();
printf("enter the value of n\n");
scanf("%d",&n);
for(i=1;i<n;i++)
{
if(n%i==0)
{
sum=sum+i;
}
}
if(n==sum)
printf("the number %d is perfect\n");
else
printf("thenumber is not perfect");
getch();
}

Output:
enter the value of n

Rekha k b ,Dept of CSE,GST,B’lore Page 13


C programmming

6
the number 6 is perfect

9. Develop a program to print sum of digits of a given number


#include<stdio.h>
#include<conio.h>
void main()
{
inti,n,t,sum=0,digit;
clrscr();
printf("enter the value of n\n");
scanf("%d",&n);
t = n;
while(n!=0)
{
digit = n%10;
n = n / 10;
sum = sum +digit;
}
printf("Sum of digits of %d = %d\n", n, sum);
getch( );
}

Output:
12345
Sum of digits of 5 is 15.
For example if the input is 98, sum(variable) is 0 initially
98%10 = 8 (% is modulus operator which gives us remainder when 98 is divided by 10).
sum = sum + remainder
so sum = 8 now.
98/10 = 9 because in c whenever we divide integer by another integer we get an integer.
9%10 = 9
sum = 8(previous value) + 9
sum = 17
9/10 = 0.
So finally n = 0, loop ends we get the required sum.

10.Write a C program to print the series of even number till 'n'.


#include<stdio.h>
#include<conio.h>
void main()
{
inti,n;
clrscr();

Rekha k b ,Dept of CSE,GST,B’lore Page 14


C programmming

printf("enter the value of n\n");


scanf("%d",&n);
for(i= 0;i<=n ; i = i+2)
{
printf(“%d”,i);
}
getch( );
}

OR

#include<stdio.h>
#include<conio.h>
void main()
{
inti,n;
clrscr();
printf("enter the value of n\n");
scanf("%d",&n);
i= 0;
while (i<=n )
{
printf(“%d”,i);
i=i+2;
}
getch( );
}

Output:
enter the value of n
5
24

11.Write a C program to print the series of odd number till 'n'.


#include<stdio.h>
#include<conio.h>
void main()
{
inti,n;
clrscr();
printf("enter the value of n\n");
scanf("%d",&n);
for(i= 1;i<=n ; i = i+2)
{
printf(“%d”,i);
}

Rekha k b ,Dept of CSE,GST,B’lore Page 15


C programmming

getch( );
}

Output:
enter the value of n
5
135

12. Write a C program to print the reverse of a number.


#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,rev=0,digit;
clrscr();
printf("enter the number\n");
scanf("%d",&n);
while(n!=0)
{
digit = n%10;
n = n / 10;
rev = rev *10+digit;
}
print(“reverse of given number = %d”,rev);
getch( );
}
Output:
enter the number
623
reverse of given number=326

13. An integer is said to be prime if it is divisible by 1 and itself. Write a C program to test
whether a positive integer is prime or not
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,flag=0;
clrscr();
printf("enter the number\n");
scanf("%d",&n);
for(i = 2; i<= n/2;i++)
{
if(n%i == 0)
{

Rekha k b ,Dept of CSE,GST,B’lore Page 16


C programmming

flag = 1;
break;
}
}

if (flag == 0)
{
printf("%d is a prime number", n);
}
else
{
printf("%d is not a prime number", n);
}
getch( );
}
Output:
enter the number:
3
3 is a prime number

14. Develop a C program to print the Fibonacci series up to n numbers


#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,fib1=0,fib2=1,fib3;
clrscr();
printf("enter the value of n\n");
scanf("%d",&n);
if(n==1)
{
printf("%d\t",fib1);
}
else if(n==2)
{
printf("%d\t%d",fib1,fib2);
}
else
{
printf("%d\t%d\t",fib1,fib2);
for(i=3;i<=n;i++)
{
fib3=fib1+fib2;
printf("%d\t",fib3);
fib1=fib2;
fib2=fib3;

Rekha k b ,Dept of CSE,GST,B’lore Page 17


C programmming

}
}
getch();
}

Output:
enter the value of n
4
0112

15.Write a C program to evaluate the following series. 1+x+x2+…+xn


#include<stdio.h>
#include<conio.h>
void main()
{
inti,x,n,sum=1;
clrscr();
printf("enter the x and n values\n");
scanf("%d%d",&x, &n);
for(i= 1;i<=n ; i++)
{
sum= sum + pow(x,i);
}
printf(“sum=%d”,sum);
getch( );
}

.
Output:
enter the value of x and n
2 2
Sum=7

16. Write a C program to find the factorial of a number using while loop,where the number n is
entered by the user.
#include<stdio.h>
#include<conio.h>
void main()
{
inti,x,n,fact=1;
clrscr();
printf("enter the number \n");
scanf("%d",&n);
for(i= 1;i<=n ; i ++)
{
fact=fact * i;

Rekha k b ,Dept of CSE,GST,B’lore Page 18


C programmming

}
getch( );
}

Output:
enter the number
3
fact=6

17.Write a program, to find the sum of squares of first n natural numbers.


#include<stdio.h>
#include<conio.h>
void main()
{
inti,x,n,sum=1;
clrscr();
printf("enter the number \n");
scanf("%d",&n);
for(i= 1;i<=n ; i ++)
{
sum= sum+i* i;
}
print(“sum=%d”,sum);
getch( );
}

Output:
enter the number
2
Sum=5

18.Write a C program to find the factors of a given number & total no of factors.

What is factor of a number?


Factor of any number is a whole number which exactly divides the number into a whole number without
leaving any remainder. For example: 2 is a factor of 6 because 2 divides 6 exactly leaving no remainder.

#include <stdio.h>
void main()
{
int i, n,count=0;
printf("Enter any number to find its factor: ");
scanf("%d", &n);
printf("All factors of %d are: \n", n);
for(i=1; i<=n; i++)

Rekha k b ,Dept of CSE,GST,B’lore Page 19


C programmming

{
/*
* If num is exactly divisible by i
* Then i is a factor of num
*/
if(num % i == 0)
{
printf("%d, ",i);
count++;
}
}
printf(“total no of factors=%d”,count);
getch( );
}

Output:
Enter any number to find its factor:4
All factors of 4 are: 1 2 4
total no of factors: 3

19.Write a program to do the arithmetic operations using multi way decision system
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

void main()
{
intch;
float a,b,res;
clrscr();
printf(“Enter two numbers:”);
scanf(“%f%f”,&a,&b);
printf(“\n Menu \n1.Addition \n 2.Subtraction \n 3.Multiplication \n 4.Division”);
printf(“nEnter your choice:”);
scanf(“%d”,&ch);

switch(ch)
{
case 1: res=a+b;
break;
case 2: res=a-b;
break;
case 3: res=a*b;
break;
case 4: res=a/b;
break;

Rekha k b ,Dept of CSE,GST,B’lore Page 20


C programmming

default: printf(“Wrong choice!!nPress any key…”);


break;
}
printf(“res=%d”,res);
getch();
}

Output:
Enter two numbers: 10 20
Menu
1.Addition
2.Subtraction
3.Multiplication
4.Division
Enter your choice
3

Res = 200

20. Explain the syntax of nested if-else statement, write a C program to find the
largest of three numbers using nested-if else statement.
Nested if-else:
Syntax:
Refer Xerox notes

Program:
#include<stdio.h>
void main()
{
int a, b, c;

printf("Enter three numbers: ");


scanf("%d %d %d",&a,&b,&c);

if(a >b)
{
if(a >c)
{
printf("%d is the largest number.", a);
}
else
{
printf("%d is the largest number.", c);
}
}

Rekha k b ,Dept of CSE,GST,B’lore Page 21


C programmming

else
{
if(b>c)
{
printf("%d is the largest number.", b);
}
else
{
printf("%d is the largest number.",c);
}
}

getch();
}

Output:
Enter three numbers: 10 20 30
30 is the largest number

21. Write a program to perform the simple calculator operations using switch().
#include<stdio.h>
#include<conio.h>

void main()
{
char op;
inta,b,res=0;
clrscr();
printf("Enter First value: ");
scanf("%d",&a);
printf("\n Enter Operator: ");
scanf("%c",&op);
printf("\n Enter Second value: ");
scanf("%d",&b);

switch(op)
{
case '+':res=a+b;
printf("Sum: %d",res);
break;
case '-': res=a-b;
printf("Difference: %d",res);
break;
case '*': res=a*b;
printf("Product: %d",res);

Rekha k b ,Dept of CSE,GST,B’lore Page 22


C programmming

break;
case '/': if(b = = 0)
{
printf(“divide by zero error”);
}
else
{
res=a/b;
printf("Quotient: %d",res);
}
break;
default: printf("Enter Valid Operator!!");
break;
}
getch();
}

Output:
Enter First value:10
Enter Operator:+
Enter second value: 20
Sum:30

22. Explain switch,if-else,nested-if with example program.


OR
Explain about the multi way decision making system with example.

Refer Xerox notes: Explain each with syntax,flowchart,one program

23. Explain about


a. Continue
b. Break
c. Goto statements
With examples

Refer Xerox notes:

24. Explain basic input and output functions with proper examples.
OR
List and explain formatted and unformatted I/O Functions with an example for each.

Refer module2 notes: Explain each with syntax.

Rekha k b ,Dept of CSE,GST,B’lore Page 23

You might also like