Write a C++ program that reads N positive integer numbers from the keyboard, calculates and displays the
following information.
a. Count of all numbers which are greater than 1 and less than 11. If no such numbers are entered, 0 for the
count value should be displayed.
b. Count of all prime numbers. The prime numbers are numbers which are divisible by 1 and itself. If no
prime numbers are entered, 0 for the count value should be displayed.
c. Average of all prime numbers which are greater than 5. If no prime numbers greater than 5 are entered, 0
for the average value should be displayed.
d. Largest number of all numbers entered from the keyboard.
e. Ascending order of two smallest numbers entered from the keyboard.
f. Value of A [where A =3.14*Rad*Rad; Rad – average of all numbers entered from the keyboard].
N is a constant and its value is 8.
number. Assume N=3, if the largest digit of your student id number is less than 3.
The program should contain only a main function. User defined functions and arrays should not be used. classes and linked lists should not be used.

Recommended Answers

All 12 Replies

>>how to write this program

Take a pen, write it. Sorry to be rude. But please show some effort from your side. Its not fair on your part to just post a question like that and expect answers to show up.

Paste whatever code you have written, or whatever ideas you have. We can try to help.

Okay, I did it. Now it's your turn.

You need to make an attempt before someone can help you with your code.

EDIT: beaten by myk, but you get the idea.

i am a beginner and trying to learn cpp. i tried to write two programs one to know the prime numbers and the other to know the count of all numbers between 1 and 11

void main(void)
{

    int num,i; 

    cout<< "Please enter the number ";
    cin >> num; i =2 ; 
    if(num>=2)
    {
    while(1){

        if (num%i==0) {

            if(num == i){

                cout << "Prime Number"; break; 

            } else {

                cout << "Not Prime"; break; 

            } 

        } else {

            i++; 

        } 

    } 

}
    else
        cout<<"not a prime";

}

and

void main (void)
{

    const int n=8;

    for (int i=0;i<n;i++)
    {
        int a;
        cin>>a;
        if(a>1&&a<11)
        {
            int b=0;
            b=b+a;
            x=x+b;
        }



    }
    cout<<"the value of x is :"<<x;
}

so how to combine these now...
..
thanks for your help

thanks myk...i have tried to do the tasks individually and then combine them to get the result and i need some help.

thanks jonsca..i am trying to solve it ..i need your help

That's much better :)

Well, a few things:

1) To test for prime, i feel this would be a better method:

for (i = 2 to n / 2) {
    if (num % i == 0) then not prime
}

2) The return type of main() is int.

3) You can use a function to combine the two tasks. ie

say,

bool isPrime(int num) 
{
    // test for prime and return value
}
int countPrimes(int values)
{
   // Here, iterate from 1 to values (a for loop would suffice)
     if (isPrime(iterating_variable) {
           count++;
     }
   // endfor

   return count;
}

PS: Use [ CODE ] tags for pasting code.

Oh and i guess i got it wrong. Did you mean sum of numbers? Or count of prime numbers between 1 and 11?

the program should display count of all the numbers greater than 1 and less than 11, and it should also display the sum of all prime numbers entered..
thanks myk

so i should use this code to get the combined effect

  int countPrimes(int values)
  {
  // Here, iterate from 1 to values (a for loop would suffice)
  if (isPrime(iterating_variable) {
  count++;
  }
  // endfor

  return count;
  }

myk i should not use any user defined functions as well. so what should i do now

Well, i had got the problem wrong.

So, basically this is what you need:

// Read input and store it in say, N.
     a)check if it is within 1 and 11.
         if yes, countNum = countNum + 1

     b)check if it is prime, using isPrime(N)
         if yes, primeSum += N
// end

So, that is basically what you will need. How the loop terminates is your wish. ie when you stop taking input is upto you.

>>i should not use any user defined functions as well.

Ah, in that case, just replace whatever is in the function, inside the loop ie

// Read input and store it in say, N.
     a)check if it is within 1 and 11.
         if yes, countNum = countNum + 1

     b)check if it is prime
         // Code for finding if it is a prime. basically loop from i = 2 to N / 2.
         // So, if you find it is prime, primeSum += N
// end

thanks myk...but i am unable to fit every thing into one program. we need to check whether whether the number entered is >5 and prime, take the average, etc..all in one program. so i am unable to do it. i can do them individually but not everything together. anyway i will try for some time and then come back to you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.