Search This Blog

R Language Simple Programs

#Armstrong Number
number <- 1
sum <- 0
while (number <= 500)
{
  digit1 = number - ((number / 10) * 10)

  digit2 = (number / 10) - ((number / 100) * 10)

  digit3 = (number / 100) - ((number / 1000) * 10)

  temp = (digit1*digit1*digit1) + (digit2*digit2*digit2) + (digit3*digit3*digit3)

  if (temp == number)
  {
    print(temp);
 
  }

  number= number+1
}


#while loop in R
i <- 10
while (i>=0) {
  print(i)
  i=i-1
}

#Armstrong number (accept number from user)
num = as.integer(readline(prompt = "Enter a number:  "))
sum = 0
temp = num
while(temp > 0) {
  digit = temp %% 10
  sum = sum + (digit ^ 3)
  temp = floor(temp / 10)
}

if(num == sum) {
  print(paste(num, "is an Armstrong number"))
} else {
  print(paste(num, "is not an Armstrong number"))
}

#To print letters
var <- LETTERS[1:6]
va <- letters[6:1]
for (x in var) {
    print(x)
}
for (y in va) {
  print(y)
}

#To print greater number from three numbers
n1 <- readline(prompt = "Enter Number :-  ")
n2 <- readline(prompt = "Enter Number :-  ")
n3 <- readline(prompt = "Enter Number :-  ")
if (n1>n2)
{
  if(n1>n3)
  {
    print("n1 is greater")
  }else
  {
  print("n3 is greater")
  }
}else
  {
  if(n2>n3)
  {
    print("n2 is greater")
  }else{
    print("n3 is greater")
  }
}

#if else
my.name <- readline(prompt="enter ur name :- ")
my.age <- readline(prompt="enter ur age :- ")

if(my.age > 18)
  {
  print("Congrats U R Eligible for Voting")
  }else{
  print("Not Eligible for Voting")
}

#Fibonacci series
first <- 0
second <- 1
n <- 1:20
n1 <- 0
for(i in n)
  {
  if(i<=1)
    n1=i
  else
    {
    n1 <- first+second
    first=second
    second=n1
  }
  print(n1)
}


#Series 1:    S= 1*2 + 2*3 + 3*4 + 4*5 + 5*6 + 6*7 + 7*8 + 8*9 + 9*10
 n <- 1:9
n1 <- 1
n2 <- 2
sum <- 0
for(i in n){
  print(paste0(n1,"*",n2))
  sum <- (n1*n2)+sum
  n1 = n1 + 1
  n2 = n2 + 1
 }
print(sum)

Output:
[1] "1*2"
[1] "2*3"
[1] "3*4"
[1] "4*5"
[1] "5*6"
[1] "6*7"
[1] "7*8"
[1] "8*9"
[1] "9*10"
> print(sum)

[1] 330

#Series 2:    S= 1*2 + 3*4 + 5*6 + 7*8 + 9*10
n <- 1:5
n1 <- 1
n2 <- 2
sum <- 0
for(i in n){
  print(paste0(n1,"*",n2))
  sum <- (n1*n2)+sum
  n1 = n1 + 2
  n2 = n2 + 2
  }
print(sum)

Output:
[1] "1*2"
[1] "3*4"
[1] "5*6"
[1] "7*8"
[1] "9*10"
 > print(sum)
[1] 190

#Series 3:    S= 1*3 + 2*4 + 3*5 + 4*6 + 5*7 + 6*8 + 7*9 + 8*10 + 9*11
n <- 1:9
n1 <- 1
n2 <- 3
sum <- 0
for(i in n){
  print(paste0(n1,"*",n2))
  sum <- (n1*n2)+sum
  n1 = n1 + 1
  n2 = n2 + 1
}
print(sum)

Output:
[1] "1*3"
[1] "2*4"
[1] "3*5"
[1] "4*6"
[1] "5*7"
[1] "6*8"
[1] "7*9"
[1] "8*10"
[1] "9*11"
> print(sum)
[1] 375

#Series 4:    S= 1-2 + 3-4 + 5-6 + 7-8 + 9-10 + 11-12 + 13-14 + 15-16 + 17-18 + 19-20
n <- 1:10
n1 <- 1
n2 <- 2
sum <- 0
for(i in n){
  print(paste0(n1,"-",n2))
  sum <- (n1-n2)+sum
  n1 = n1 + 2
  n2 = n2 + 2
}
print(sum)

Output:
[1] "1-2"
[1] "3-4"
[1] "5-6"
[1] "7-8"
[1] "9-10"
[1] "11-12"
[1] "13-14"
[1] "15-16"
[1] "17-18"
[1] "19-20"
> print(sum)
[1] -10

#Series 5:    S=1 + ½ + 1/3 + ¼ + 1/5+………+1/10 

n <- 1:10
sum <- 0
for(i in n){
  print(paste0(1/i,"+"))
  sum <- sum + (1/i)
}
print(sum)

 Output:
[1] "1+"
[1] "0.5+"
[1] "0.333333333333333+"
[1] "0.25+"
[1] "0.2+"
[1] "0.166666666666667+"
[1] "0.142857142857143+"
[1] "0.125+"
[1] "0.111111111111111+"
[1] "0.1+"
> print(sum)
[1] 2.928968

#Series 6:   S=½ + 2/3 + ¾ + 4/5 + 5/6 + 6/7 + 7/8 + 8/9 + 9/10
n <- 1:9
sum <- 0
n1 <- 1
n2 <- 2
for(i in n){
  print(paste0(n1,"/",n2))
  sum <- sum + (n1/n2)
  n1 <- n1+1
  n2 <- n2+1
}
print(sum)

Output:
[1] "1/2"
[1] "2/3"
[1] "3/4"
[1] "4/5"
[1] "5/6"
[1] "6/7"
[1] "7/8"
[1] "8/9"
[1] "9/10"
> print(sum)
[1] 7.071032

#Series 7:    S= 1 – ½ + 1/3 – ¼ + 1/5 – 1/6 + 1/7 – 1/8 + 1/9 -1/10
n1 <- 1
n2 <- 1
sum <- 0
while(n2<=10)
{
  if(n2%%2==0){
    sum <- sum-(n1/n2)
  }else{
    sum <- sum+(n1/n2)
  }
  n2 <- n2+1
}
print(sum)

Output:
[1] 0.6456349

No comments:

Post a Comment