0% found this document useful (0 votes)
77 views7 pages

22practice Assignment 2

The document contains 7 programming problems involving series calculations. It provides the full code solutions for each problem. The problems include printing ASCII values and characters, calculating the sum of squares of natural numbers, calculating harmonic series, calculating series involving powers and factorials, finding the Nth term of a factorial series, calculating a factorial and summing a series involving factorials and reciprocals, and printing out a repeating series.

Uploaded by

Nitesh Yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views7 pages

22practice Assignment 2

The document contains 7 programming problems involving series calculations. It provides the full code solutions for each problem. The problems include printing ASCII values and characters, calculating the sum of squares of natural numbers, calculating harmonic series, calculating series involving powers and factorials, finding the Nth term of a factorial series, calculating a factorial and summing a series involving factorials and reciprocals, and printing out a repeating series.

Uploaded by

Nitesh Yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 7

PRACTICE ASSIGNMENT 2

Name: Nitesh Kumar Yadav


Reg. No.: 20195103
Group: B1

Write a program to print all the ASCII values and their equivalent characters using while
loop. The ASCII values vary from 0 to 255.
#include<stdio.h>

int main()
{
char ascii;
int i;

for(i=0;i<=255;i++)
{
printf("%c = %d\n", i, i);
}
}

2. WAP to implement following series.


i. 1^2+2^2+3^2+4^2+...N^2

#include<stdio.h>

int main()
{
int i,n,sum=0;
printf("Input the number of terms : ");
scanf("%d",&n);
printf("\nThe square natural upto %d terms are :",n);
for(i=1;i<=n;i++)
{
printf("%d ",i*i);
sum+=i*i;
}
printf("\nThe Sum of Square Natural Number upto %d terms = %d \n",n,sum);
}

ii. 1+ 1/2 + 1/3 + 1/4 + 1/5 + .. 1/N

#include <stdio.h>
double sum(int n)
{
double i, s = 0.0;
for (i = 1; i <= n; i++)
s = s + 1/i;
PRACTICE ASSIGNMENT 2
return s;
}

int main()
{
int n = 5;
printf("Sum is %f", sum(n));
}

iii. 1 + 3^2/3^3 + 5^2/5^3 + 7^2/7^3 + ... till N terms

#include<stdio.h>
#include<math.h>

int main()
{
int i,N;
float sum;
int count;

printf("Enter total number of terms: ");


scanf("%d",&N);

sum=0.0f;

count=1;
for(i=1;i<=N;i++)
{
sum = sum + ( (float)(pow(count,2)) / (float)(pow(count,3)) );
count+=2;
}

printf("Sum of the series is: %f\n",sum);

iv. [(1^1)/1!] + [(2^2)/2!] + [(3^3)/3!] + [(4^4)/4!] + [(5^5)/5!] + ... + [(n^n)/n!]

#include <stdio.h>

double sumseries(double);
PRACTICE ASSIGNMENT 2

main()
{
double number,sum;
printf("\n Enter the value: ");
scanf("%lf", &number);
sum = sumseries(number);
printf("\n Sum of the above series = %lf ", sum);
}

double sumseries(double m)
{
double sum2 = 0, f = 1, i;
for (i = 1; i <= m; i++)
{
f = f * i;
sum2 = sum2 +(i / f);
}
return(sum2);
}

v. 1/2 - 2/3 + 3/4 - 4/5 + 5/6 - ...... n


vi. (1) + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+n)

#include <stdio.h>

void summation(int);
void seriesPrinter(int);
int main()
{
int n;
printf("Enter the limit of the series: ");
scanf("%d",&n);
printf("\n");
seriesPrinter(n);
summation(n);
return 0;
}

void summation(int n)
{
int sum = (n*(n+1)*(n+2))/6;
printf("%d",sum);
}
void seriesPrinter(int n)
{
PRACTICE ASSIGNMENT 2
int count = 1;
int nextTerm = count + 1;
for(nextTerm = count + 1;nextTerm<=n+1;nextTerm++)
{
printf("(");
for(count = 1;count<nextTerm;count++)
{
printf("%d",count);
if(count != nextTerm-1)
{
printf("+");
}
else
{
;
}
}
printf(")");
if(nextTerm == n+1)
{
;
}
else
{
printf("+");
}

}
printf("=");
}

vii. a, b, b, c, c, c, d, d, d, d, …..

3. Given a number N. The task is to write a program to find the Nth term in the below
series: 1, 3, 12, 60, 360…

#include <stdio.h>
using namespace std;

int factorial(int N)
{
int fact = 1;
for (int i = 1; i <= N; i++)
PRACTICE ASSIGNMENT 2
fact = fact * i;
return fact;
}

int nthTerm(int N)
{
return (factorial(N + 1) / 2);
}
int main()
{
int N = 6;
printf("%d",nthTerm(N));
}

4. Write a program to calculate the factorial of a number. Then calculate the sum of n
terms of the following series: 1/1! + 2/2! + 3/3! + ……. + n/n! . N will be key board
input. N&gt;=10. Factorial of n= n*(n-1)*(n-2)*(n-3)* 1

#include<stdio.h>

int main()
{
int c, n, f = 1;

printf("Enter a number to calculate its factorial\n");


scanf("%d", &n);
for (c = 1; c <= n; c++)
f = f * c;

printf("Factorial of %d = %d\n", n, f);

return 0;
}

#include <stdio.h>

double sumseries(double);

main()
{
double number,sum;
PRACTICE ASSIGNMENT 2
printf("\n Enter the value: ");
scanf("%lf", &number);
sum = sumseries(number);
printf("\n Sum of the above series = %lf ", sum);
}

double sumseries(double m)
{
double sum2 = 0, f = 1, i;
for (i = 1; i <= m; i++)
{
f = f * i;
sum2 = sum2 +(i / f);
}
return(sum2);
}
PRACTICE ASSIGNMENT 2

You might also like