In: Computer Science
write pseudocode for the following problems using if
while conditions
A student buys various books at the start of the
semester from a bookshop. Write pseudocode for a program which
takes the total amount spent on books and the total number of books
as input and outputs the average cost per book.
Write pseudocode for a program which takes a year as
input (e.g. 2014) and determines whether or not it is a leap year.
A leap year is a multiple of 4, and if it is a multiple of 100, it
must also be a multiple of 400.
Write pseudocode for a program which takes 3 integer
values, a b c, as input and prints their sum. However, if one of
the values is the same as another of the values, it does not count
towards the sum.
When squirrels get together for a party, they like to
have nuts. A squirrel party is successful when the number of nuts
is between 40 and 60, inclusive. Unless it is the weekend, in which
case there is no upper bound on the number of nus. Write pseudocode
of a program which inputs the number of nuts consumed at a party,
and prints “True” if the party with the given values is successful,
or “False” otherwise.
Write pseudo code that will perform the
following.
Read in 5 separate numbers.
Calculate the average of the five numbers.
Find the smallest (minimum) and largest (maximum) of
the five entered numbers.
Write out the results found from steps b and c with a
message describing what they are.
Write pseudo code that reads in three numbers and
writes them all in sorted order.
You are driving a little too fast, and a police
officer stops you. Write pseudocode to compute the result, encoded
as an integer value: 0=no ticket, 1=small ticket, 2=big ticket. If
speed is 60 or less, the result is 0. If speed is between 61 and 80
inclusive, the result is 1. If speed is 81 or more, the result is
2. Unless it is your birthday -- on that day, your speed can be 5
higher in all cases.
Write pseudocode for a program that prints the numbers
from 1 to 100. But for multiples of three print “Fizz” instead of
the number and for the multiples of five print “Buzz”. For numbers
which are multiples of both three and five print
“FizzBuzz”.
Iterations
Write a loop to print this fencepostpattern. Such
fencepost loops can be created by placing one post (i.e. |) outside
your loop, and then alternating between wires (i.e. ==) and posts
inside the loop.
|==|==|==|==|
Write pseudocode for a program that takes an integer
and uses a fencepost loop to print the factors of that number,
separated by the word "and". For example, for the number 24, it
should print the following output.
1 and 2 and 3 and 4 and 6 and 8 and 12 and 24
Write pseudocode for a program which repeatedly reads
integers from the user, stopping only when the user enters -1, and
returns the largest value input.
write a program that calculates the sum of numbers
entered by a user one at a time. The program terminates when user
enters -1.
Write pseudcode for a program that calculates the area
of simple shapes. The Shape given as input can be circle, square,
or right triangle.
Extend the previous program such that the users can
calculate the area (of the 3 specified shapes) as many times as
they want.
Write PC that takes in a +ve number N and prints the
square of size N composed of '*'.
Write PC that takes in a +ve number N and determines
if it is a prime or not.
Q4. can be calculated from the infinite series
given below:
=4- 43+ 45- 47+ 49- 411+
Write a pseudo code that prints the value of after the first 100 terms, 200 terms and 300 terms. Write one pseudo code that prints all three values. Don’t write 3 different codes.
Write a program that takes in a +ve integer and prints
its break up in terms of units, tens, hundreds etc. The number is
not longer than 4 digits.
Input 2000 Output: 0 units, 0
tens, 0 hundreds, 2 thousands
Input 173 Output:
3 units, 7 tens, 1 hundreds
Write a program to output the individual digits of a
number from right to left with spaces added in between each digit.
For example if the number is 9867 then your program should output:
7 6 8 9
Repeat the above so that the digits are printed
from left to right. For example for 9867 the output should be 9 8 6
7. The maximum number you have to cater for is 105. The leading
zeros should NOT be printed so if the number is 105 the output
should be 1 0 5 and NOT 0 0 0 1 0 5.
Write a program that inputs numbers from the user till
the user inputs a negative number. The program should then print
the second maximum of all numbers and second minimum of all numbers
that were input. The last negative number should not count towards
the minimum. For example if the input is: 10 7 42 2 2 0 56 6
-1
The output should be: second maximum is 10 and second minimum is
2
#include<iostream>
#include<iomanip>
using namespace std;
int main(){
float number_of_books;
float total_cost;
cout<<"Enter total amount spent on books"<<endl;
cin>>total_cost;
cout<<"Enter number of books"<<endl;
cin>>number_of_books;
float average = total_cost/number_of_books;
cout<<fixed<<setprecision(2)<<average;
return 0;
}
//---------------------leap year
#include <iostream>
using namespace std;
int main()
{
int year;
cout << "Enter a year: ";
cin >> year;
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0)
cout << year << " is a leap year.";
else
cout << year << " is not a leap year.";
}
else
cout << year << " is a leap year.";
}
else
cout << year << " is not a leap year.";
return 0;
}
//----------------------------
//sum of three numbers
#include <iostream>
using namespace std;
int main()
{
int a, b, c;
cin >> a >> b >> c;
int sum = a + b + c;
if (a == b && a != c)
{
sum = sum - a;
}
if (a == c && a != b)
{
sum = sum - a;
}
if (b == c && a != c)
{
sum = sum - b;
}
if (a == b && a == c)
{
sum = a;
}
cout<<"Sum :- "<<sum;
return 0;
}
//-------------------------------------------
//nuts party
#include <iostream>
using namespace std;
int main()
{
int number_of_nuts;
cout << "Enter the number of nuts consumed at a party" << endl;
cin >> number_of_nuts;
if (number_of_nuts >= 40)
{
cout << "True";
}
else
{
cout << "False";
}
return 0;
}
//----------------
//-------------------------
// //five number operation
#include <iostream>
using namespace std;
int main()
{
int arr[5];
cout << "Enter five numbers" << endl;
int i = 0;
while (i < 5)
{
cin >> arr[i];
++i;
}
float average = 0;
float sum = 0;
i = 0;
while (i < 5)
{
sum = sum + arr[i];
++i;
}
average = sum / 5;
cout << "Average of given five numers :- " << average << endl;
i = 1;
int min = arr[0], max = arr[0];
while (i < 5)
{
if (min > arr[i])
min = arr[i];
if (max < arr[i])
max = arr[i];
++i;
}
cout << "Minimum number :-" << min << endl;
cout << "Maximum Number :-" << max << endl;
return 0;
}
//-------------------------------
//three number sorting
#include<iostream>
using namespace std;
int main(){
int array[5]; //here we are use array for easy way to sort number
int n = 3;
cout<<"enter three numbers"<<endl;
int i=0;
while (i<n) {
cin>>array[i];
++i;
}
//here am using bubble sort algorith
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n-1-i; j++)
{
if(array[j] > array[j+1])
swap(array[j],array[j+1]);
}
}
i = 0;
cout<<endl<<"Sorted numbers"<<endl;
//after sorting printing array of three numbers
while (i<n)
{
cout<<array[i]<<" ";
++i;
}
return 0;
}
//for aother question, please post question again left question.