#include<stdio.h>
int main()
{
int i, n, a=0, b=0;
printf("enter number : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i%2!=0)
{
if(i>1)
a = a + 2;
}
else
{
b = a/2;
}
}
if(n%2!=0)
{
printf("%d",a);
}
else
{
printf("%d",b);
}
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int i, n, a=0, b=0;
cout << "enter number : ";
cin >> n;
for(i=1;i<=n;i++)
{
if(i%2!=0)
{
if(i>1)
a = a + 2;
}
else
{
b = a/2;
}
}
if(n%2!=0)
{
cout << a;
}
else
{
cout << b;
}
return 0;
}
//Java program to find nth element of the series
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a = 0, b = 0;
if(n % 2 == 0)
{
for(int i = 1 ; i <= (n-2) ; i = i+2)
{
a = a + 2;
b = a / 2;
}
System.out.print(b);
}
else
{
for(int i = 1 ; i < (n-2) ; i = i+2)
{
a = a + 2;
b = a / 2;
}
a = a + 2;
System.out.print(a);
}
}
}
n = int(input('enter the number:'))
a=0
b=0
for i in range(1,n+1):
if(i%2!=0):
a= a+2
else:
b= b+1
if(n%2!=0):
print('{}'.format(a-2))
else:
print('{}'.format(b-1))
More efficient way in python:
num = int(input())
odd,even = 0,0
if(num%2==0):
even = (num-1)//2
print(even)
else:
odd = num – 1
print(odd)
n = int(input(‘enter the number:’))
if n%2!=0:
print(n-1)
else:
print((n-2)/2)
#include
int main(){
int i,k,c=0,v=0;
scanf(“%d”,&k);
int a[20000];
for(i=1;i<=k;i++){
a[i]=c;
c+=2;
a[++i]=v++;
}
printf("%d",a[k]);
return 0;
}
#include
int main()
{
int n;
scanf(“%d”,&n);
if(n%2!=0)
printf(“%d”,n-1);
else
printf(“%d”,(n/2)-1);
}
package tcs;
import java.util.Scanner;
public class Tcs4 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n=scan.nextInt();
int a=0;
int b=0;
System.out.println(a);
System.out.println(b);
for(int i=3;i<=n;i++)
{
if(i%2==0)
{
b=a/2;
System.out.println(b);
}
else
{
a=a+2;
System.out.println(a);
}
}
}
}
import java.util.Scanner;
public class series
{
public static void main(String[] args)
{
System.out.println(“enter n”);
Scanner obj = new Scanner(System.in);
int n = obj.nextInt();
int i=n/2;
System.out.println((n%2)!=0?i*2:i-1);
}
}
If n-1 is odd ,return (n-1)/2;
If n-1 is even, return (n-1);
Two line solution.
#PYTHON 3.7
#this program also gives the same result
i=int(input(‘N(positive integer only)=’))
if i%2==0: print(i//2-1,end=’,’)
else: print(i-1,end=’,’)
#include
int a[20];
void main()
{
int i,j,n;
scanf(“%d”,&n);
for(i=1;i<=n;i=i+2)
{
a[i]=(i-1)/2;
}
for(j=0;j<=n;j=j+2)
{
a[j]=j;
}
printf("%d",a[n-1]);
}
n=int(input())
if n%2==0:
n=int(n/2)-1
else:
n=n-1
print(n)
Recursive code in C
main()
{
int n;
scanf(“%d”,&n);
printf(“%d”,term(n));
}
int term(int n){
int a;
if(n%2!=0)
return (n-1);
else
return(term(n-1)/2);
}
n=int(input())
if n>20000:
print(0)
else:
if n%2==1:
print(2*(n//2))
else:
print((n//2)-1)