0% found this document useful (0 votes)
7 views44 pages

Essential Algorithms in C Programming

The document contains a series of programming problems and their solutions in C, covering various algorithms including factorial calculation, GCD, digit reversal, prime checking, and more. Each problem is presented with input and output examples, demonstrating the functionality of the provided code. The problems range from basic arithmetic and number theory to more complex concepts like polynomial evaluation and operator precedence.

Uploaded by

suiujjh
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)
7 views44 pages

Essential Algorithms in C Programming

The document contains a series of programming problems and their solutions in C, covering various algorithms including factorial calculation, GCD, digit reversal, prime checking, and more. Each problem is presented with input and output examples, demonstrating the functionality of the provided code. The problems range from basic arithmetic and number theory to more complex concepts like polynomial evaluation and operator precedence.

Uploaded by

suiujjh
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

##PROBLEM 1: Write an algorithm and draw a flowchart to find the factorial of a number.

--------------------------------------------------------------------------------------------------------------------------------------
#include <stdio.h>
long long fact(int n){
long long f=1;
for(int i=2;i<=n;i++)
f*=i;
return f;
}
int main(){
int n;
scanf("%d",&n);
printf("%lld\n", fact(n));
return 0;
}
-----------------------------------------------------------------------------------------------------------------------------------

INPUT: 5
OUTPUT: 120

--------------------------------------------------------------------------------------------------------------------------------------

##PROBLEM 2: Write an algorithm to compute the greatest common divisor (Euclid’s

method)
--------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
int gcd(int a,int b){
if(b==0) return a<0?-a:a;
return gcd(b,a%b);
}
int main(){
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n", gcd(a,b));
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 48 18
OUTPUT: 6

--------------------------------------------------------------------------------------------------------------------------------------
## PROBLEM 3: Write an algorithm and flowchart to reverse the digits of a given integer
--------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
int main(){
int n;
scanf("%d",&n);
int x=n, r=0;
while(x){
r=r*10 + x%10;
x/=10;
}
printf("%d\n", r);
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 12345
OUTPUT: 54321

--------------------------------------------------------------------------------------------------------------------------------------

##PROBLEM 4: Develop an algorithm to find whether a number is prime.

--------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
#include <math.h>
int main(){
int n;
scanf("%d",&n);
if(n<2){
printf("Not prime\n");
return 0;
}
for(int i=2;i*i<=n;i++)
if(n%i==0){
printf("Not prime\n");
return 0;
}
printf("Prime\n");
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 17
OUTPUT: Prime

--------------------------------------------------------------------------------------------------------------------------------------

## PROBLEM 5: Sum of First N Natural Numbers


-------------------------------------------------------------------------------

#include <stdio.h>
int main(){
long long n;
scanf("%lld",&n);
printf("%lld\n", n*(n+1)/2);
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 10
OUTPUT: 55

--------------------------------------------------------------------------------------------------------------------------------------

## PROBLEM 6: Algorithm to convert a decimal integer into binary.


-------------------------------------------------------------------------------

#include <stdio.h>
int main(){
unsigned n;
scanf("%u",&n);
if(n==0){
printf("0\n");
return 0;
}
unsigned b[32];
int i=0;
while(n){
b[i++]=n%2;
n/=2;
}
for(int j=i-1;j>=0;j--)
printf("%u", b[j]);
printf("\n");
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 15
OUTPUT: 1111

----------------------------------------------------------------------------------------------------------------------------------------

## PROBLEM 7: Flowchart to solve quadratic equations for real roots.


-------------------------------------------------------------------------------

#include <stdio.h>
#include <math.h>
int main(){
double a,b,c;
scanf("%lf%lf%lf",&a,&b,&c);
double D=b*b-4*a*c;
if(D<0){
printf("Complex roots\n");
} else {
double r1=(-b+sqrt(D))/(2*a);
double r2=(-b-sqrt(D))/(2*a);
printf("%.6f %.6f\n", r1, r2);
}
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 1 -5 6
OUTPUT: 3.000000 2.000000

--------------------------------------------------------------------------------------------------------------------------------------

## PROBLEM 8: Write an algorithm to find the largest of three numbers.


--------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
int main(){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
int m=a;
if(b>m) m=b;
if(c>m) m=c;
printf("%d\n",m);
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 10 25 15
OUTPUT: 25

-----------------------------------------------------------------------------------------------------------------------------------------

## PROBLEM 9: Algorithm to check whether a number is an Armstrong number


--------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
int main(){
int n;
scanf("%d",&n);
int a=n/100;
int b=(n/10)%10;
int c=n%10;
if(a*a*a+b*b*b+c*c*c==n)
printf("Armstrong\n");
else
printf("Not Armstrong\n");
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 153
OUTPUT: Armstrong

--------------------------------------------------------------------------------------------------------------------------------------

## PROBLEM 10: Flowchart to compute the sum of digits of a number.


--------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
int main(){
int n;
scanf("%d",&n);
if(n<0) n=-n;
int s=0;
while(n){
s+=n%10;
n/=10;
}
printf("%d\n",s);
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 12345
OUTPUT: 15

--------------------------------------------------------------------------------------------------------------------------------------

## PROBLEM 11: Algorithm to generate Fibonacci sequence up to n terms.


--------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
int main(){
int n;
scanf("%d",&n);
long long a=0,b=1;
for(int i=0;i<n;i++){
printf("%lld ", a);
long long c=a+b;
a=b;
b=c;
}
printf("\n");
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 10
OUTPUT: 0 1 1 2 3 5 8 13 21 34

--------------------------------------------------------------------------------------------------------------------------------------
## PROBLEM 12: Write an algorithm to check if a number is palindrom
--------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
int main(){
int n;
scanf("%d",&n);
int x=n, r=0;
while(x){
r=r*10 + x%10;
x/=10;
}
printf(r==n?"Yes\n":"No\n");
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 121
OUTPUT: Yes

--------------------------------------------------------------------------------------------------------------------------------------

## PROBLEM 13: Algorithm to test if a number is perfect (sum of divisors equals number).
--------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
int main(){
int n;
scanf("%d",&n);
int s=1;
for(int i=2;i*i<=n;i++){
if(n%i==0){
s+=i;
if(i!=n/i) s+=n/i;
}
}
if(n==1) s=0;
if(s==n)
printf("Perfect\n");
else
printf("Not Perfect\n");
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 28
OUTPUT: Perfect

--------------------------------------------------------------------------------------------------------------------------------------
## PROBLEM 14: Write an algorithm to compute the product of first n odd numbers.
-------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
int main(){
int n;
scanf("%d",&n);
long long p=1;
for(int i=1;i<=n;i++)
p*=(2*i-1);
printf("%lld\n",p);
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 5
OUTPUT: 945

--------------------------------------------------------------------------------------------------------------------------------------

PROBLEM 15: Flowchart to check if a year is a leap year.


--------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
int main(){
int y;
scanf("%d",&y);
int leap=(y%400==0)||((y%4==0)&&(y%100!=0));
printf(leap?"Leap\n":"Not Leap\n");
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 2024
OUTPUT: Leap

--------------------------------------------------------------------------------------------------------------------------------------

## PROBLEM 16: Write a program to demonstrate the use of different data types in C.
--------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
int main(){
int i=10;
float f=3.14;
double d=2.718281828;
char c='A';
printf("int=%d float=%f double=%lf char=%c\n", i,f,d,c);
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------
OUTPUT: int=10 float=3.140000 double=2.718282 char=A

--------------------------------------------------------------------------------------------------------------------------------------

## PROBLEM 17: Program to compute the value of the expression (a+b)^2.


--------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
int main(){
long long a,b;
scanf("%lld%lld",&a,&b);
long long res=a*a+b*b+2*a*b;
printf("%lld\n", res);
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 3 4
OUTPUT: 49

--------------------------------------------------------------------------------------------------------------------------------------

## PROBLEM 18: Write a program to show the difference between integer and floating

division.
--------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
int main(){
int a,b;
scanf("%d%d",&a,&b);
printf("int div=%d\n", a/b);
printf("float div=%f\n", (double)a/b);
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 7 2
OUTPUT:
int div=3
float div=3.500000
--------------------------------------------------------------------------------------------------------------------------------------

## PROBLEM 19: Write a program to demonstrate the difference between pre-increment and

post-increment operators.
--------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
int main(){
int x=5;
printf("x=%d x++=%d ++x=%d\n", x, x++, ++x);
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

OUTPUT: x=7 x++=6 ++x=7

----------------------------------------------------------------------------------------------------------------------------------

## PROBLEM 20: Program to evaluate quadratic formula for given a,b,c.


--------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
#include <math.h>
int main(){
double a,b,c;
scanf("%lf%lf%lf",&a,&b,&c);
double D=b*b-4*a*c;
if(D<0){
printf("Complex roots\n");
} else {
double r1=(-b+sqrt(D))/(2*a);
double r2=(-b-sqrt(D))/(2*a);
printf("%.6f %.6f\n", r1, r2);
}
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 1 -5 6
OUTPUT: 3.000000 2.000000

--------------------------------------------------------------------------------------------------------------------------------------

## PROBLEM 21: Write a program to compute the sum of series 1 + 1/2 + ... + 1/n.
--------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
int main(){
int n;
scanf("%d",&n);
double s=0;
for(int i=1;i<=n;i++)
s+=1.0/i;
printf("%.6f\n", s);
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 5
OUTPUT: 2.283333

--------------------------------------------------------------------------------------------------------------------------------------

## PROBLEM 22:
Write a program to convert Celsius to Fahrenheit and vice versa.
--------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
int main(){
double c;
scanf("%lf",&c);
double f=(c*9.0)/5.0+32;
printf("%.2f\n", f);
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 0
OUTPUT: 32.00
--------------------------------------------------------------------------------------------------------------------------------------

## PROBLEM 23: Program to compute compound interest.


--------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
#include <math.h>
int main(){
double P,r;
int n;
scanf("%lf%lf%d",&P,&r,&n);
double A=P*pow(1+r/100.0,n);
printf("%.2f\n", A);
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 1000 5 2
OUTPUT: 1102.50

--------------------------------------------------------------------------------------------------------------------------------------

## PROBLEM 24: Write a program to evaluate a polynomial ax^3+bx^2+cx+d.


--------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
int main(){
double a,b,c,d,x;
scanf("%lf%lf%lf%lf%lf",&a,&b,&c,&d,&x);
double y=a*x*x*x+b*x*x+c*x+d;
printf("%.6f\n", y);
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 1 2 3 4 2
OUTPUT: 26.000000

--------------------------------------------------------------------------------------------------------------------------------------

## PROBLEM 25: Program to demonstrate operator precedence with the expression a+b*c/d.
--------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
int main(){
double a,b,c,d;
scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
double res=a+b*c/d;
printf("%.6f\n", res);
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 2 3 4 2
OUTPUT: 8.000000

--------------------------------------------------------------------------------------------------------------------------------------

## PROBLEM 26: Write a program to compute nCr.


--------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
long long nCr(int n,int r){
if(r<0||r>n) return 0;
if(r>n-r) r=n-r;
long long num=1,den=1;
for(int i=1;i<=r;i++){
num*=(n-r+i);
den*=i;
}
return num/den;
}
int main(){
int n,r;
scanf("%d%d",&n,&r);
printf("%lld\n", nCr(n,r));
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 5 2
OUTPUT: 10

--------------------------------------------------------------------------------------------------------------------------------------

## PROBLEM 27: Write a program to compute nPr Permutation


--------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
long long nPr(int n,int r){
if(r<0||r>n) return 0;
long long p=1;
for(int i=0;i<r;i++)
p*=(n-i);
return p;
}
int main(){
int n,r;
scanf("%d%d",&n,&r);
printf("%lld\n", nPr(n,r));
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 5 2
OUTPUT: 20

--------------------------------------------------------------------------------------------------------------------------------------

## PROBLEM 28: Write a program to verify DeMorgan’s law for given boolean inputs.
--------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
int main(){
int p,q;
scanf("%d%d",&p,&q);
int lhs=!(p&&q);
int rhs=(!p)||(!q);
printf(lhs==rhs?"True\n":"False\n");
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 1 0
OUTPUT: True

--------------------------------------------------------------------------------------------------------------------------------------

## PROBLEM 29: Write a program to compute the harmonic mean of two integers.
--------------------------------------------------------------------------------------------------------------------------------------
#include <stdio.h>
int main(){
double a,b;
scanf("%lf%lf",&a,&b);
double hm=2.0/(1.0/a+1.0/b);
printf("%.6f\n", hm);
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 4 6
OUTPUT: 4.800000

--------------------------------------------------------------------------------------------------------------------------------------

## PROBLEM 30: Write a program to check overflow in integer multiplication.


--------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
#include <limits.h>
int main(){
long long a,b;
scanf("%lld%lld",&a,&b);
long long prod=a*b;
if(a!=0&&prod/a!=b)
printf("Overflow\n");
else
printf("Product=%lld\n", prod);
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 10 20
OUTPUT: Product=200

--------------------------------------------------------------------------------------------------------------------------------------

## PROBLEM 31: Program to check whether a number is positive, negative, or zero.


--------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
int main(){
int n;
scanf("%d",&n);
if(n>0)
printf("Positive\n");
else if(n<0)
printf("Negative\n");
else
printf("Zero\n");
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: -5
OUTPUT: Negative

--------------------------------------------------------------------------------------------------------------------------------------

## PROBLEM 32: Program to generate multiplication table of a given integer.


--------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
int main(){
int n;
scanf("%d",&n);
for(int i=1;i<=10;i++)
printf("%d x %d = %d\n", n,i,n*i);
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 5
OUTPUT:
5x1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

--------------------------------------------------------------------------------------------------------------------------------------

## PROBLEM 33: Write a program to print first 20 triangular numbers.


--------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
int main(){
for(int n=1;n<=20;n++)
printf("%d ", n*(n+1)/2);
printf("\n");
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

OUTPUT: 1 3 6 10 15 21 28 36 45 55 66 78 91 105 120 136 153 171 190 210


--------------------------------------------------------------------------------------------------------------------------------------

## PROBLEM 34: Program to calculate factorial using while loop.


--------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
int main(){
int n;
scanf("%d",&n);
long long f=1;
int i=2;
while(i<=n)
f*=i++;
printf("%lld\n", f);
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 6
OUTPUT: 720

--------------------------------------------------------------------------------------------------------------------------------------

## PROBLEM 35: Program to print Pascal’s triangle up to n rows.


--------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
long long comb(int n,int r){
if(r>n-r) r=n-r;
long long v=1;
for(int i=1;i<=r;i++)
v=v*(n-r+i)/i;
return v;
}
int main(){
int n;
scanf("%d",&n);
for(int i=0;i<n;i++){
for(int j=0;j<=i;j++)
printf("%lld ", comb(i,j));
printf("\n");
}
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 5
OUTPUT:
1
11
121
1331
14641

--------------------------------------------------------------------------------------------------------------------------------------
## PROBLEM 36: Program to approximate sin(x) using its series expansion up to n terms.
-------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
#include <math.h>
int main(){
double x;
int n;
scanf("%lf%d",&x,&n);
double s=0;
for(int k=0;k<n;k++){
double tn=pow(x, 2*k+1);
double fact=1;
for(int i=1;i<=2*k+1;i++)
fact*=i;
s+=(k%2?-1:1)*tn/fact;
}
printf("%.6f\n", s);
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 0.5 5
OUTPUT: 0.479427

--------------------------------------------------------------------------------------------------------------------------------------

PROBLEM 37 : Program to solve the linear equation ax+b=0.


--------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
int main(){
double a,b;
scanf("%lf%lf",&a,&b);
if(a==0){
if(b==0)
printf("Infinite solutions\n");
else
printf("No solution\n");
} else
printf("%.6f\n", -b/a);
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 2 -8
OUTPUT: 4.000000

--------------------------------------------------------------------------------------------------------------------------------------
## PROBLEM 38: Program to generate all Pythagorean triplets where c < 50
--------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
int main(){
for(int a=1;a<50;a++)
for(int b=a;b<50;b++){
int c2=a*a+b*b;
for(int c=b;c<50;c++)
if(c*c==c2)
printf("(%d,%d,%d)\n", a,b,c);
}
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

OUTPUT: (3,4,5) (5,12,13) (6,8,10) (7,24,25) ... (and more)


--------------------------------------------------------------------------------------------------------------------------------------

## PROBLEM 39: Program to test Goldbach’s conjecture for even integers less than 100.
--------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
int isprime(int n){
if(n<2) return 0;
for(int i=2;i*i<=n;i++)
if(n%i==0) return 0;
return 1;
}
int main(){
int m;
scanf("%d",&m);
if(m%2||m>=100){
printf("Invalid\n");
return 0;
}
for(int p=2;p<=m;p++)
if(isprime(p)&&isprime(m-p)){
printf("%d = %d + %d\n", m, p, m-p);
break;
}
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 20
OUTPUT: 20 = 3 + 17

--------------------------------------------------------------------------------------------------------------------------------------

## PROBLEM 40: Program to compute the sum of prime numbers less than or equal to n.
--------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
int isprime(int n){
if(n<2) return 0;
for(int i=2;i*i<=n;i++)
if(n%i==0) return 0;
return 1;
}
int main(){
int n;
scanf("%d",&n);
long long s=0;
for(int i=2;i<=n;i++)
if(isprime(i)) s+=i;
printf("%lld\n", s);
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 20
OUTPUT: 77

--------------------------------------------------------------------------------------------------------------------------------------

## PROBLEM 41: Program to check Collatz sequence for a given number.


--------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
int main(){
long long n;
scanf("%lld",&n);
while(n!=1){
printf("%lld ", n);
if(n%2==0)
n/=2;
else
n=3*n+1;
}
printf("1\n");
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 10
OUTPUT: 10 5 16 8 4 2 1

--------------------------------------------------------------------------------------------------------------------------------------

## PROBLEM 42: Write a program using switch to print day of week given number (1–7).
--------------------------------------------------------------------------------------------------------------------------------------
#include <stdio.h>
int main(){
int d;
scanf("%d",&d);
switch(d){
case 1: puts("Sunday"); break;
case 2: puts("Monday"); break;
case 3: puts("Tuesday"); break;
case 4: puts("Wednesday"); break;
case 5: puts("Thursday"); break;
case 6: puts("Friday"); break;
case 7: puts("Saturday"); break;
default: puts("Invalid");
}
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 3
OUTPUT: Tuesday

--------------------------------------------------------------------------------------------------------------------------------------

## PROBLEM 43:

Program to compute gcd of two numbers using repeated subtraction (while

loop).
--------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
int main(){
int a,b;
scanf("%d%d",&a,&b);
if(a<0) a=-a;
if(b<0) b=-b;
while(a&&b){
if(a>b)
a-=b;
else
b-=a;
}
printf("%d\n", a?a:b);
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 48 18
OUTPUT: 12

--------------------------------------------------------------------------------------------------------------------------------------
## PROBLEM 44: Program that uses break to stop reading input when the sum exceeds 100.
--------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
int main(){
int x;
int s=0;
while(scanf("%d",&x)==1){
s+=x;
if(s>100) break;
}
printf("Sum=%d\n", s);
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 30 40 50
OUTPUT: Sum=120

--------------------------------------------------------------------------------------------------------------------------------------

## PROBLEM 45: Program that uses continue to print only odd numbers between 1 and 50.
--------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
int main(){
for(int i=1;i<=50;i++){
if(i%2==0) continue;
printf("%d ", i);
}
printf("\n");
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

OUTPUT: 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49

--------------------------------------------------------------------------------------------------------------------------------------

## PROBLEM 46: Program to read n integers and find the maximum.


--------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
int main(){
int n;
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
int m=a[0];
for(int i=1;i<n;i++)
if(a[i]>m) m=a[i];
printf("%d\n", m);
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 5
10 20 30 15 25
OUTPUT: 30

--------------------------------------------------------------------------------------------------------------------------------------

## PROBLEM 47: Program to find the minimum of n integers.


--------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
int main(){
int n;
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
int m=a[0];
for(int i=1;i<n;i++)
if(a[i]<m) m=a[i];
printf("%d\n", m);
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 5
10 20 5 15 25
OUTPUT: 5

---

## PROBLEM 48: Program to compute the mean of n integers.


--------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
int main(){
int n;
scanf("%d",&n);
double s=0;
for(int i=0;i<n;i++){
double x;
scanf("%lf",&x);
s+=x;
}
printf("%.6f\n", s/n);
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 5
10 20 30 40 50
OUTPUT: 30.000000
--------------------------------------------------------------------------------------------------------------------------------------

## PROBLEM 49: program to compute the variance of n integers


---------------------------------------------------------------------------------------------------------------

#include <stdio.h>
int main(){
int n;
scanf("%d",&n);
double a[n], s=0, sq=0;
for(int i=0;i<n;i++){
scanf("%lf",&a[i]);
s+=a[i];
sq+=a[i]*a[i];
}
double mu=s/n;
printf("%.6f\n", sq/n-mu*mu);
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 5
10 20 30 40 50
OUTPUT: 200.000000

--------------------------------------------------------------------------------------------------------------------------------------

## PROBLEM 50: Program to reverse an array.


--------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
int main(){
int n;
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
for(int i=0;i<n/2;i++){
int t=a[i];
a[i]=a[n-1-i];
a[n-1-i]=t;
}
for(int i=0;i<n;i++)
printf("%d ", a[i]);
printf("\n");
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 5
12345
OUTPUT: 5 4 3 2 1

--------------------------------------------------------------------------------------------------------------------------------------

## PROBLEM 51: Program to count the number of even and odd numbers in an array.
--------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
int main(){
int n;
scanf("%d",&n);
int a[n], even=0, odd=0;
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
if(a[i]%2==0) even++;
else odd++;
}
printf("Even=%d Odd=%d\n", even, odd);
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 7
1234567
OUTPUT: Even=3 Odd=4

--------------------------------------------------------------------------------------------------------------------------------------

## PROBLEM 52: Program to check whether the array is a palindrome.


--------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
int main(){
int n;
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
int ok=1;
for(int i=0;i<n/2;i++)
if(a[i]!=a[n-1-i]) ok=0;
printf(ok?"Palindrome\n":"Not Palindrome\n");
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 5
12321
OUTPUT: Palindrome

--------------------------------------------------------------------------------------------------------------------------------------

## PROBLEM 53: Program to perform linear search.


--------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
int main(){
int n,key;
scanf("%d%d",&n,&key);
int a[n];
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
int pos=-1;
for(int i=0;i<n;i++)
if(a[i]==key){
pos=i;
break;
}
printf("%d\n", pos);
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 5 25
10 20 25 30 35
OUTPUT: 2
--------------------------------------------------------------------------------------------------------------------------------------

## PROBLEM 54:Program to perform binary search.


--------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
int main(){
int n,key;
scanf("%d%d",&n,&key);
int a[n];
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
int l=0,r=n-1,pos=-1;
while(l<=r){
int m=(l+r)/2;
if(a[m]==key){
pos=m;
break;
} else if(a[m]<key)
l=m+1;
else
r=m-1;
}
printf("%d\n", pos);
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------

INPUT: 5 25
10 20 25 30 35
OUTPUT: 2

## PROBLEM 55: Program to sort an array using bubble sort.


--------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
int main(){
int n;
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
for(int i=0;i<n-1;i++)
for(int j=0;j<n-1-i;j++)
if(a[j]>a[j+1]){
int t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
for(int i=0;i<n;i++)
printf("%d ", a[i]);
printf("\n");
return 0;
}
-------------------------------------------------------------------------------

INPUT: 5
30 10 50 20 40
OUTPUT: 10 20 30 40 50

------------------------------------------------------------------------------------------------------------------------------------

## PROBLEM 56: Program to compute frequency of each element in array


-------------------------------------------------------------------------------

#include <stdio.h>
int main(){
int n;
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
int used[n];
for(int i=0;i<n;i++) used[i]=0;
for(int i=0;i<n;i++){
if(used[i]) continue;
int cnt=1;
for(int j=i+1;j<n;j++)
if(a[j]==a[i]){
cnt++;
used[j]=1;
}
printf("%d occurs %d times\n", a[i], cnt);
}
return 0;
}
-------------------------------------------------------------------------------

INPUT: 7
1223334
OUTPUT:
1 occurs 1 times
2 occurs 2 times
3 occurs 3 times
4 occurs 1 times

-------------------------------------------------------------------------------

## PROBLEM 57: Program to compute the dot product of two arrays


-------------------------------------------------------------------------------

#include <stdio.h>
int main(){
int n;
scanf("%d",&n);
double a[n], b[n];
for(int i=0;i<n;i++)
scanf("%lf",&a[i]);
for(int i=0;i<n;i++)
scanf("%lf",&b[i]);
double s=0;
for(int i=0;i<n;i++)
s+=a[i]*b[i];
printf("%.6f\n", s);
return 0;
}
-------------------------------------------------------------------------------

INPUT: 3
123
456
OUTPUT: 32.000000

-------------------------------------------------------------------------------

## PROBLEM 58: Program to compute the mode of a given array.


-------------------------------------------------------------------------------

#include <stdio.h>
int main(){
int n;
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
int mode=a[0], maxcount=0;
for(int i=0;i<n;i++){
int count=0;
for(int j=0;j<n;j++)
if(a[j]==a[i]) count++;
if(count>maxcount){
maxcount=count;
mode=a[i];
}
}
printf("%d\n", mode);
return 0;
}
-------------------------------------------------------------------------------

INPUT: 7
1223334
OUTPUT: 3

-------------------------------------------------------------------------------

## PROBLEM 59: Program to compute the median of a given array.


-------------------------------------------------------------------------------

#include <stdio.h>
void sort(int *a,int n){
for(int i=0;i<n-1;i++)
for(int j=0;j<n-1-i;j++)
if(a[j]>a[j+1]){
int t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
int main(){
int n;
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,n);
if(n%2)
printf("%.2f\n", (double)a[n/2]);
else
printf("%.2f\n", (a[n/2-1]+a[n/2])/2.0);
return 0;
}
-------------------------------------------------------------------------------

INPUT: 5
30 10 50 20 40
OUTPUT: 30.00

-------------------------------------------------------------------------------

## PROBLEM 60: Program to merge two sorted arrays into one sorted array.
-------------------------------------------------------------------------------

#include <stdio.h>
int main(){
int n,m;
scanf("%d%d",&n,&m);
int a[n],b[m];
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
for(int j=0;j<m;j++)
scanf("%d",&b[j]);
int c[n+m],i=0,j=0,k=0;
while(i<n&&j<m)
c[k++]=(a[i]<b[j])?a[i++]:b[j++];
while(i<n) c[k++]=a[i++];
while(j<m) c[k++]=b[j++];
for(i=0;i<k;i++)
printf("%d ", c[i]);
printf("\n");
return 0;
}
-------------------------------------------------------------------------------

INPUT: 3 3
10 20 30
5 15 25
OUTPUT: 5 10 15 20 25 30
-------------------------------------------------------------------------------

## PROBLEM 61: Program to add two matrices.


-------------------------------------------------------------------------------
#include <stdio.h>
int main(){
int r,c;
scanf("%d%d",&r,&c);
double A[r][c],B[r][c];
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
scanf("%lf",&A[i][j]);
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
scanf("%lf",&B[i][j]);
for(int i=0;i<r;i++){
for(int j=0;j<c;j++)
printf("%.2f ", A[i][j]+B[i][j]);
printf("\n");
}
return 0;
}
-------------------------------------------------------------------------------

INPUT: 2 2
12
34
56
78
OUTPUT:
6.00 8.00
10.00 12.00

-------------------------------------------------------------------------------

## PROBLEM 62: Program to subtract two matrices.


-------------------------------------------------------------------------------

#include <stdio.h>
int main(){
int r,c;
scanf("%d%d",&r,&c);
double A[r][c],B[r][c];
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
scanf("%lf",&A[i][j]);
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
scanf("%lf",&B[i][j]);
for(int i=0;i<r;i++){
for(int j=0;j<c;j++)
printf("%.2f ", A[i][j]-B[i][j]);
printf("\n");
}
return 0;
}
-------------------------------------------------------------------------------

INPUT: 2 2
56
78
12
34
OUTPUT:
4.00 4.00
4.00 4.00

-------------------------------------------------------------------------------

## PROBLEM 63: Program to multiply two matrices.


-------------------------------------------------------------------------------

#include <stdio.h>
int main(){
int r,k,c;
scanf("%d%d%d",&r,&k,&c);
double A[r][k],B[k][c],C[r][c];
for(int i=0;i<r;i++)
for(int j=0;j<k;j++)
scanf("%lf",&A[i][j]);
for(int i=0;i<k;i++)
for(int j=0;j<c;j++)
scanf("%lf",&B[i][j]);
for(int i=0;i<r;i++)
for(int j=0;j<c;j++){
C[i][j]=0;
for(int t=0;t<k;t++)
C[i][j]+=A[i][t]*B[t][j];
}
for(int i=0;i<r;i++){
for(int j=0;j<c;j++)
printf("%.2f ", C[i][j]);
printf("\n");
}
return 0;
}
-------------------------------------------------------------------------------

INPUT: 2 2 2
12
34
56
78
OUTPUT:
19.00 22.00
43.00 50.00

-------------------------------------------------------------------------------
## PROBLEM 64: Program to compute transpose of a matrix.
-------------------------------------------------------------------------------

#include <stdio.h>
int main(){
int r,c;
scanf("%d%d",&r,&c);
double A[r][c];
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
scanf("%lf",&A[i][j]);
for(int j=0;j<c;j++){
for(int i=0;i<r;i++)
printf("%.2f ", A[i][j]);
printf("\n");
}
return 0;
}
-------------------------------------------------------------------------------

INPUT: 2 3
123
456
OUTPUT:
1.00 4.00
2.00 5.00
3.00 6.00
-------------------------------------------------------------------------------

## PROBLEM 65: Program to check if a matrix is symmetric.


-------------------------------------------------------------------------------

#include <stdio.h>
int main(){
int n;
scanf("%d",&n);
double A[n][n];
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
scanf("%lf",&A[i][j]);
int ok=1;
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
if(A[i][j]!=A[j][i]) ok=0;
printf(ok?"Symmetric\n":"Not Symmetric\n");
return 0;
}
-------------------------------------------------------------------------------

INPUT: 3
123
245
356
OUTPUT: Symmetric
-------------------------------------------------------------------------------

## PROBLEM 66:Program to check if a matrix is Skew-Symmetric Matrix.


-------------------------------------------------------------------------------

#include <stdio.h>
#include <math.h>
int main(){
int n;
scanf("%d",&n);
double A[n][n];
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
scanf("%lf",&A[i][j]);
int ok=1;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(fabs(A[i][j]+A[j][i])>1e-9) ok=0;
printf(ok?"Skew-symmetric\n":"Not Skew\n");
return 0;
}
-------------------------------------------------------------------------------

INPUT: 3
0 2 -3
-2 0 1
3 -1 0
OUTPUT: Skew-symmetric
-------------------------------------------------------------------------------

## PROBLEM 67: Program to compute determinant of a 2×2 matrix.


-------------------------------------------------------------------------------

#include <stdio.h>
int main(){
double a,b,c,d;
scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
printf("%.2f\n", a*d-b*c);
return 0;
}
-------------------------------------------------------------------------------

INPUT: 1 2 3 4
OUTPUT: -2.00
-------------------------------------------------------------------------------
## PROBLEM 68:Program to compute determinant of a 3×3 matrix.
-------------------------------------------------------------------------------

#include <stdio.h>
int main(){
double a[3][3];
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
scanf("%lf",&a[i][j]);
double det=a[0][0]*(a[1][1]*a[2][2]-a[1][2]*a[2][1])-
a[0][1]*(a[1][0]*a[2][2]-a[1][2]*a[2][0])+
a[0][2]*(a[1][0]*a[2][1]-a[1][1]*a[2][0]);
printf("%.2f\n", det);
return 0;
}
-------------------------------------------------------------------------------

INPUT: 1 2 3 0 1 4 5 1 0
OUTPUT: 18.00

-------------------------------------------------------------------------------

## PROBLEM 69: Program to compute trace of a matrix.


-------------------------------------------------------------------------------

#include <stdio.h>
int main(){
int n;
scanf("%d",&n);
double x,s=0;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++){
scanf("%lf",&x);
if(i==j) s+=x;
}
printf("%.2f\n", s);
return 0;
}
-------------------------------------------------------------------------------

INPUT: 3
123456789
OUTPUT: 15.00
-------------------------------------------------------------------------------

## PROBLEM 70: Program to check whether a matrix is uper triangular


-------------------------------------------------------------------------------

#include <stdio.h>
#include <math.h>
int main(){
int n;
scanf("%d",&n);
double A[n][n];
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
scanf("%lf",&A[i][j]);
int ok=1;
for(int i=1;i<n;i++)
for(int j=0;j<i;j++)
if(fabs(A[i][j])>1e-9) ok=0;
printf(ok?"Upper triangular\n":"Not Upper\n");
return 0;
}
-------------------------------------------------------------------------------

INPUT: 3
123
045
006
OUTPUT: Upper triangular

-------------------------------------------------------------------------------

## PROBLEM 71: Program to check whether a matrix is Lower Triangular Check


-------------------------------------------------------------------------------

#include <stdio.h>
#include <math.h>
int main(){
int n;
scanf("%d",&n);
double A[n][n];
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
scanf("%lf",&A[i][j]);
int ok=1;
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
if(fabs(A[i][j])>1e-9) ok=0;
printf(ok?"Lower triangular\n":"Not Lower\n");
return 0;
}
-------------------------------------------------------------------------------

INPUT: 3
100
230
456
OUTPUT: Lower triangular

-------------------------------------------------------------------------------
## PROBLEM 72: Program to check whether a matrix is Diagonal Matrix Check
-------------------------------------------------------------------------------

#include <stdio.h>
#include <math.h>
int main(){
int n;
scanf("%d",&n);
double A[n][n];
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
scanf("%lf",&A[i][j]);
int ok=1;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(i!=j&&fabs(A[i][j])>1e-9) ok=0;
printf(ok?"Diagonal\n":"Not Diagonal\n");
return 0;
}
-------------------------------------------------------------------------------

INPUT: 3
100
020
003
OUTPUT: Diagonal
-------------------------------------------------------------------------------

## PROBLEM 73: Program to check whether a matrix is Orthogonal Matrix Check


-------------------------------------------------------------------------------

#include <stdio.h>
#include <math.h>
int main(){
int n;
scanf("%d",&n);
double A[n][n];
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
scanf("%lf",&A[i][j]);
int ok=1;
double eps=1e-6;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++){
double s=0;
for(int k=0;k<n;k++)
s+=A[i][k]*A[j][k];
if(i==j){
if(fabs(s-1)>eps) ok=0;
} else {
if(fabs(s)>eps) ok=0;
}
}
printf(ok?"Orthogonal\n":"Not Orthogonal\n");
return 0;
}
-------------------------------------------------------------------------------

INPUT: 2
01
10
OUTPUT: Orthogonal
-------------------------------------------------------------------------------

## PROBLEM 74: Program to compute row sums and column sums.


-------------------------------------------------------------------------------

#include <stdio.h>
int main(){
int r,c;
scanf("%d%d",&r,&c);
double A[r][c];
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
scanf("%lf",&A[i][j]);
for(int i=0;i<r;i++){
double s=0;
for(int j=0;j<c;j++)
s+=A[i][j];
printf("Row %d sum=%.2f\n", i, s);
}
for(int j=0;j<c;j++){
double s=0;
for(int i=0;i<r;i++)
s+=A[i][j];
printf("Col %d sum=%.2f\n", j, s);
}
return 0;
}
-------------------------------------------------------------------------------

INPUT: 2 3
123
456
OUTPUT:
Row 0 sum=6.00
Row 1 sum=15.00
Col 0 sum=5.00
Col 1 sum=7.00
Col 2 sum=9.00
-------------------------------------------------------------------------------
## PROBLEM 75: Program to allocate a matrix dynamically using malloc and free it after use.
-------------------------------------------------------------------------------

#include <stdio.h>
#include <stdlib.h>
int main(){
int r,c;
scanf("%d%d",&r,&c);
double **A=malloc(r*sizeof(double*));
for(int i=0;i<r;i++)
A[i]=malloc(c*sizeof(double));
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
scanf("%lf", &A[i][j]);
for(int i=0;i<r;i++){
for(int j=0;j<c;j++)
printf("%.2f ", A[i][j]);
printf("\n");
free(A[i]);
}
free(A);
return 0;
}
-------------------------------------------------------------------------------

INPUT: 2 3
123
456
OUTPUT:
1.00 2.00 3.00
4.00 5.00 6.00

-------------------------------------------------------------------------------

## PROBLEM 76: Write a function to compute factorial (iterative).


-------------------------------------------------------------------------------

#include <stdio.h>
long long fact(int n){
long long f=1;
for(int i=2;i<=n;i++)
f*=i;
return f;
}
int main(){
int n;
scanf("%d",&n);
printf("%lld\n", fact(n));
return 0;
}
-------------------------------------------------------------------------------
INPUT: 7
OUTPUT: 5040
-------------------------------------------------------------------------------

## PROBLEM 77: Write a recursive function to compute factorial.


-------------------------------------------------------------------------------

#include <stdio.h>
long long rf(int n){
return n<2?1:n*rf(n-1);
}
int main(){
int n;
scanf("%d",&n);
printf("%lld\n", rf(n));
return 0;
}
-------------------------------------------------------------------------------

INPUT: 7
OUTPUT: 5040
-------------------------------------------------------------------------------

## PROBLEM 78: Write a function to compute gcd of two numbers.


-------------------------------------------------------------------------------

#include <stdio.h>
int gcd(int a,int b){
while(b){
int t=a%b;
a=b;
b=t;
}
return a<0?-a:a;
}
int main(){
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n", gcd(a,b));
return 0;
}
-------------------------------------------------------------------------------

INPUT: 60 48
OUTPUT: 12

-------------------------------------------------------------------------------

## PROBLEM 79: Write a function to compute lcm of two numbers using gcd.
-------------------------------------------------------------------------------
#include <stdio.h>
long long gcd(long long a,long long b){
while(b){
long long t=a%b;
a=b;
b=t;
}
return a<0?-a:a;
}
int main(){
long long a,b;
scanf("%lld%lld",&a,&b);
printf("%lld\n", a/gcd(a,b)*b);
return 0;
}
-------------------------------------------------------------------------------

INPUT: 12 18
OUTPUT: 36

-------------------------------------------------------------------------------

## PROBLEM 80: Write a recursive function to compute nth Fibonacci number.


-------------------------------------------------------------------------------

#include <stdio.h>
long long fib(int n){
return n<2?n:fib(n-1)+fib(n-2);
}
int main(){
int n;
scanf("%d",&n);
printf("%lld\n", fib(n));
return 0;
}
-------------------------------------------------------------------------------

INPUT: 10
OUTPUT: 55

-------------------------------------------------------------------------------

## PROBLEM 81: Write a function to compute nPr


-------------------------------------------------------------------------------

#include <stdio.h>
long long nPr(int n,int r){
long long p=1;
for(int i=0;i<r;i++)
p*=(n-i);
return p;
}
int main(){
int n,r;
scanf("%d%d",&n,&r);
printf("%lld\n", nPr(n,r));
return 0;
}
-------------------------------------------------------------------------------

INPUT: 6 3
OUTPUT: 120

-------------------------------------------------------------------------------

## PROBLEM 82: Write a function to compute nCr


-------------------------------------------------------------------------------

#include <stdio.h>
long long nCr(int n,int r){
if(r<0||r>n) return 0;
if(r>n-r) r=n-r;
long long num=1,den=1;
for(int i=1;i<=r;i++){
num*=(n-r+i);
den*=i;
}
return num/den;
}
int main(){
int n,r;
scanf("%d%d",&n,&r);
printf("%lld\n", nCr(n,r));
return 0;
}
-------------------------------------------------------------------------------

INPUT: 6 3
OUTPUT: 20

-------------------------------------------------------------------------------

## PROBLEM 83: Write a function to evaluate a polynomial using Horner’s method.


-------------------------------------------------------------------------------

#include <stdio.h>
double horner(double a[], int deg, double x){
double res=a[deg];
for(int i=deg-1;i>=0;i--)
res=res*x+a[i];
return res;
}
int main(){
int k;
scanf("%d",&k);
double a[k+1];
for(int i=0;i<=k;i++)
scanf("%lf", &a[i]);
double x;
scanf("%lf", &x);
printf("%.6f\n", horner(a,k,x));
return 0;
}
-------------------------------------------------------------------------------

INPUT: 2
1213
OUTPUT: 13.000000

-------------------------------------------------------------------------------

## PROBLEM 84: Write a function to compute power x^n (fast exponentiation).


-------------------------------------------------------------------------------

#include <stdio.h>
double fpow(double x, long long n){
double r=1;
long long e=n;
if(e<0){ x=1/x; e=-e; }
while(e){
if(e&1) r*=x;
x*=x;
e>>=1;
}
return r;
}
int main(){
double x;
long long n;
scanf("%lf%lld",&x,&n);
printf("%.6f\n", fpow(x,n));
return 0;
}
-------------------------------------------------------------------------------

INPUT: 2 10
OUTPUT: 1024.000

-------------------------------------------------------------------------------

## PROBLEM 85: Write a function to test whether a number is prime.


-------------------------------------------------------------------------------
#include <stdio.h>
#include <math.h>
int main(){
int n;
scanf("%d",&n);
if(n<2){
printf("Not prime\n");
return 0;
}
for(int i=2;i*i<=n;i++)
if(n%i==0){
printf("Not prime\n");
return 0;
}
printf("Prime\n");
return 0;
}
-------------------------------------------------------------------------------

INPUT: 29
OUTPUT: Prime

-------------------------------------------------------------------------------

## PROBLEM 86: Write a function to compute arithmetic mean of array elements.


-------------------------------------------------------------------------------

#include <stdio.h>
double mean(double a[], int n){
double s=0;
for(int i=0;i<n;i++)
s+=a[i];
return s/n;
}
int main(){
int n;
scanf("%d",&n);
double a[n];
for(int i=0;i<n;i++)
scanf("%lf", &a[i]);
printf("%.6f\n", mean(a,n));
return 0;
}
-------------------------------------------------------------------------------

INPUT: 5
10 20 30 40 50
OUTPUT: 30.000000
-------------------------------------------------------------------------------

## PROBLEM 87: Write a function to compute geometric mean of array elements.


-------------------------------------------------------------------------------

#include <stdio.h>
#include <math.h>
double gmean(double a[], int n){
double s=0;
for(int i=0;i<n;i++)
s+=log(a[i]);
return exp(s/n);
}
int main(){
int n;
scanf("%d",&n);
double a[n];
for(int i=0;i<n;i++)
scanf("%lf", &a[i]);
printf("%.6f\n", gmean(a,n));
return 0;
}

-------------------------------------------------------------------------------
INPUT: 4
2 4 8 16
OUTPUT: 5.278031
-------------------------------------------------------------------------------

## PROBLEM 88: Write a function to swap two numbers using call by value
-------------------------------------------------------------------------------

#include <stdio.h>
void swap_val(int a,int b){
int t=a;
a=b;
b=t;
}
int main(){
int a,b;
scanf("%d%d",&a,&b);
swap_val(a,b);
printf("%d %d\n", a,b);
return 0;
}
-------------------------------------------------------------------------------

INPUT: 10 20
OUTPUT: 10 20

-------------------------------------------------------------------------------

## PROBLEM 89: Write a function to swap two numbers using call by reference.
-------------------------------------------------------------------------------

#include <stdio.h>
void swap_ref(int *a,int *b){
int t=*a;
*a=*b;
*b=t;
}
int main(){
int a,b;
scanf("%d%d",&a,&b);
swap_ref(&a,&b);
printf("%d %d\n", a,b);
return 0;
}
-------------------------------------------------------------------------------

INPUT: 10 20
OUTPUT: 20 10
-------------------------------------------------------------------------------

## PROBLEM 90: Write a function that takes an array and returns its maximum element.
-------------------------------------------------------------------------------

#include <stdio.h>
int arrmax(int a[], int n){
int m=a[0];
for(int i=1;i<n;i++)
if(a[i]>m) m=a[i];
return m;
}
int main(){
int n;
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
printf("%d\n", arrmax(a,n));
return 0;
}
-------------------------------------------------------------------------------

INPUT: 5
10 25 15 30 20
OUTPUT: 30
-------------------------------------------------------------------------------

You might also like