Problem statement:- Program to print series 1 9 17 33 49 73 97 ...N.
Data requirement:-
Input Data:- n
Output Data:-pr
Additional Data:- i
Program in C
Input Data:- n
Output Data:-pr
Additional Data:- i
Program in C
Here is the source code of the C Program to print series 1 9 17 33 49 73 97 ...N.
Code:
#include<stdio.h>
#include<math.h>
int main()
{
int n,i,pr=0;
printf("Enter the range of number(Limit):");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i%2==0)
{
pr=2*pow(i,2)+1;
printf("%d ",pr);
}
else
{
pr=2*pow(i,2)-1;
printf("%d ",pr);
}
}}
Enter the range of number(Limit):7
1 9 17 33 48 73 97
Program in C++
Here is the source code of the C++ Program to print series 1 9 17 33 49 73 97 ...N.
Code:
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int n,i,pr=0;
cout<<"Enter the range of number(Limit):";
cin>>n;
for(i=1;i<=n;i++)
{
if(i%2==0)
{
pr=2*pow(i,2)+1;
cout<<pr<<" ";
}
else
{
pr=2*pow(i,2)-1;
cout<<pr<<" ";
}
}}
Enter the range of number(Limit):5
1 9 17 33 48
Program in Java
Here is the source code of the Java Program to print series 1 9 17 33 49 73 97 ...N.
Code:
import java.util.Scanner;
public class Print_Series3 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n,i,pr=0;
System.out.printf("Enter the range of number(Limit):");
n=sc.nextInt();
for(i=1;i<=n;i++)
{
if(i%2==0)
{
pr=(int) (2*Math.pow(i,2)+1);
System.out.print(pr+" ");
}
else
{
pr=(int) (2*Math.pow(i,2)-1);
System.out.printf(pr+" ");
}
}
sc.close();
}
}
Enter the range of number(Limit):6
1 9 17 33 49 73
Program in Python
Here is the source code of the Python Program to print series 1 9 17 33 49 73 97 ...N.
Code:
n=int(input("Enter the range of number(Limit):"))
i=1
pr=0
while i<=n:
if(i%2==0):
pr=2*pow(i, 2) +1
print(pr,end=" ")
else:
pr = 2*pow(i, 2) - 1
print(pr, end=" ")
i+=1
Enter the range of number(Limit):5
1 9 17 33 49
More Questions:-
0 Comments
Please do not Enter any spam link in the comment box