1.problem Statement: Write A C Program To Find Factorial of A Given Number Using Non Recursive Functions. Algorithm
1.problem Statement: Write A C Program To Find Factorial of A Given Number Using Non Recursive Functions. Algorithm
1.Problem Statement: Write a C Program to find factorial of a given number using non recursive
functions.
Algorithm:
1. Start
2. Input the number n (the number for which the factorial is to be calculated).
3. Initialize a variable fact to 1 (as the factorial of 0 is 1).
4. For i = 1 to n:
5. Multiply fact by i (i.e., fact = fact * i).
6. Output the value of fact (the factorial of n).
7. End
C Program:
2.Problem Statement: Write a C Program to find binary equivalent of a given decimal number using
non recursive functions.
Algorithm:
1. Start
2. Input the decimal number n.
3. Initialize an empty string or array to store the binary result.
4. While n > 0:
5. Take the remainder of n when divided by 2 (i.e., n % 2) and store it as the binary digit.
6. Divide n by 2 (i.e., n = n / 2).
7. Reverse the binary digits and display the binary number.
8. End
C Program:
3.Problem Statement: Write a C Program to co primes between 1 and 1000 using non recursive
functions.
Algorithm:
1. Find Co-Primes Between 1 and 1000
2. Algorithm:
3. Start
4. For each pair (i, j) where 1 <= i < j <= 1000:
5. Check if gcd(i, j) = 1 (using the formula for GCD).
6. If true, (i, j) are co-primes.
7. Output the co-prime pairs.
8. End
C Program:
4.Problem Statement: Write a C Program to find HCF of 2 given numbers using non recursive
functions.
Algorithm:
1. Find HCF (Highest Common Factor) of 2 Given Numbers
2. Algorithm:
3. Start
4. Input two numbers a and b.
5. While b != 0:
6. Set a = b and b = a % b.
7. The HCF will be the value of a after the loop ends.
8. Output the HCF.
9. End
C Program:
5.Problem Statement: Write a C Program to print sum of digits using non recursive functions.
Algorithm:
1. Find Sum of Digits of a Given Number
2. Algorithm:
3. Start
4. Input the number n.
5. Initialize sum = 0.
6. While n > 0:
7. Add the last digit of n (i.e., n % 10) to sum.
8. Remove the last digit from n (i.e., n = n / 10).
9. Output the sum of digits.
10. End
C Program:
#include <stdio.h>
int sum(int n){
int m,s=0;
while(n!=0){
m=n%10;
n=n/10;
s=s+m;
}return s;
}int main(){
int n;
printf("enter the number\n");
scanf("%d",&n);
printf("the sum of digits is %d",sum(n));
return 0;
}
Output:
enter the number
1234
Programming for Problem Solving Lab 24071A7256
Page no.223
DEPARTMENT OF CSE-(CYS,DS) AND AI&DS
BRANCH: ARTIFICIAL INTELLEGENCE AND DATA SCIENCE
Week no. :9
Date:2.12.2024
6.Problem Statement: Write a C Program to find GCD of 3 given numbers using non recursive
functions.
Algorithm:
1. Find GCD of 3 Given Numbers
2. Algorithm:
3. Start
4. Input three numbers a, b, and c.
5. Find GCD of a and b (using the method for 2 numbers).
6. Find GCD of the result from step 3 and c.
7. Output the GCD.
8. End
C Program:
#include <stdio.h>
int GCD(int n1,int n2,int n3){
int m,i,g;
m=(n1<n2)?n1:n2;
m=(n3<m)?n3:m;
for(i=1;i<=m;i++){
if(n1%i==0) if(n2%i==0){ if(n3%i==0){
g=i; } }}
}return g;
}int main(){
int n1,n2,n3;
printf("enter the three numbers:");
scanf("%d%d%d",&n1,&n2,&n3);
printf("%d is the GCD of the given three numbers.",GCD(n1,n2,n3)); return 0;}
Output:
enter the three numbers:12
8
Programming for Problem Solving Lab 24071A7256
Page no.224
DEPARTMENT OF CSE-(CYS,DS) AND AI&DS
BRANCH: ARTIFICIAL INTELLEGENCE AND DATA SCIENCE
Week no. :9
Date:2.12.2024
14
2 is the GCD of the given three numbers.
7.Problem Statement: Write a C Program to find LCM of 3 given numbers using non recursive
functions.
Algorithm:
1. Start
2. Input three numbers a, b, and c.
3. Find LCM of a and b using the formula:LCM
4. (𝑎,𝑏)=∣𝑎×𝑏∣GCD(𝑎,𝑏)
5. LCM(a,b)=
7. ∣a×b∣
6. GCD(a,b)
8. Find LCM of the result from step 3 and c using the same formula.
9. Output the LCM.
10. End
C Program:
8.Problem Statement: Write a C Program to find the power of a given number using non recursive
functions.
Algorithm:
1. Find the Power of a Given Number
2. Algorithm:
3. Start
4. Input the base b and the exponent e.
5. Initialize result = 1.
6. For i = 1 to e:
7. Multiply result by b (i.e., result *= b).
8. Output the value of result.
9. End
C Program:
9.Problem Statement: Write a C program to find sum of natural numbers using non recursive
function.
Algorithm:
1. Find Sum of Natural Numbers
2. Algorithm:
3. Start
4. Input the number n.
5. Initialize sum = 0.
6. For i = 1 to n:
7. Add i to sum (i.e., sum += i).
8. Output the sum.
9. End
C Program:
10.Problem Statement: Write a C program to reverse a given integer number using non recursive
functions.
Algorithm:
1. Reverse a Given Integer Number
2. Algorithm:
3. Start
4. Input the number n.
5. Initialize reverse = 0.
6. While n > 0:
7. Add the last digit of n to reverse (i.e., reverse = reverse * 10 + (n % 10)).
8. Remove the last digit from n (i.e., n = n / 10).
9. Output the reversed number.
10. End
C Program:
11.Problem Statement: Write a C program to find twin primes between 1 and 1000 using non
recursive functions.
Algorithm:
1. Find Twin Primes Between 1 and 1000
2. Algorithm:
3. Start
4. For each number i from 2 to 1000:
5. Check if i and i + 2 are both prime numbers.
6. If true, print (i, i + 2) as twin primes.
7. End
C Program:
(599, 601),(617, 619),(641, 643),(659, 661),(809, 811),(821, 823),(827, 829),(857, 859),(881, 883)
12.Problem Statement: Write a C program to convert octal number to decimal number using non
recursion
Algorithm:
1. Convert Octal Number to Decimal Number
2. Algorithm:
3. Start
4. Input the octal number n.
5. Initialize decimal = 0 and base = 1.
6. While n > 0:
7. Get the last digit n % 10 and add it to decimal as decimal += (n % 10) * base.
8. Divide n by 10 and multiply base by 8.
9. Output the decimal number.
10. End
C Program:
13.Problem Statement: Write a C program to print n Fibonacci terms using non recursion.
Algorithm:
1. Print First n Fibonacci Terms
2. Algorithm:
3. Start
4. Input the number n.
5. Initialize a = 0 and b = 1.
6. Print the first two terms (a and b).
7. For i = 3 to n:
8. Calculate next = a + b.
9. Print next.
10. Update a = b and b = next.
11. End
C Program:
14.Problem Statement: Write a C program to find sum of all even or odd numbers in given range
using non recursion.
Algorithm:
1. Start
2. Input the range start and end.
3. Input the choice (1 for even, 2 for odd).
4. Initialize sum = 0.
5. For i = start to end:
6. If the choice is 1 (even), check if i % 2 == 0 and add it to sum.
7. If the choice is 2 (odd), check if i % 2 != 0 and add it to sum.
8. Output the sum.
9. End
C Program:
Output
15.Problem Statement: Write a C program to print all strong numbers between 1 and n using non
recursion
Algorithm:
Programming for Problem Solving Lab 24071A7256
Page no.234
DEPARTMENT OF CSE-(CYS,DS) AND AI&DS
BRANCH: ARTIFICIAL INTELLEGENCE AND DATA SCIENCE
Week no. :9
Date:2.12.2024
1. Start
2. Input the value n.
3. For each number i from 1 to n:
4. Find the number of digits d in i.
5. Calculate the sum of each digit raised to the power d and compare it with i.
6. If they are equal, print i as an Armstrong number.
7. End
C Program: