0% found this document useful (0 votes)
2 views

DOC-20241027-WA0015.

The document outlines a coding assignment with multiple programming problems involving functions, algorithms, and flowcharts. It includes detailed problem statements, algorithms, and sample C programs for tasks such as identifying prime numbers, checking amicable numbers, calculating series sums, determining relative primality, printing patterns, and calculating powers efficiently. The conclusion emphasizes the learning experience gained from applying programming concepts and the importance of clean code.

Uploaded by

nknows1607
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

DOC-20241027-WA0015.

The document outlines a coding assignment with multiple programming problems involving functions, algorithms, and flowcharts. It includes detailed problem statements, algorithms, and sample C programs for tasks such as identifying prime numbers, checking amicable numbers, calculating series sums, determining relative primality, printing patterns, and calculating powers efficiently. The conclusion emphasizes the learning experience gained from applying programming concepts and the importance of clean code.

Uploaded by

nknows1607
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Name Prachi Singh

UID no. 2024800119

Experiment No. 3

AIM: To Apply the concept of functions to incorporate modularity.

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

1. Start (prime function)


2. Take argument which is an integer n
3. Initialize flag to 1 and i to 2
4. Repeat steps 5 to 7 until i*i<=n
5. If n%i is equal to 0 update flag to 0
6. Return flag
7. Increment i
8. End
FLOWCHART:
PROGRAM: #include<stdio.h>
int prime(int n);

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

1. Start (isamicable funtion)


2. Take four integer arguments int x, int y, int z, int w
3. If x=y and w=z print “True”
4. Else print “False”
5. End

1. Start (sum funtion)


2. Take argument integer n
3. Initialize s to 0 and i to 1
4. Repeat steps 5 to 7 until n
5. If n%i=0
6. Update s to s+i
7. Increment i
8. Return s
9. End
FLOWCHART:
PROGRAM: #include<stdio.h>
int sum(int n);
int isamicable(int x, int y, int w, int z);
void main() {
int num1,num2,temp1,temp2;
printf("Enter two numbers ");
scanf("%d %d",&num1,&num2);
temp1=num1;
temp2=num2;
num1=sum(num1);
num2=sum(num2);
isamicable(temp1, num2, temp2, num1); }
int isamicable(int x, int y, int w, int z) {
if(x==y && w==z)
{ printf("True\n"); }
else { printf("False\n"); } }
int sum(int n) {
int s=0; for(int i=1;i<n;i++)
{ if(n%i==0) { s=s+i; } }
return s; }
RESULT:
Program 3

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

1. Start (series function)


2. Take an integer argument a
3. Initialize term to term=factorial(a)/a
4. Return term
5. End

1. Start (factorial function)


2. Take argument n
3. Initialize f to 1 and i to 1
4. Repeat steps 5 and 6 until i<=n
5. Update f to f=f*i
6. Increment i
7. Return f
FLOWCHART:
PROGRAM:

#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).

ALGORITHM: 1. Start (Main function)


2. Input two numbers num1 and num2
3. Initialize x=gcd(num1, num2) by calling gcd function
4. If x=1 print ‘numbers are relatively prime’
5. Else print ‘numbers are not relatively prime
6. End

1. Start (gcd function)


2. Take two arguments int a and int b
3. Repeat steps 4 and 5 while rem=a%b is not equal to 0
4. Initialize temp to temp=b
5. Update b=rem and a=temp
6. Return b
7. End
FLOWCHART:
PROGRAM: #include<stdio.h>
int gcd(int a, int b);
void main(){
int num1,num2,x;
printf("Enter two
numbers ");
scanf("%d
%d",&num1,&num2
);
x=gcd(num1,num2);
if(x==1)
{printf("The
numbers are
relatively prime\n ");
}
else
{printf("The
numbers are not
relatively prime
\n");}
}int gcd(int a, int b)
{int temp,rem;
while((rem=a%b)!=
0)
{temp=b;
b=rem;
a=temp;
} return
b;
}

RESULT:
Program 5

PROBLEM Write a function that prints this pattern below : 1


STATEMENT:

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:

1. Start (Pattern Function)


2. Take argument integer n
3. Initialize I to 0
4. Repeat steps 5 to 13 until i<=n
5. Initialize j to 0
6. Repeat step 7 and 8 until j<=2*(n-i)
7. Print a space “ ”
8. Increment j
9. Repeat steps 10 and 11 until j<=2*i-1
10. Print value of j
11. Increment j
12. Print on next line
13. Increment i
14. End
FLOWCHART:
PROGRAM: #include <stdio.h> void
print_pattern(int n) { for
(int i = 1; i <= n; i++) {
for (int j = i; j < n; j++) {
printf(" ");
}
for (int j = 1; j <= (2 * i - 1); j++) {
printf("%d ", j);
}
printf("\n");
}
} int
main() {
int n;
printf("Enter a positive integer n: ");
scanf("%d", &n);
if (n < 1) {
printf("Please enter a positive integer greater than 0.\n");
return 1;
}
print_pattern(n);
return 0;
}
RESULT:-
PROGRAM 6

PROBLEM
STATEMENT:
A common method of evaluating powers is
merely to perform repeated
multiplications. A more efficient
method of evaluating XN is possible:

Initialize PRODUCT to 1, POWER to X and


M to N. While M is non-zero repeat the
following task:

If M is odd then

Replace PRODUCT by PRODUCT * POWER


End If;

Replace M by M/2;

Replace POWER by POWER * POWER End


task.

The required result is then in POWER.

Write a function which represents this


procedure for calculating X^N.
ALGORITHM:

1. Start (Main function)


2. Input number integer b and m from the user
3. Call power function and store value in x by x=power(b,m) 4. Print the value of x
5. End

1. Start (Power function)


2. Take two arguments int m and int b
3. Initialize product to 1
4. Repeat steps 5 and 6 till m is not equal to 0
FLOWCHART:
PROGRAM: #include<stdio.h> int power(int m,int b); void
main(){ int product=1,m,b,x; printf("Enter the
number and power respectively \n"); scanf("%d
%d",&m,&b); x=power(b,m); printf("The %d ^%d is
%d",m,b,x);
} int power(int m,int
b)
{int product=1; while(m!=0)

{if(m%2==1)
{product*=b;}
m=m/2; b=b*b; }
return product;
}

RESULT:

In conclusion, this coding assignment provided a valuable opportunity to apply


programming concepts and problem-solving skills. It reinforced understanding of key
principles such as algorithm design, debugging, and code optimization.
CONCLUSION:
The project not only deepened technical skills but also highlighted the importance of writing clean,
maintainable code. Overall, it was a beneficial exercise in both practical coding and theoretical
understanding.

You might also like