0% found this document useful (0 votes)
4 views9 pages

Basic Math: 10 Simple Problems

Uploaded by

syndicateid187
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)
4 views9 pages

Basic Math: 10 Simple Problems

Uploaded by

syndicateid187
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

Basic Math - 10 Easy Problems

1. Check whether a number is prime.


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

#include <bits/stdc++.h>
using namespace std;
int main(){
long long n; cin>>n;
if(n<2){ cout<<"NO\n"; return 0; }
for(long long i=2;i*i<=n;i++)
if(n%i==0){ cout<<"NO\n"; return 0; }
cout<<"YES\n";
}

import [Link].*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner([Link]);
long n=[Link]();
if(n<2){ [Link]("NO"); return; }
for(long i=2;i*i<=n;i++)
if(n%i==0){ [Link]("NO"); return; }
[Link]("YES");
}
}

n=int(input())
if n<2:
print("NO")
else:
ok=True
i=2
while i*i<=n:
if n%i==0:
ok=False
break
i+=1
print("YES" if ok else "NO")

2. Find the factorial of a number.


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

#include <bits/stdc++.h>
using namespace std;
int main(){
int n; cin>>n;
long long f=1;
for(int i=1;i<=n;i++) f*=i;
cout<<f<<"\n";
}

import [Link].*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner([Link]);
int n=[Link]();
long f=1;
for(int i=1;i<=n;i++) f*=i;
[Link](f);
}
}

n=int(input())
f=1
for i in range(1,n+1):
f*=i
print(f)

3. Reverse the digits of a number.


#include <stdio.h>
int main(){
long long n; scanf("%lld",&n);
long long sign = n<0?-1:1;
n = (n<0?-n:n);
long long r=0;
while(n){
r=r*10 + n%10;
n/=10;
}
printf("%lld\n", r*sign);
}

#include <bits/stdc++.h>
using namespace std;
int main(){
long long n; cin>>n;
long long sign = n<0?-1:1;
n = llabs(n);
long long r=0;
while(n){
r=r*10 + n%10;
n/=10;
}
cout<<r*sign<<"\n";
}

import [Link].*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner([Link]);
long n=[Link]();
boolean neg=n<0;
if(neg) n=-n;
long r=0;
while(n>0){
r=r*10 + n%10;
n/=10;
}
[Link](neg?-r:r);
}
}

n=int(input())
sign=-1 if n<0 else 1
n=abs(n)
rev=0
while n:
rev = rev*10 + n%10
n//=10
print(sign*rev)

4. Check whether a number is an Armstrong number.


#include <stdio.h>
#include <math.h>
int main(){
int n; scanf("%d",&n);
int t=n, c=0;
while(t){ c++; t/=10; }
t=n; int s=0;
while(t){
int d=t%10;
s+=pow(d,c);
t/=10;
}
printf(s==n?"YES\n":"NO\n");
}

#include <bits/stdc++.h>
using namespace std;
int main(){
int n; cin>>n;
int t=n,c=0;
while(t){ c++; t/=10; }
t=n; int s=0;
while(t){
int d=t%10;
s+=pow(d,c);
t/=10;
}
cout<<(s==n?"YES":"NO")<<"\n";
}

import [Link].*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner([Link]);
int n=[Link]();
int t=n,c=0;
while(t>0){ c++; t/=10; }
t=n; int s=0;
while(t>0){
int d=t%10;
s+=[Link](d,c);
t/=10;
}
[Link](s==n?"YES":"NO");
}
}

n=int(input())
t=n; c=0
while t:
c+=1; t//=10
t=n; s=0
while t:
d=t%10
s+=d**c
t//=10
print("YES" if s==n else "NO")

5. Find the sum of digits of a number.


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

#include <bits/stdc++.h>
using namespace std;
int main(){
long long n; cin>>n;
n = llabs(n);
long long s=0;
while(n){ s+=n%10; n/=10; }
cout<<s<<"\n";
}

import [Link].*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner([Link]);
long n=[Link]();
if(n<0) n=-n;
long s=0;
while(n>0){
s+=n%10;
n/=10;
}
[Link](s);
}
}

n=int(input())
print(sum(int(d) for d in str(abs(n))))

6. Check whether a number is a perfect number.


#include <stdio.h>
int main(){
int n; scanf("%d",&n);
int s=0;
for(int i=1;i<=n/2;i++)
if(n%i==0) s+=i;
printf(s==n?"YES\n":"NO\n");
}

#include <bits/stdc++.h>
using namespace std;
int main(){
int n; cin>>n;
int s=0;
for(int i=1;i<=n/2;i++)
if(n%i==0) s+=i;
cout<<(s==n?"YES":"NO")<<"\n";
}

import [Link].*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner([Link]);
int n=[Link]();
int s=0;
for(int i=1;i<=n/2;i++)
if(n%i==0) s+=i;
[Link](s==n?"YES":"NO");
}
}

n=int(input())
s=sum(i for i in range(1,n//2+1) if n%i==0)
print("YES" if s==n else "NO")

7. Find the greatest common divisor (GCD) of two numbers.


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

#include <bits/stdc++.h>
using namespace std;
int gcd(int a,int b){
while(b){ int t=b; b=a%b; a=t; }
return a;
}
int main(){
int a,b; cin>>a>>b;
cout<<gcd(a,b)<<"\n";
}

import [Link].*;
public class Main{
static int gcd(int a,int b){
while(b!=0){
int t=b;
b=a%b;
a=t;
}
return a;
}
public static void main(String[] args){
Scanner sc=new Scanner([Link]);
int a=[Link](), b=[Link]();
[Link](gcd(a,b));
}
}

a,b=map(int,input().split())
while b:
a,b=b,a%b
print(a)

8. Find the least common multiple (LCM) of two numbers.


#include <stdio.h>
int gcd(int a,int b){
while(b){ int t=b; b=a%b; a=t; }
return a;
}
int main(){
int a,b; scanf("%d%d",&a,&b);
int g=gcd(a,b);
long long l=(long long)a/g*b;
printf("%lld\n",l);
}

#include <bits/stdc++.h>
using namespace std;
int gcd(int a,int b){
while(b){ int t=b; b=a%b; a=t; }
return a;
}
int main(){
int a,b; cin>>a>>b;
long long g=gcd(a,b);
cout<<(long long)a/g*b<<"\n";
}

import [Link].*;
public class Main{
static int gcd(int a,int b){
while(b!=0){
int t=b;
b=a%b;
a=t;
}
return a;
}
public static void main(String[] args){
Scanner sc=new Scanner([Link]);
int a=[Link](), b=[Link]();
int g=gcd(a,b);
long l=(long)a/g*b;
[Link](l);
}
}

a,b=map(int,input().split())
import math
print(a*b//[Link](a,b))

9. Print the Fibonacci series up to n terms.


#include <stdio.h>
int main(){
int n; scanf("%d",&n);
long long a=0,b=1;
if(n>=1) printf("%lld ",a);
if(n>=2) printf("%lld ",b);
for(int i=3;i<=n;i++){
long long c=a+b;
printf("%lld ",c);
a=b; b=c;
}
}
#include <bits/stdc++.h>
using namespace std;
int main(){
int n; cin>>n;
long long a=0,b=1;
if(n>=1) cout<<a<<" ";
if(n>=2) cout<<b<<" ";
for(int i=3;i<=n;i++){
long long c=a+b;
cout<<c<<" ";
a=b; b=c;
}
}

import [Link].*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner([Link]);
int n=[Link]();
long a=0,b=1;
if(n>=1) [Link](a+" ");
if(n>=2) [Link](b+" ");
for(int i=3;i<=n;i++){
long c=a+b;
[Link](c+" ");
a=b; b=c;
}
}
}

n=int(input())
a,b=0,1
if n>=1: print(a, end=" ")
if n>=2: print(b, end=" ")
for i in range(3,n+1):
print(a+b, end=" ")
a,b=b,a+b

10. Count how many digits are in a number.


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

#include <bits/stdc++.h>
using namespace std;
int main(){
long long n; cin>>n;
if(n==0){ cout<<1<<"\n"; return 0; }
n=llabs(n);
int c=0;
while(n){ c++; n/=10; }
cout<<c<<"\n";
}

import [Link].*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner([Link]);
long n=[Link]();
if(n==0){ [Link](1); return; }
if(n<0) n=-n;
int c=0;
while(n>0){ c++; n/=10; }
[Link](c);
}
}

n=input().strip()
if n[0]=='-': n=n[1:]
print(len(n))

You might also like