1) Write Down An Algorithm That Reads in A Positive Integer X and Tests If It Is A Prime Number. (2) The Integer 1 Is Defined As Non-Prime
1) Write Down An Algorithm That Reads in A Positive Integer X and Tests If It Is A Prime Number. (2) The Integer 1 Is Defined As Non-Prime
number.
Program:
#include<stdio.h>
#include<conio.h>
void main()
int a,x,flag=1;
//clrscr();
scanf("%d",&x);
a=2;
while(a<=x-1)
if(x%a==0)
flag=0;
break;
else
{
flag=1;
a++;
if(flag==0)
else
printf("x is prime");
getch();
Output:
x is prime
2) Write down an algorithm to find out smallest and largest amongst 3 elements.
Program:
#include<stdio.h>
void main()
{
int a,b,c,large,small;
printf("Enter three number\n");
scanf("%d %d %d",&a,&b, &c);
large = a>b?a>c?a:c:b>c?b:c;
small = a<b?a<c?a:c:b<c?b:c;
Output:
Largest number is 4
Smallest Number is 1
3) Modify the above algorithm so that it will work with N elements where N is accepted
from the user.
Program:
#include<stdio.h>
int main()
lar = n;
sm=n;
scanf ("%d",&n);
if (n>lar)
lar=n;
if (n<sm)
sm=n;
return 0;
Output:
13
43
90
4) Write down an algorithm to read in an integer between 0 and 99 and output the value in
English. For example, if the input is 28, it should output “twenty-eight”.
Program:
#include<iostream>
using namespace std;
"ELEVEN","TWELVE","THIRTEEN","FOURTEEN","FIFTEEN","SIXTEEN","SEVENTEEN","EIGHTTEEN","NI
NETEEN"};
if(value<0)
cout<<" ";
convert(-value);
if(value % 10)
else
cout<<first[value];
return;
int main()
int number;
cin>>number;
convert(number);
return 0;
OUTPUT:
Enter a number : 45
FORTY FIVE
Recommended
5) Write down an algorithm to get the weight (in kilograms) and height (in meters) of a person.
Then calculate and print the Body Mass Index (BMI) according to the formula:
The BMI should be printed with 3 decimal places. You should choose an appropriate data types
and names for your variables.
6) Write down an algorithm to read in a temperature in Celsius and convert it to Fahrenheit. The
relationship between Celsius and Fahrenheit is
C = (F-32)*5/9.
7) Write down an algorithm to find out roots of a quadratic equation. Please note that roots can
be imaginary.
8) Write down an algorithm that reads in the number of seconds and converts it to hours,
minutes and seconds. A sample output of your program is as follows. Don’t bother with the
singular or plural forms of the nouns.
Please enter the number of seconds --> 500500 seconds = 0 hours 8 minutes 20 seconds
Hint: Use the / and % operators.
9) Write down an algorithm that reads in a mark of a student (which is an integer between 0 and
100) and prints the corresponding grades (A-F). The mark-to-grade conversion table is as
follows:
Grade A B C D F
Range ≥80 65-79 50-64 40-49 <40
1. Modify the program to allow user-specified grade boundaries. You may need to define
more variables.
2. Modify the program so that it checks if the input is between 0 and 100. If not, it should
ask the user to input again until the input is in the correct range. Use while statements.
3. Modify the program so that it repeats the above computation on 50 students. Use while
statements.
Draw histogram
Draw histogram
Reverse an array
Sort an array
Insert element at nth position in an array. Please note it should insert and not overwrite.