DOC-20241027-WA0015.
DOC-20241027-WA0015.
Experiment No. 3
Program 1
PROBLEM
STATEMENT: Write a function which takes a range as input. Print all the numbers in the range with ‘*' in
front of prime numbers only.
Example:
Print a table as follows
1 2* 3* 4 5* ... 10 11* 12 13*
14 15 ... 20 upto 100. All
primes are starred.
ALGORITHM:
1. Start (Main function)
2. Input first and last numbers for range of numbers from the user
3. Repeat steps 4 till 8 until first number<=last number
4. Call function y=prime(f)
5. If first number=1 print 1
6. If y=0 print first number
7. If y is not equal to 0 print (*first number)
8. Increment first number
9. End
void main()
{
int f,l,y,flag;
printf("Enter the range of numbers ");
scanf("%d %d",&f,&l);
for(f;f<=l;f++)
{ y=prime(f);
if(f==1)
{
printf("1 ");
}
else
{
if(y==0)
{ printf("%d ",f);
}
else
{ printf("*%d ",f);
}
}
}
}
int prime(int n)
{ int flag=1;
for(int i=2;i*i<=n;i++)
{
if(n%i==0)
{
flag=0;
}
}
return flag;
}
RESULT:
Program 2
PROBLEM
STATEMENT: Write a function which takes as parameters two positive integers and returns TRUE if the
numbers are amicable and FALSE otherwise. A pair of numbers is said to be amicable if the
sum of divisors of each of the numbers (excluding the no. itself) is equal to the other
number.Ex. 1184 and 1210 are amicable.
ALGORITHM: 1. Start (Main Function)
2. Input two numbers from the user num1 and num2
3. Initialize two variables temp1=num1 and temp2=num2
4. Call sum function and update num1 to sum(num1)
5. Call sum function and update num2 to sum(num2)
6. Call function isamicable with temp1, num2, num1, temp2 respectively as arguments
7. End
PROBLEM Write a function that prints the sum of the series ( 1!/1+2!/2+3!/3+. .. +n!/n ). Take n as the
STATEMENT: input and return sum of series as the output. ( 3! = 3*2*1).
ALGORITHM: 1. Start (Main function)
2. Input number x from the user
3. Initialize sum to 0 and j to 1
4. Repeat step 5 and 6 till j<=x
5. Call series function and update sum=sum+series(j)
6. Increment j
7. Print value of sum
8. End
#include<stdio.h> int
factorial(int n); int
series(int a);
void main()
{ double x,y,sum=0;
printf("Enter a number ");
scanf("%lf",&x); for(int
j=1;j<=x;j++)
{ sum=sum+series(j);
}
printf("The sum of series is %.2lf\n",sum);
}
int factorial(int n)
{ int f=1;
for(int i=1;i<=n;i++)
{
F=f*I; }return
f;} int
series(int a)
{int term=0; term=factorial(a)/a;
return(term);
}
RESULT:
Program 4
PROBLEM
Write a function to find out whether given numbers are relatively prime or not. A number is
STATEMENT:
relatively prime if the ‘1’ is the only common factor between the two numbers. For example:
9 and 8 are relatively prime. (9 =1x3x3 and 8=1x2x2x2).
RESULT:
Program 5
123
12345
1234567
123456789
Take n as input and print the respective pattern as the output. For above example n=5. Let’s
take n=3 then the pattern will be :
123
12345
ALGORITHM:
PROBLEM
STATEMENT:
A common method of evaluating powers is
merely to perform repeated
multiplications. A more efficient
method of evaluating XN is possible:
If M is odd then
Replace M by M/2;
{if(m%2==1)
{product*=b;}
m=m/2; b=b*b; }
return product;
}
RESULT: