C++ Seekg to Read in First Nine Numbers

C++ Programme to Count Positive, Zero and Negative Numbers



In this article, you will learn and get code on counting of positive, zero, and negative numbers in C++. The programme is created in following ways:

  • Count Positive, Zero, and Negative numbers output of 10 given Numbers by User
  • Let user to ascertain the size for the Assortment
  • Count, Positive, Zero, and Negative numbers using while loop

Count Positive, Negative and Zero in C++

The program given beneath counts the occurrence (frequency) of positive, nothing, and negative numbers from the given set of numbers entered by the user (at run-fourth dimension) in C++ programming.

The question is, write a program in C++ that count and prints frequency of positive, zippo, and negative numbers. The respond to this question is given below:

            #include<iostream>            using namespace            std;            int            main() {            intpos=0, neg=0, zer=0, i, arr[10];            cout<<"Enter x Numbers: ";            for(i=0; i<10; i++)            cin>>arr[i];            for(i=0; i<10; i++)     {            if(arr[i]>0)             pos++;            else if(arr[i]==0)             zer++;            else            neg++;     }            cout<<"\northFrequency of Positive Numbers: "<<pos;            cout<<"\nFrequency of Cipher: "<<zer;            cout<<"\nFrequency of Negative Numbers: "<<neg;            cout<<endl;            render            0; }

This programme was build and run under Code::Blocks. Here is its sample run:

C++ program count positive negative zero

Now supply any 10 numbers say 1, 0, -3, iv, 5, 0, -8, 9, 6, 0 and press ENTER key to see the occurrence or frequency of positive, aught, and negative numbers as shown in the output given below:

count positive negative numbers c++

In above program, when user enters x numbers (for case: 1, 0, -3, 4, 5, 0, -8, 9, half dozen, 0), and so all the ten numbers gets initialized to arr[] in following style:

  • arr[0]=i
  • arr[1]=0
  • arr[2]=-3
  • arr[3]=four
  • and and then on, upto
  • arr[9]=0

Now the dry out run of above program with these 10 values goes like:

  • Initial values, pos=0, neg=0, zer=0
  • Because the kickoff for loop gets executed and 10 numbers gets received by user
  • So its time to execute the second for loop
  • Its initialization function execute at get-go and merely at once. And so i=0
  • At present the condition, i<ten or 0<10 evaluates to be true, therefore program flow goes within the loop
  • The condition of if, that is, arr[i]>0 or arr[0]>0 or 1>0 evaluates to be truthful, therefore plan menses goes within if'south body and the value of pos gets incremented. Becuase pos's previous value is 0, so now pos=1
  • Because the condition of if evaluates to exist true, therefore else if and else block will non gets executed
  • So plan menses goes back, increments the value of i (now i=1) and evaluates the condition of for loop again
  • That is, the condition i<10 or 1<10 again evaluates to be true, therefore program period over again goes inside the loop
  • And evaluates the status of if, that is, the condition, arr[i]>0 or arr[i]>0 or 0>0 evaluates to be false, therefore program menstruation evaluates the condition of else if, that is the condition arr[i]==0 or arr[1]==0 or 0==0 evaluates to be true, therefore program menses goes to else if's body and the value of zer gets incremented. Because zer'southward previous value is 0, so now zer=1
  • At present once again program flow goes back, increments the value of i (now i=two) and evaluates the status of for loop again.
  • This process continues until the condition of for loop evaluates to be imitation
  • On continuing the dry run of this for loop, here are the values (we'll get) afterward each evaluation:
    • pos=1
    • zer=one
    • neg=i
    • pos=ii
    • pos=iii
    • zer=2
    • neg=two
    • pos=four
    • pos=5
    • zer=3
  • Afterward exiting from the loop, print the value of each variable corresponding to positive, zero and negative numbers
  • That is, the variable, pos (corresponds to positive number'southward occurrence) holds its value equally v. Then at that place are total of 5 positive numbers bachelor in the list
  • Similarly, therefore total of 3 zeros and ii negative numbers in the listing

Allow User to Define the Size for Array

This program allows user to define the size for the array. That is, how many number, he/she wants to enter, and then notice and print the occurrence of positive, cipher, and negative numbers.

            #include<iostream>            using namespace            std;            int            primary() {            intpos=0, neg=0, zer=0, i, arr[100], tot;            cout<<"Enter the Size (max. 100): ";            cin>>tot;            cout<<"Enter "<<tot<<" Numbers: ";            for(i=0; i<tot; i++)     {            cin>>arr[i];            if(arr[i]>0)             pos++;            else if(arr[i]==0)             zer++;            else            neg++;     }            cout<<"\nFrequency of Positive Numbers: "<<pos;            cout<<"\nFrequency of Zero: "<<zer;            cout<<"\northwardFrequency of Negative Numbers: "<<neg;            cout<<endl;            return            0; }

Below is its sample run with user input, 5 as size, and ane, -2, 3, 0, 4 as 5 numbers:

count positive and negative numbers in array c++

Annotation - At the time of receiving numbers, the above plan counts positive, aught, and negative numbers. That is, above programme does both the job of receiving and counting with only one for loop

Using while Loop

This plan uses while loop to receive the input (list of numbers) and counts the occurrence of positive, nix, and negative numbers.

            #include<iostream>            using namespace            std;            int            main() {            intpos=0, neg=0, zer=0, i=0, arr[100], tot;            cout<<"Enter the Size (max. 100): ";            cin>>tot;            cout<<"Enter "<<tot<<" Numbers: ";            while(i<tot)     {            cin>>arr[i];            if(arr[i]>0)             pos++;            else if(arr[i]==0)             zer++;            else            neg++;         i++;     }            cout<<"\nFrequency of Positive Numbers: "<<pos;            cout<<"\nFrequency of Zero: "<<zer;            cout<<"\northwardFrequency of Negative Numbers: "<<neg;            cout<<endl;            render            0; }

This program produces the aforementioned output as of previous program.

Same Programme in Other Languages

  • C Count Positive, Cipher and Negative Numbers
  • Java Count Positive, Zero and Negative Numbers

C++ Online Exam


« Previous Plan Side by side Plan »




crosbyarlden.blogspot.com

Source: https://codescracker.com/cpp/program/cpp-program-count-positive-negative-zero.htm

0 Response to "C++ Seekg to Read in First Nine Numbers"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel