Experiment 3
Experiment 3
Experiment No. 3
Program 1
PROBLEM Write a function which takes a range as input. Print all the numbers in the range
STATEMENT : 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
2. Define function prototype - int prime(int n) to check if number is prime and
another prototype - void output(int n) to display the result as given .
3. Take input from user for a number
4. Iterate for loop from 1 to the number
5. If prime(i) print i* using output function
6. Else print i
7. Stop
FLOWCHART:
PROGRAM: #include<stdio.h>
int prime(int n);
void main()
{
int r;
printf("Enter the range of numbers :");
scanf("%d",&r);
for(int i=1;i<=r;i++)
{
prime(i);
}
}
int prime(int n)
{
int flag=1;
if(n==1)
{
printf(" %d ",n);
}
else
{
for(int i=2;i*i<=n;i++)
{
if(n%i==0)
{
flag=0;
break;
}
}
if(flag==0)
{
printf(" %d ",n);
}
else
{
printf(" %d* ",n);
}
}
return (n);
}
RESULT:
Program 2
PROBLEM Write a function which takes as parameters two positive integers and returns
STATEMENT: 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
2. Define function prototype void amicable(int n1 ,int n2) and int sumdiv(int
n)
3. Take 2 integers as input from users
4. In func amicable call sumdiv to calculate sum of divisors , iterate from 1 to
x and if the number divides x , store it in sum of factors and return the sum
5. If the sum of factors of one number == other number and vice versa ,
printf(“TRUE”)
6. Else printf(“FALSE”)
7. End
FLOWCHART:
PROGRAM: #include <stdio.h>
void amicable(int n1 , int n2);
int sumdiv(int n);
int main()
{
int n1 , n2;
printf("Enter 2 numbers:\n");
scanf("%d %d" , &n1, &n2);
amicable(n1 , n2);
}
void amicable(int n1 , int n2)
{
int sum1 = sumdiv(n1) , sum2 = sumdiv(n2);
if(sum1 == n2 && sum2 == n1)
{
printf("The numbers %d and %d are amicable.\n",n1,n2);
}
else
{
printf("The numbers %d and %d are not amicable.\n",n1,n2);
{
}
int sumdiv(int n)
{
int sum = 0;
for(int i = 1 ; i < n ; i ++)
{
if(n % i == 0)
{
sum += i;
}
}
return sum;
}
RESULT:
Program 3
PROBLEM Write a function that prints the sum of the series ( 1!/1+2!/2+3!/3+....+n!/n ). Take
STATEMENT: n as the input and return sum of series as the output. ( 3! = 3*2*1).
ALGORITHM: 1. Start
2. Take n as input
3. Find n! and divide it by n
4. Print the sum of the series
5. Stop
FLOWCHART:
PROGRAM: #include <stdio.h>
int factorial(int n);
int series_sum(int n);
int main()
{
int n;
printf("Enter the number:");
scanf("%d" , &n);
int sum = series_sum(n);
printf("The result is: %d",sum);
}
int factorial(int n)
{
int fact = 1;
for(int i = 1 ; i <= n ; i ++)
{
fact = fact * i;
}
return fact;
}
int series_sum(int n)
{
int sum = 0, fact = 0;
for(int i = 1 ; i <= n ; i ++)
{
fact = factorial(i);
sum = sum + (fact / i);
}
return sum;
}
RESULT:
Program 4
PROBLEM Write a function to find out whether given numbers are relatively prime or not. A
STATEMENT: number is 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).
ALGORITHM: 1. Start
2. Declare integer variables n = 0 and fact to store factorial initialized to 1.
3. Using a for loop iterate from 1 till the input number while multiplying fact
each time with the given value of I in loop to calculate the factorial.
4. Display the calculated factorial on screen.
5. Stop.
FLOWCHART:
PROGRAM: #include<stdio.h>
int relative_prime(int n1,int n2);
void main()
{
int a,b,result;
printf("Enter 2 numbers :\n");
scanf("%d%d",&a,&b);
result=relative_prime(a,b);
}
RESULT:
Program 5
ALGORITHM: 1. Start
2. Take n as input
3. Call pattern
4. Iterate int i from 1 till n
5. Print spaces till (n-i)
6. Print numbers from 1 to 2*n – 1
7. End
FLOWCHART:
PROGRAM: #include <stdio.h>
void pattern(int rows);
int main(){
int n;
printf("Enter no: ");
scanf("%d" , &n);
pattern(n);
}
void pattern(int rows){
for(int i = 1 ; i <= rows ; i ++){
for(int spaces = 1 ; spaces <= (rows-i) ; spaces
++){
printf(" ");
}
for(int j = 1 ; j <= (2 * i)-1 ; j ++){
printf("%d " , j);
}
printf("\n");
}
}
RESULT:
Program 6
PROBLEM Write a function which represents this procedure for calculating X^N.
STATEMENT:
5)End If;
6)Replace M by M/2;
8)End task.
FLOWCHART:
PROGRAM: #include<stdio.h>
#include<stdlib.h>
int power(int x, int y)
{
int z=1;
while(y!=0)
{
if(y%2==1)
{
z=z*x;
}
y=y/2;
x=x*x;
}
return z;
}
int main()
{
int a,b,c;
printf("Enter a^b\n");
scanf("%d%d",&a,&b);
c=power(a,b);
printf("Result=%d", c);
return 0;
}
RESULT:
CONCLUSION: Functions allows programmers to break down complex problems into smaller,
more manageable parts. By organizing code into functions, it becomes easier to
understand, test, and maintain. This approach promotes reusability, reduces
redundancy, and improves the overall structure of the program. Debugging
becomes easier. It is easier to understand the program logic. Mastering the use of
functions helps in creating clean, efficient, and well-organized code that is easier
to work with and scale.