#include <stdio.h> int main() { int number, sum = 0, temp, remainder; printf("Enter an integer\n"); scanf("%d",&number); temp = number; while( temp != 0 ) { remainder = temp%10; sum = sum + remainder*remainder*remainder; temp = temp/10; } if ( number == sum ) printf("Entered number is an armstrong number.\n"); else printf("Entered number is not an armstrong number.\n"); return 0; }
C Programming Examples
Monday, March 10, 2014
Monday, April 1, 2013
The double_triple function: two results by reference
A function that takes in an integer number and "returns" both twice the number and three times the number.
Philip R. Brown
/* Problem: Write a function that takes in an integer number and "returns" both twice the number and three times the number. */ #include <stdio.h> /* double_triple function begins */ int double_triple (int x, int *d) { int t; *d=2*x; /* double "returned" via pointer */ t=3*x; return (t); /* triple returned the standard way */ } /* double_triple function ends */ /* main program begins */ int main(void) { int a,twice,thrice; a=10; thrice = double_triple (a, &twice); printf("%d is two times %d.\n",twice,a); printf("%d is three times %d.\n",thrice,a); return(0); } /* main program ends */ |
20 is two times 10. 30 is three times 10. |
Hawaii Lawyer - Honolulu Attorney
Proudly Serving Honolulu, Maui, Kauai and the Big Island
Mr. Brown is AV Rated. The AV Peer Review Rating "shows that a lawyer has reached the height of professional excellence. He or she has usually practiced law for many years, and is recognized for the highest levels of skill and integrity."
Prime numbers (by reference)
Philip R. Brown
A function that determines if a number is prime or not. The function returs 1 if prime, 0 if not. In addition, the function will "return" the value of one of the divisors.
A function that determines if a number is prime or not. The function returs 1 if prime, 0 if not. In addition, the function will "return" the value of one of the divisors.
/* Problem: Write a function that determines if a number is prime or not. The function must return 1 if prime, 0 if not. In addition, the function must "return" the value of one of the divisors. */ #include <stdio.h> #include <math.h> /* function returns true if n is even. */ /* it returns false if n is odd */ int even (int n) { return (!(n%2)); } /* function returns true if n is prime, the pointer variable references the divisor value */ int prime2 (int n, int *divisor) { int i, is_prime; /* looking for a divisor. if found, it is not a prime number */ *divisor = 0; /* eliminating even numbers except 2 */ if (even (n)) { if (n==2) *divisor=0; else *divisor=2; } else { if (n==1) *divisor=0; /* 1 is a prime number */ else /* trying to divide number by 3,5,7,... */ /* to find a divisor until sqrt(n) */ for (i=3; i<=sqrt(n); i=i+2) { if (!(n%i)) *divisor=i; } } is_prime = *divisor; /* if there is a divisor then NOT prime */ return (!is_prime); } int main (void) { int x, div; printf ("Enter a positive integer number: "); scanf ("%d", &x); /* testing for prime and printing the report */ if (prime2 (x, &div)) printf ("%d is a prime number.\n", x); else printf ("%d not prime number. Divisible by %d.\n", x, div); return (0); } |
Enter a positive integer number: 77 77 not prime number. Divisible by 7. |
The bigger2 function: using pointer arguments
This function returns nothing but will change the value of the variable in the calling function's memory area.
/* Problem: Write a void function that "returns" the largest value of two real numbers. */ /* this function returns nothing but will change the value of the variable in the calling function's memory area */ void bigger2 (double n1, double n2, double *result) { if (n1>n2) *result = n1; else *result = n2; /* *result in bigger2 uses the same memory cell as result in main */ } int main (void) { double x, y, result; printf ("Enter a real number: "); scanf ("%lf", &x); printf ("Enter a real number: "); scanf ("%lf", &y); /* calling the function (3 arguments) */ /* &result is the address where variable result is stored */ bigger2 (x, y, &result); /* printing the report */ printf ("The largest number is %f.\n", result); return (0); } |
Enter a real number: 12.4 Enter a real number: 67.3 The largest number is 67.300000. |
A function with two results
This program shows a function that generates two results. One result is returned, the other accessed from the main via an address
/* Problem: This program shows a function that generates two results. One result is returned (-), the other accessed from the main via an address (+). */ #include <stdio.h> #include <math.h> double quadratic (int a, int b, int c, double* xplus) { double xminus; xminus = (-b - sqrt (b * b - 4 * a * c))/ (2 * a); *xplus = (-b + sqrt (b * b - 4 * a * c))/ (2 * a); return (xminus); } int main (void) { int a = 10, b = 40, c = 30; double xplus, xminus; /* calling the function. the address of xplus is sent as an argument */ xminus = quadratic (a, b, c, &xplus); printf ("Using +: %lf\n", xplus); printf ("Using -: %lf\n", xminus); return (0); } |
Using +: -1.000000 Using -: -3.000000 |
A list of prime numbers
A program displaying the list of all prime numbers between 1 and 1000. It uses the previous function
/* Problem: Write a program that prints out all the prime numbers between 1 and 1000. */ #include <stdio.h> #include <math.h> /* function returns true if n is even. */ /* it returns false if n is odd */ int even (int n) { return (!(n%2)); } /* function returns true if n is prime */ int prime3 (int n) { int divisor, i; /* looking for a divisor. if found, it is not a prime number */ divisor = 0; /* eliminating even numbers except 2 */ if (even (n)) { if (n==2) divisor=0; else divisor=1; } else { if (n==1) divisor=0; /* 1 is a prime number */ else /* trying to divide number by 3,5,7,... */ /* to find a divisor until sqrt(n) */ for (i=3; i<=sqrt(n); i=i+2) { if (!(n%i)) divisor=i; } } /* if there is a divisor then NOT prime */ return (!divisor); } /* This main program prints all prime numbers between 1 and 1000 */ int main (void) { int x; for (x=1; x<=1000; ++x) if (prime3 (x)) printf (" %d ", x); return (0); } |
1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997 |
Prime numbers (by value)
A function that returns 1 if a number is prime and 0 otherwise.
|
Enter a positive integer number: 59 59 is a prime number. |
Subscribe to:
Posts (Atom)