[go: up one dir, main page]

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

Unit 2

The document contains various programming exercises and concepts related to loops, algorithms, and conditional statements in C. It includes explanations of while loops, do-while loops, nested if-else statements, and examples of programs for tasks such as finding the largest number, converting temperatures, and performing arithmetic operations. Additionally, it provides algorithms and flowcharts for determining if a number is odd or even and checking if a number is prime.

Uploaded by

vivekaher583
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 views7 pages

Unit 2

The document contains various programming exercises and concepts related to loops, algorithms, and conditional statements in C. It includes explanations of while loops, do-while loops, nested if-else statements, and examples of programs for tasks such as finding the largest number, converting temperatures, and performing arithmetic operations. Additionally, it provides algorithms and flowcharts for determining if a number is odd or even and checking if a number is prime.

Uploaded by

vivekaher583
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/ 7

Unit 2

1. Differentiate between while loop and do while loop.

Ans.

2. Write an algorithm and draw a flowchart to find largest number from three numbers.
Ans.
Algorithm:
Step 1:Start
Step 2:Declare variables no1,no2,no3
Step 3: Accept / Initialize values for variables no1,no2,no3
Step 4: If no1 >no2 and no1>no3 then display "no1 is largest" otherwise check if no2>no1 and
no2>no3 then display "no2 is largest" otherwise display "no3 is largest"
Step 5: Stop
Flowchart
3. Write a program to convert temperature in Fahrenheit degrees to Centigrade degrees.
Ans.
#include<stdio.h>

#include<conio.h>

void main()

float celsius, fahrenheit;


printf("Enter temperature in Fahrenheit: ");
scanf("%f", &fahrenheit);
celsius = (fahrenheit - 32) * 5 / 9;
printf("Temperature in Fahrenheit =%f Temperature in Centigrade =%f", fahrenheit, celsius);
getch();
}
4. Write a C program with comments to reverse the digit of integer number. For example the
number 12345 should be displayed as 54321.
Ans.
#include<stdio.h>
#include<conio.h>
void main()
{
int num, res=0,ans=0;
clrscr();
printf("Enter the number");
scanf("%d", &num);
while(num!=0)
{
res=num%10;
ans=ans*10+res;
num=num/10;
}
printf("Reverse number is %d", ans);
getch();
}

5. Explain nested if-else with example.


(Note: Any example shall be considered)

Ans.
When a series of decision is required, nested if-else is used. Nesting means using one if-else
construct within another one. If the condition in the outer if, is true, then only the inner if-else
will get executed. Further the statements in the inner if will get execute only if the condition of
inner if, evaluates to true.
If it is false, the statements in inner else will get executed. If the outer if evaluates to false, then
the statements in outer else get executed.
General syntax:
if(condition)
{
if(condition)
{ statements }
else { statements }
}
else { statements }
statements

Example:
#include
#include
void main()
{
int val;
clrscr();
printf("Enter a number");
scanf("%d",&val);
if(val>=5)
{
if(val>5)
{
printf("Number is greater than 5");
}
else
{
printf("Number is equal to 5");
}
}
Else
{
printf("Number is less than 5");
}
getch();
}

6. Write an algorithm to determine the given number is odd or even.

Ans.

Step 1- Start

Step 2- Read / input the number.

Step 3- if n%2==0 then number is even.

Step 4- else number is odd.

Step 5- display the output.

Step 6- Stop

7. Illustrate the use of break and continue statement with example.


Ans.
Break: It breaks the execution of the loop which allows exiting from any loop or switch, such
that break statement skips the remaining part of current iterations of the loop.
Syntax: break;

Continue: It is used when it is required to skip the remaining portion of the loop without
breaking loop it will transfer control directly to next iteration
Syntax: continue;

In given program sequence if “break” executes then execution control will jump out of loop &
next statement after loop will be executed. In given program sequence if “continue” executes
then execution control will skip remaining statements of loop & will start next iteration of loop

8. Write a program to add, subtract, multiply and divide two numbers, accepted from user switch
case.
Ans.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,ch,add,sub,mul,div;
clrscr();
printf("\n1 for addition \n2 for substraction");
printf("\n3 for multiplication \n4 for division");
printf("\nEnter two numbers:");
scanf("%d%d",&a,&b);
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: add=a+b;
printf("Addition of a & b=%d",add);
break;
case 2: sub=a-b;
printf("Substraction of a & b=%d",sub);
break;
case 3: mul=a*b;
printf("Multiplication of two numbers=%d",mul);
break;
case 4: div=a/b;
printf("Division of two numbers=%d",div);
break;
default: printf("Invalid choice....");
}
getch();
}

9. Write a program to take input as a number and reverse it by while loop.

(Note: Any other correct logic shall be considered).

#include

#include

void main()

int no;

int sum=0,rem;

printf("\n Enter number:");

scanf("%d",&no);

while(no>0)

rem=no%10;

no=no/10;
sum=sum*10+ rem;

printf("\nsum=%d",sum);

getch();

10. Draw flowchart for checking weather given number is prime or not
Ans.

You might also like