0% found this document useful (0 votes)
209 views

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

The document provides 12 algorithms to perform various operations on arrays and numbers. These include algorithms to: 1) Check if a number is prime; 2) Find the smallest and largest of 3 numbers; and 3) Modify the above to work for N numbers.

Uploaded by

Pranita Kamble
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
209 views

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

The document provides 12 algorithms to perform various operations on arrays and numbers. These include algorithms to: 1) Check if a number is prime; 2) Find the smallest and largest of 3 numbers; and 3) Modify the above to work for N numbers.

Uploaded by

Pranita Kamble
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

1) Write down an algorithm that reads in a positive integer x and tests if it is a prime

number.

Note: (1) A number is prime if it is only divisible by 1 and itself.

(2) The integer 1 is defined as non-prime.

Program:

/*NO. IS PRIME OR NOT PRIME*/

#include<stdio.h>

#include<conio.h>

void main()

int a,x,flag=1;

//clrscr();

printf("enter the value of x");

scanf("%d",&x);

a=2;

while(a<=x-1)

if(x%a==0)

flag=0;

break;

else

{
flag=1;

a++;

if(flag==0)

printf("x is not prime");

else

printf("x is prime");

getch();

Output:

enter the value of x

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;

printf("Largest Number is : %d\n",large);

printf("Smallest Among 3 Number is : %d",small);

Output:

Enter Three Numbers


2
4
1

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

int i, n, lar,sm, elem;

printf ("Enter total number of elements n");

scanf ("%d", &elem);

printf ("Enter first number n");


scanf ("%d", &n);

lar = n;

sm=n;

for (i=1; i<= elem -1 ; i++)

printf ("n Enter another number n");

scanf ("%d",&n);

if (n>lar)

lar=n;

if (n<sm)

sm=n;

printf ("n The largest number is %d", lar);

printf ("n The smallest number is %d", sm);

return 0;

Output:

Enter total number of elements

Enter first number


4

Enter another number

Enter another number

13

Enter another number

43

Enter another number

90

The largest number is 90

The smallest number is 4

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;

void convert(int value)

char *first[20] = {"ZERO", "ONE", "TWO",


"THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE","TEN",

"ELEVEN","TWELVE","THIRTEEN","FOURTEEN","FIFTEEN","SIXTEEN","SEVENTEEN","EIGHTTEEN","NI
NETEEN"};

char *second[10] = {"", "TEN", "TWENTY",


"THIRTY","FORTY","FIFTY","SIXTY","SEVENTY","EIGHTY","NINETY"};

if(value<0)

cout<<" ";

convert(-value);

else if(value >= 20)

cout << second[value / 10];

if(value % 10)

cout << " ";


convert(value % 10);

else

cout<<first[value];

return;

int main()

int number;

cout<<"Enter a 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:

BMI = weight / (height*height)

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.

10) Write down an algorithm which will perform following functionality

Read an integer array

Display above array

Find out sum of all elements of an array

Find out mean of an array


Find out standard deviation of an array

Find out probability of occurrence of every element in an array

Draw histogram

10) Write down an algorithm which will perform following functionality

Read an integer array

Display above array

Find out sum of all elements of an array

Find out mean of an array

Find out standard deviation of an array

Find out probability of occurrence of every element in an array

Draw histogram

11) Write down an algorithm to

Read and display Float array

Reverse an array

Check whether array is a palindrome

Sort an array

Search an element in an array

Find out how many times a given element is appearing in an array

12) Write an algorithm to perform following operations on an array :-

Insert element at nth position in an array. Please note it should insert and not overwrite.

Delete an element at a given position

Display an array. Take care of proper validations.

You might also like