top of page
Writer's pictureD A Rosi Arsida Wardani

C ++ Program and Algorithm | Separate integer numbers less than 1000 into their components


 

Analysis :

To get the number hundreds, the formula is :

hundreds = number/100

To get the number tens, the formula is :

tens = (number % 100) / 10

Meanwhile, to get the units number, the formula is :

units = (number % 100) % 10

 

Algorithm integer_components :

{separate integer numbers less than 1000 into their components}

Declaration :

num, hundreds, tens, units : integer

components : string

Description :

read (num)

if (num>=100) then

hundreds ← num/100

tens ← (num%100)/10

units ← (num%100)%10

write (components)

else if (num >= 10 And num<=99) then

tens ← (num%100)/10

units ← (num%100)%10

write (components)

else

units ← (num%100)%10

write (components)

endif


 

Flowchart :

Here's the output if we input 167 :


 

C++ Program :


#include <iostream>
using namespace std;

int main(int argc, char** argv){
	int num, hundreds, tens , units;
	cout<<"Enter number : "; 
	cin>>num;
	if(num >=100){
   		hundreds = num / 100;
    	tens = (num%100)/10;
    	units = (num % 100) % 10;
 		cout<<hundreds<<" H + "<<tens<<" T + "<<units<<" U ";	  
	} else if(num >=10 && num <=99){
 		tens = (num%100)/10;
   		units = (num % 100) % 10;
   		cout<<tens<<" T + "<<units<<" U ";
	} else {
    	units = (num % 100) % 10;
  		cout<<units<<" U ";   
	}
	return 0;
}

Output :




4 views0 comments

Recent Posts

See All

One-dimensional Arrays

One-dimensional arrays are data structures that contain data types of the same type. In the form of a group of related memories...

Loop

Introduction Examples of algorithms in everyday life : Example 1 : To finish eating a plate of rice (initial conditions) Mouthfuls of a...

Comments


Post: Blog2_Post
bottom of page