OUTPUT (after running):
Enter your number: 3
Factorial of 3 = 6
Example
10: Write a C program to sum the following series:
Sum = 1 + 2 + 3 + 4 + 5 + … n
Solution 10:
The program is as follows:
#include
#include
void main( )
{
int n, sum;
clrscr( );
printf(“\nEnter the last term of the series”);
scanf(“%d”, &n);
sum = series (n);
printf(“\nSum of series = %d”, sum);
getch( );
}
series (int last)
{
int i, s=0;
for (i=1; i<=last; i++)
s+ = I;
return (s);
}
NOTE
Please run this program yourself.
Example
11: Write a C program to find the sum of the digits of an integer number. Use functions
only.
Solution 11:
The program is as follows:
#include
#include
void main( )
{
int s;
long n;
clrscr( );
printf(“\nEnter your number”);
scanf(“%ld”, &n);
s = sumdig (n);
printf(“\n Sum of digits of %ld = %d”, n, s);
getch( );
}
sumdig (long num)
{
int d, sum = 0;
while (num > 0)
{
d= num % 10;
num = num /10;
sum += d;
}
return (sum);
}
OUTPUT (after running):
Enter your number: 246
Sum of digits of 246 = 12
Example
12: Write a C program to compute the bionomial coefficient
n
C
r
using functions:
n
C
r
= n! / r! (n – r)!
Solution 12:
The program is as follows:
#include
#include
void main( )
{
int ncr;
long n, r;
clrscr( );
printf(“\n Enter value of n and r”);
scanf(“%ld%ld”, &n, &r);
ncr = fact(n) / (fact (r) * fact(n – r));
printf(“\n Value of ncr = %d”, ncr);
getch( );
}
fact (int num)
{
int i;
long f = 1;
for (i =1; i <= num; i++)
f = f * i;
return (f );
}
NOTE
Please run the program yourself.
Example
13: Write a C function to reverse a number.
Solution 13:
The program is as follows:
reverse (int n)
{
int r, rev = 0;
while (n > 0)
{
r = n % 10;
n = n/10;
rev = revr * 10 + r;
}
return (rev);
}
Example
14: Write a C program to sum the following series:
Sum = x
3
/ 3! + x
5
/ 5! + x
7
/ 7! + …
Solution 14:
The program is as follows:
#include
#include
void main( )
{
float sum = 0.0;
int t1, t2, x, i, n;
clrscr( );
printf(\n Enter the number of terms:”);
scanf(“%d”, &n);
printf(“\n Enter the value of x:”);
scanf(“%d”, &x);
for ( i = 3; i <= n; i += 2)
{
t1 = power (x, i);
t2 = fact (i);
sum = sum + (float) t1/t2;
}
printf(“\n The sum of the series = %f”, sum);
getch( );
}
power (int x, int y)
{
int i, pow = 1;
for (i=1; i <=y; i++)
pow = pow * x;
return (pow);
}
fact (int n)
{
int j, fact = 1;
for (j=1; j<=n; j++)
fact = fact * j;
return (fact);
}
Please run this program yourself.
Example
15: Write a C program to compute a
b
using recursion:
power (a, b) = 1 if b = 0
a * power (a, b-1) if b > 0
Solution 15: We use the fact that a
b
is simply the product of a and a
b-1
.
The program is as
follows:
#include
#include
void main( )
{
float power (float, int);
int b;
float a, p;
clrscr( );
printf(“\n Enter two numbers:”);
scanf(“%f %d”, &a, &b);
p = power (a, b);
printf(“\n%f raise to %d = %f”, a, b, p);
getch( );
}
float power (float x, int y)
{
if ( y = =0)
return 1;
else
return (x * power (x, y-1));
}
OUTPUT (after running):
Enter two numbers: 2 3
2 raised to 3 = 8.000000
Example
16: Consider the very popular Tower of Hanoi problem. It involves moving a specified
number of disks from one tower to another using a third, auxiliary tower. Suppose that there are
three towers A, B, and C. The game starts with the disks stacked on tower A in order of
decreasing size (i.e., the largest on the bottom and the smallest on top). The aim is to transfer
Do'stlaringiz bilan baham: |