You are on page 1of 10

CSE Assignment-2

Here I have written the Code, attached Link and also Screenshot.
1.Factorial :
#include <stdio.h>

int main()
{
int N,K,S;
scanf("%d",&N);
S=1;
for(K=1;K<=N;K=K+1)
{
S=S*K;
}
printf("Result: %d",S);
}

Link : https://onlinegdb.com/rJrpwYrWO

Screenshot is given below,


2.Take 2 numbers as input and find GCD and LCM
Solution:
#include<stdio.h>
int main()
{
int L, S, i, GCD, LCM;

printf("Enter two numbers: ");


scanf("%d %d", &L, &S);

for(i=1; i<=L && i<=S; i++)


{
if(L%i==0 && S%i==0)
GCD=i;
}

LCM=(L*S)/GCD;

printf("GCD = %d\n",GCD);
printf("LCM = %d\n",LCM);

return 0;
}
LINK : https://onlinegdb.com/BJlpuFH-d
Screenshot is given below,
3. find Reverse form of any integer number and sum of digit :
#include <stdio.h>
int main()
{
int n, rev=0 ,remainder,sum=0;
printf("Enter an integer: ");
scanf("%d", &n);
while (n != 0)
{
remainder = n % 10;
rev = rev * 10 + remainder;
n = n/10;
sum=sum+remainder;
}
printf("Reversed number = %d\n", rev );
printf("Sum of digit = %d", sum);
return 0;
}

LINK : https://onlinegdb.com/B1eL7dr8bO
Screenshot is given below,
4.Find Prime Number between two range :
#include <stdio.h>
int main()
{
// Declare the variables
int a, b, i, j, count;

printf("Enter interval: ");


scanf("%d %d",&a,&b);

// Traverse each number in the interval


// with the help of for loop
for (i = a; i <= b; i++)
{
// Skip 0 and 1

if (i == 1 || i == 0)
continue;

// count variable to tell


// if i is prime or not
count = 1;
for (j = 2; j <= i / 2; ++j)
{
if (i % j == 0) {
count = 0;
break;
}
}

// count = 1 means i is prime


// and count = 0 means i is not prime
if (count == 1)
printf("%d ", i);
}

return 0;
}
5.Fibonacci series upto Nth number :
#include<stdio.h>
int main()
{
int sum = 0,a=0,b=1, n;
printf("Enter the nth value: ");
scanf("%d", &n);
printf("Fibonacci series: ");
while(sum <= n)
{
printf("%d ", sum);
a = b; // swap elements
b = sum;
sum = a + b; // next term is the sum of the last two terms
}
return 0;
}
LINK: https://onlinegdb.com/BkDFnFSbO
Screenshot is given below,
THE END

You might also like