Jump to content

[C++] Algoritm generare toate numerele de maxim "X" cifre


Vennom
 Share

Recommended Posts

Este facut de mine deci nu este optimizat 100% insa merge. La ce e bun ? Cand ai nevoie sa lucrezi cu numere de o anumita marime.

#include <iostream>
#include <conio.h>
using namespace std;

int main() {
	int x,i,nr,y=0;
	cout << "Numar de cifre: ";
	cin >> x;

	for(i=0;y<=x;i++) {
		nr=i;
		y=0;
	while(nr!=0) { // Calculeaza numarul de cifre al numarului (variabila "i")
		y++;
		nr=nr/10;
	}

	if(y<=x) // Am pus aceasta conditie deoarece ar afisa si primul numar mai mare cu o cifra decat x. Ex: x=2 ar afisa si 100.
	cout << i << ", ";
	}
	getch();
}
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.