Thursday, October 16, 2014

switch case used in c++



Prog 9: Write a program to make following output using switch case and for loop.
1) 1/1+1/2+1/3+…………1/n
2) 1/1+1/4+1/9+………….1/n2
3)  Exit

#include<iostream.h>
#include<conio.h>
main()
{
float sum=0.00; 
int i,n,ch; 
clrscr();
cout<<"enter your choice:";
cin>>ch;
switch(ch)
{


case 1:
cout<<"enter the value of n:";
cin>>n;
for(i=1;i<=n;i++)
{

sum=sum+1.00/i;
}
cout<<"sum="<<m;
break;
case 2:
cout<<"enter the value of n:";
cin>>n;
for(i=1;i<=n;i++)
{

sum=sum+1.00/(i*i);
}
cout<<"sum="<<m;
break;

case 3:
return 0;
}




getch();
}

Output:

enter your choice:1 enter the value of n:5 sum=2.283334

No comments: