There is heuristic approach by dividing all data set into equal number points. suppose you have 3 classes k divide by n objects/k where k=3 so equal number of objects will be distributed into classes or clusters k=3. This approach alternative is rules of thumb which is similar k=(n/2)^1/2.
For K-means algorithm is unsupervised (Clustering) . The number of clusters are guess and randomly choose. But follow based on following conditions. If you have 'n' objects number of clusters maximum 'n' when each object belongs to independent cluster. if number of clusters minimum 1 if all objects are belongs to single cluster.
apart from this between 1 and N you may choose any value randomly.
When k-means is used for data quantization, the number of classes is established by satisfying a condition on quantization error amplitude. This works well especially for uniformly distributed random samples. If this is not the case (i.e. the data have an observable natural grouping tendency), the histogram shows some agglomeration points (at different 'scales') as points of local maximum. Counting these points in a fuzzy manner (by neglecting the local maximum points that are not "so intense" - until optimization goal is best fitted) is a good practical manner to answer your question.
There are also 'classical' rules such as thumb rule, elbow rule, average silhouette method, Akaike / Bayesian / Deviance information criteria, jump method and cross-validation (when you have a goal function to optimize and big enough data set that can be split in smaller sub-parts such that to obtain an agreement on the number of clusters that best fits the optimization goal across all sub-parts). However, their performance depends mainly on the nature of data.