PF Lab Assignment 2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Name: MOEEZ SHAHZAD

Roll No. 23P-0673


Section: BS(CS) 1-A

Question 1:

/*
Name : Moeez Shahzad
Roll no: 23P-0673
Section : BCS 1-A
*/

#include<stdio.h> //Header file


int main(void) //main function
{
int itration; //Locally declared variables
float pi=3.00;
float a=2.00;
do //Used do while loop in the user enter negative number
{
printf("Please enter any positive integer for itration you want: ");
scanf("%d",&itration); //Number of itration taken by user
printf("Loading......\n\n");
if(itration<0) //It will generate an error If user input negative value
{ printf("...................ERROR\n");
printf("You have entered negative value.\nPlease enter positive value
again\n");
}
}
while(itration<0);
if(itration==1) //Used if condition if Number of Itration is 1
{
printf("Approximately value of pi is %f by providing number
%d\n",pi,itration);//Display value of pi on itration 1
return 0;//stops program
}
float arr[itration];//if itration will be greater then 1 then it should be stored in
array
float sign=1;//It is used for different sign after every itration
for(int i=0;i<itration-1;i++)//Used for loop for itration as we know
number of itrations
{
arr[i]=sign*(4/(a*(a+1.00)*(a+2.00)));//Here is the main logic
sign = -sign;//Changing sign for after every every itration
a +=2;
}
for(int j=0;j<itration;j++)//again using for loop for for adding value of
array in pi
{
pi+=arr[j];
}
printf("Approximately value of pi is %f by providing number
%d\n",pi,itration);//Display approximate value of pi
printf("------------------------------------------------------------------------\n");
return 0;//Ending program
}

You might also like