0% found this document useful (0 votes)
20 views6 pages

Programming For Problem Solving: Experiment - 4

The document outlines various programming tasks in C++, including writing code for calculating averages, Fibonacci series, and the sum of squares of digits. It also discusses differences in for loop syntax and provides expected outputs for specific code snippets. Additionally, it includes sample programs for summing a series and calculating the sum of squares of digits.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views6 pages

Programming For Problem Solving: Experiment - 4

The document outlines various programming tasks in C++, including writing code for calculating averages, Fibonacci series, and the sum of squares of digits. It also discusses differences in for loop syntax and provides expected outputs for specific code snippets. Additionally, it includes sample programs for summing a series and calculating the sum of squares of digits.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

PROGRAMMING FOR PROBLEM SOLVING

EXPERIMENT - 4

Task 1: For given algorithm write C++ code using for loop and while loop.
1. Start
2. Print “How many numbers”
3. Input N
S=0: For given algorithm write C++ code using for loop and while loop.
1. Start
2. Print “How many numbers”
3. Input N
4. S=0
5. C=1
6. Print “Enter the number”
7. Input A
8. S=S+A
9. C=C+1
10.If C<=N then GOTO Step 6
11.Avg=S/N
12.Print “Average is “ Avg
13.Stop
PROGRAM:
#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"How many numbers ";
cin>>n;
float S = 0;
int C = 1;
float A;

while(C<=n){
cout<<"Enter the number ";
cin>>A;
S = S + A;
C = C + 1;
}
float avg = S/(float)n;
cout<<"Average is "<<avg;

return 0;
}

Task 2: Is there any difference between the following for statements?


Explain
1. for(x=1;x<100;x++);
x will be post incremented
2. for(x=1;x<100;++x);
x will be pre incremented
3. for(x=1;x<100;x=x+1);
x will be increased by 1
4. for(x=1;x<100;x+=1);
Add and assignment operator, it adds right operand to the left operand
and assign the result to the left operand
5. x=1; for(;x<100;x++);
We cannot initialize the x value out of the for loop
Task 3: State the output or error without using code blocks for below
blocks of code. Give your justification.
1. #include<iostream> 2. #include<iostream>
void main() void main()
{ {
int c; int x=10;
for(c=5;c<5;c++) for(;x<=50;x++);
cout<<“\n Hello”; cout<<x;
} }
NO OUTPUT WILL BE SHOWN OUTPUT: 51
3. #include<iostream> 4. #include<iostream>
void main() void main()
{ {
int c=5; int c=5;
while( c ) while( c )
{ {
cout<<c; cout<<c;
c--; c=c-2;
} }
} }
OUTPUT: 54321 OUTPUT WILL BE IN INFINITE
LOOP
5. #include<iostream> 6. #include<iostream>
void main() void main()
{ {
int k=1000; int c=1;
while(k>0) while(1)
cout<<“I am a loop”; {
} cout<<c;
OUTPUT: I am a loop c++;
(for 1000 times) }
ERROR WILL BE SHOWN.
WE HAVE TO CLOSE }
7. iB = 10; 8. int i = 0;
while ( iB < 50) for (i=0; i<20; i++)
{ {
iTry = iB * 10; switch(i)
cout<<"\nNow is number: "<<iTry; {
iB += 5; case 0:
} i += 5;
ERROR WILL SHOWN case 1:
WE HAVE NOT iB and iTry i += 2;
case 5:
i += 5;
default:
i += 4;
break;
}
cout<<i;
}
OUTPUT: 1621
Task 4: Write a C++ program to print Fibonacci series up to N. (for loop)
#include <iostream>
using namespace std;

int main() {
int n, t1 = 0, t2 = 1, nextTerm = 0;

cout << "Enter the number of terms: ";


cin >> n;

cout << "Fibonacci Series: ";

for (int i = 1; i <= n; ++i) {

if(i == 1) {
cout << t1 << ", ";
continue;
}
if(i == 2) {
cout << t2 << ", ";
continue;
}
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;

cout << nextTerm << ", ";


}

return 0;
}

Task 5: Write a C++ program and draw flowchart to calculate the sum of
square of digits of a number given by the user. (while loop)
PROGRAM:
#include<iostream>
using namespace std;
int main()
{
int num, rem, sq, sum=0;
cout<<"Enter a Number: ";
cin>>num;
while(num>0)
{
rem = num%10;
if(rem==0)
sq = 1;
else
sq = rem*rem;
sum = sum + sq;
num = num/10;
}
cout<<"\nSum of squares of all digits = "<<sum;
cout<<endl;
return 0;
}

Task 6: Write a C++ program to print the sum of the following series up to
n terms where n is given by the user.
1+x+x2/2! + x3/3! +… n terms
PROGRAM:
#include <iostream>
using namespace std;

int main()
{
float x, sum, no_row;
int i, n;
cout << "\n\n Display the sum of the series [ 1+x+x^2/2!+x^3/3!+....]\n";
cout << " Input the value of x: ";
cin >> x;
cout << " Input number of terms: ";
cin >> n;
sum = 1;
no_row = 1;
for (i = 1; i < n; i++)
{
no_row = no_row * x / (float)i;
sum = sum + no_row;
}
cout << " The sum is : " << sum << endl;
return 0;
}

You might also like