Você está na página 1de 6

1. Write a program that uses a function to find all prime numbers from 1 to 1000. #include<stdio.

h> int prime(int n) { int j=2; while(j<=n/2) { if(n%j==0) return 0;; j++; } return 1; } int main() { int i; for(i=1;i<1000;i++) if(prime(i)) printf("%d is prime\n",i); return 0; } 2. Write a program that uses function to calculate the sum and the total number of digits of some number that we input from keyboard. #include<stdio.h> int number(int n) { int num=0; while(n>0) { num++; n/=10; } return num; } int sum(int n) { int s=0,a; while(n>0) { a=n%10; s+=a; n/=10; }

return s; } int main() { int n; printf("Enter number: "); scanf("%d",&n); printf("%d has %d digits, with total sum of %d.",n,number(n),sum(n)); return 0; }

3. Write a program that displays all 4 digit numbers that are dividable by the sum of the first two digits + the last two digits. 3417 / (34+17), 5265 / (52+65). #include<stdio.h> int sum(int); int main() { int i,br=0; for (i=1000;i<10000;i++) if(i%sum(i)==0) { br++; printf("%d\t",i); } printf("\nTotal numbers found %d.",br); return 0; } int sum(int n) { int t; t=(n/100+n%100); return t; }

4. Write a program that finds all numbers from 1 to 1000 that are prime numbers and their sum of digits is also a prime number. #include<stdio.h> int prime(int n) { int j=2; while(j<=n/2) { if(n%j==0) return 0;; j++; } return 1; } int sum(int n) { int s=0,a; while(n>0) { a=n%10; s+=a; n/=10; } return s; } int main() { int i; for(i=1;i<1000;i++) if(prime(i)&&prime(sum(i))) printf("%d\t",i); return 0; }

5. Write a function that takes two parameters x and n, and returns:

xn xn+2 x+ ,x0 n n+2 f ( x ) = n 1 n +1 x + x , x < 0 n 1 n + 1 Then write a program that gives of the results from x [4, 4] with step 0.1

#include<stdio.h> double f(float i,int j); float power(float i, int j); int main () { int n; float x; printf("Enter number: "); scanf("%d",&n); if ((n>=-2) && (n<=1)) printf("Undetermined.\n"); else { x=-4.0; while (x<=4) { printf("x=%3.1f, f(x)=%10.4f\n", x, f(x,n)); x+=0.1; } } return 0; } double f(float i,int j) { double value; if (i>0) value=i+power(i,j)/j-power(i,j+2)/(j+2); else value=-power(i,j-1)/(j-1)+power(i,j+1)/(j+1); return value; } float power(float i,int j) { int k; double value; if (i==0) value=0.0; else { value=1.0; for (k=1;k<=j;++k) value*=i; } return value; }

Recursive functions
1. Write a program that calculates the sum function to calculate

11 + 22 + 33 + ... + n n . Use recursive

mn .

#include<stdio.h> long int power(int n,int m) { if(m==0) return 1; if(m==1) return n; else return(n*power(n,m-1)); } void main() { long int s=0; int i,n; printf("Enter the number n: "); scanf("%d",&n); for(i=1;i<=n;i++) s=s+power(i,i); printf("The total sum = %d",s); }

2. Write a program that finds all of the permutations with and without replacement for taken k of n. #include<stdio.h> long int power(int n,int m) { if(m==0) return 1; if(m==1) return n; else return(n*power(n,m-1)); } long int fact(int n) {

P ( n, k ) =

n! , P ( n, k ) = n k (n k )!

if (n==1) return n; else return(n*fact(n-1)); } void main() { int i,n,k; printf("Enter the number n and k: "); scanf("%d %d",&n,&k); printf("Total permutations with replacement are %d\n",power(n,k)); printf("Total permutations without replacement are %d\n",fact(n)/fact(n-k)); } 3. Write a program that cancels a fraction with greatest common factor. Use recursive function to find the greatest common factor. #include<stdio.h> int gcf(int i, int j) { if(j==0) return i; else return (gcf(j,i%j)); } void main() { int nom,den,c; printf("Enter the nominator and denominator:"); scanf("%d %d",&nom,&den); c=gcf(nom,den); nom/=c; den/=c; printf("The result is %d/%d",nom,den); }

Você também pode gostar