-
Notifications
You must be signed in to change notification settings - Fork 0
/
fact_sumOfDigits.R
98 lines (90 loc) · 1.83 KB
/
fact_sumOfDigits.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
fact<-function(number){
if(number>=0){
if(number==0){
cat("Factorial is: ",1,"\n")
}
else{
pro<-1
for(i in 1:number)
pro<-pro*i
cat("Factorial is: ",pro,"\n")
}
}else{
cat("Number should be non-negative\n")
}
}
isPrime<-function(number){
if(number>1){
flag<-0
for(i in 2:(number-1)){
if(number%%i == 0){
flag<-1
break
}
}
if(number==2)
return(TRUE)
if(flag==1){
return(FALSE)
}else{
return(TRUE)
}
}
else{
return(FALSE)
}
}
sumOfDigits<-function(number){
sum1<-0
while(number!=0){
sum1<-sum1+number%%10
number<-number%/%10
}
cat("Sum of digits is: ",sum1,"\n")
}
productOfDigits<-function(number){
pro<-1
while(number!=0){
pro<-pro*number%%10
number<-number%/%10
}
cat("Product of digits is: ",pro,"\n")
}
primeTill<-function(number){
if(number>1){
cat("Prime numbers till", number,"\n")
for(j in 2:(number-1)){
if(isPrime(j)){
print(j)
}
}
}else{
cat("No prime numbers\n")
}
}
repeat{
cat("---------------------------\n 1.Factorial \n 2.Check if number is prime \n 3.Sum of digits of number \n 4.Product of digits of number \n 5.Prime number till given number \n 6. Exit \n--------------------------\n")
choice<-readline(prompt = "Enter choice : \n");
choice<-as.integer(choice)
if(choice==6)
break
number<-readline(prompt = "Enter a number: ");
number<-as.integer(number)
if(choice == 1){
fact(number)
}else if(choice==2){
if(isPrime(number)){
print("Number is prime")
}else{
print("Number is not prime")
}
}else if(choice == 3){
sumOfDigits(number)
}else if(choice == 4){
productOfDigits(number)
}else if(choice == 5){
primeTill(number)
}else{
print("Be in limits")
}
}