unsigned binary_to_gray (unsigned num) { return num ^ (num >> 1); } unsigned gray_to_binary (unsigned num) { /* note: assumes num is no more than 32-bits in length */ num ^= (num >> 16); num ^= (num >> 8); num ^= (num >> 4); num ^= (num >> 2); num ^= (num >> 1); return num ; }
1,953,125
An easy way to calculate all the factors of whole numbers is to iterate from 1 to that number and check if the remainder is zero using the "%" operation. def getFactors(num): """Return the factors of a whole number""" factors = [] for i in range(1,num+1): if num%i == 0: factors.append(i) return factors Factoring negative numbers and zero simply requires a few more tweaks to the code.
num 1 is predator num 2 is the beast num 3 is mimi num 4 is peachy and num 5 is centi
Dim x As String x = "HELLO" Dim tmpString As String tmpString = Nothing For j = 0 To x.Length - 1 Dim int int=x.Length - 1 int=int-j tmpString=x(j) Next
Such a method could be: public int NumberOfDigits(int num) { num = num/10; if( num == 0) return 1; return ( 1+NumberOfDigits(num) ); }
#include <stdio.h> int main() { int fact=1,num; printf("Enter number: "); scanf("%d",&num); if((num==0) (num==1)) { printf("Factorial = %d",fact); return 0; } else { while(num!=1) { fact=fact*num; num--; } printf("Factorial = %d",fact); return 0; } }
You can create the factorial of desired number. Try this code: Private Sub Command1_Click() Dim fact As Integer fact = 1 Dim num As Integer Dim i As Integer num = InputBox("Enter Number..") For i = 1 To Val(num) fact = fact * num num = num - 1 Next Label1.Caption = fact End Sub
1969
#include<stdio.h> #include<conio.h> void main() { int num; for(num=1;num<=100;num++) { printf("%d",num); } getch(); }
#include<iostream> #include<sstream> #include<exception> std::string decimal_to_roman (unsigned num) { std::stringstream ss {}; while (num>0) { if (num>10000) throw std::range_error ( "ERROR: decimal_to_roman (unsigned num) [num is out of range]"); else if (num==10000) { ss<<"[M]"; num-=10000; } else if (num>=9000) { ss<<"[CM]"; num-=9000; } else if (num>=5000) { ss<<"[D]"; num-=5000; } else if (num>=4000) { ss<<"[CD]"; num-=4000; } else if (num>=1000) { ss<<"M"; num-=1000; } else if (num>=900) { ss<<"CM"; num-=900; } else if (num>=500) { ss<<"D"; num-=500; } else if (num>=400) { ss<<"CD"; num-=400; } else if (num>=100) { ss<<"C"; num-=100; } else if (num>=90) { ss<<"XC"; num-=90; } else if (num>=50) { ss<<"L"; num-=50; } else if (num>=40) { ss<<"XL"; num-=40; } else if (num>=10) { ss<<"X"; num-=10; } else if (num==9) { ss<<"IX"; num-=9; } else if (num>=5) { ss<<"V"; num-=5; } else if (num==4) { ss<<"IV"; num-=4; } else if (num>=1) { ss<<"I"; num-=1; } } return ss.str(); } int main (void) { for (unsigned n=1; n<=10000; ++n) { try { std::cout << n << "\t = " << decimal_to_roman(n) << std::endl; } catch (std::range_error& e) { std::cerr<<e.what()<<std::endl; break; } } }
#include<stdio.h> #include<math.h> bool is_prime(unsigned num) { unsigned max, factor; if (num<2) return false; if (!(num%2)) return num==2; max = (unsigned) sqrt((double)num) + 1.0; for (factor=3; factor<max; ++factor) if (!(num%factor)) return false; return true; } int main() { unsigned num; for (num=1; num<=100; ++num) if (is_prime(num)) printf ("%u is prime\n", num); return 0; }