Professional Documents
Culture Documents
C Programmming: 1. Distinguish Between The Following: A. While and Do-While B. Break and Continue
C Programmming: 1. Distinguish Between The Following: A. While and Do-While B. Break and Continue
C Programmming: 1. Distinguish Between The Following: A. While and Do-While B. Break and Continue
3.break statement is used in switch and loops. 3.continue statement is used in loops only
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
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
i) if statement
refer notes: syntax, flowchart for if , example.
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)
{
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:
Welcome to C language
Welcome to C language
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:
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 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();
}
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.
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;
}
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
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.
6.Differentiate between for and while loop. Discuss the usage of each.
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
6
the number 6 is perfect
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.
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
getch( );
}
Output:
enter the value of n
5
135
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)
{
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
}
}
getch();
}
Output:
enter the value of n
4
0112
.
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;
}
getch( );
}
Output:
enter the number
3
fact=6
Output:
enter the number
2
Sum=5
18.Write a C program to find the factors of a given number & total no of factors.
#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++)
{
/*
* 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;
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;
if(a >b)
{
if(a >c)
{
printf("%d is the largest number.", a);
}
else
{
printf("%d is the largest number.", c);
}
}
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);
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
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.