PF Lab A4
PF Lab A4
PF-LAB
#include <iostream>
using namespace std;
int main()
{
cout<<"\n\t COUNTING 1-10\n\n";
int counter=1;
return 0;
}
2. Write a program that displays first five numbers and their sum using while loop.
#include <iostream>
using namespace std;
int main()
{
//using while loop
int counter=1, sum=0;
while (counter<=5)
{
sum = sum + counter;
cout<<"\n\t"<<counter<<endl;
counter++;
}
cout<<"\tSum of first 5 numbers is \'"<<sum<<"\'\n";
return 0;
}
3. Write a program that displays first five numbers with their squares using while
loop.
#include <iostream>
using namespace std;
int main()
{
int counter=1, square;
while (counter<=5)
{
square = counter * counter;
cout<<"\n\t\'"<<counter<<"\' square of "<<counter<<"
is \'"<<square<<"\'\n";
counter++;
}
return 0;
}
4. Write a program that inputs starting and ending number from the user and then
displays all even numbers in the given range.
#include <iostream>
using namespace std;
int main()
{
int counter, num1, num2;
while (counter<=num2)
{
if (counter%2==0)
{
cout<<"\t"<<counter<<endl;
}
counter++;
}
return 0;
}
5. Write a program that’s inputs a number from the user and displays a table of that
number using while loop.
#include <iostream>
using namespace std;
int main()
{
//using while loop
cout<<"\n\tPRINT TABLE OF ANY NUMBER\n\n";
int counter=1, num, table;
cout<<"Enter a number:\n";
cin>>num;
while (counter<=10)
{
cout<<"\t"<<num<<" * "<<counter<<" = "<<num *
counter<<endl;
counter++;
}
return 0;
}
6. Write a program that displays the following series using while loop:
1 3 5 7 9 11 13 15 17 19 21.
#include <iostream>
using namespace std;
int main()
{
int counter=1;
cout<<"\n";
while(counter<=22)
{
cout<<"\t"<<counter;
counter += 2;
}
return 0;
}
7. Write a program that inputs a number from the user and display the factorial of
that number using while loop.
#include <iostream>
using namespace std;
int main()
{
//using do while loop
cout<<"\n\tFACTORIAL OF ANY NUMBER\n\n";
int counter=1, num, factorial=1;
cout<<"\tEnter a number:\n";
cin>>num;
while (counter<=num)
{
factorial *= counter;
counter++;
}
cout<<"\tfactorial of \'"<<num<<"\'' is \'"<<factorial<<"\'"<<endl;
return 0;
}
8. Write a program that displays back counting from 10 to 1 using do while loop.
#include <iostream>
using namespace std;
int main()
{
//using do while loop
cout<<"\n\tBACK COUNTING 10-1\n\n";
int counter=10;
do
{
cout<<"\t"<<counter<<endl;
counter--;
}
while (counter>=1);
return 0;
}
9. Write a program that displays first five numbers with their cubes using do while
loop.
#include <iostream>
using namespace std;
int main()
{
//using do while loop
cout<<"\n\tFIRST FIVE NUMBERS WITH THEIR CUBES\n\n";
int counter=1;
do
{
cout<<"\tNumber is \'"<<counter<<"\' & it\'s cube
is \'"<<counter*counter*counter<<"\'"<<endl;
counter++;
}
while (counter<=5);
return 0;
}
10. Write a program that displays first ten odd numbers using do while loop.
#include <iostream>
using namespace std;
int main()
{
//using do while loop
cout<<"\n\t1st TEN ODD NUMBERS\n\n";
int counter=1;
do
{
cout<<"\t"<<counter<<endl;
counter += 2;
}
while (counter<=20);
return 0;
}
11. Write a program that prints odd numbers from 11 to 49 using loop.
#include <iostream>
using namespace std;
int main()
{
// using for loop
cout<<"\n\tODD NUMBERS FROM 11-49\n\n";
int counter;
return 0;
}
12. Write a program that inputs a number from the user and displays the factorial of
that number using do while loop.
#include <iostream>
using namespace std;
int main()
{
//using do while loop
cout<<"\n\tFACTORIAL OF ANY NUMBER\n\n";
int counter=1, num, factorial=1;
cout<<"\tEnter a number:\n";
cin>>num;
do
{
factorial *= counter;
counter++;
}
while (counter<=num);
cout<<"\tfactorial of \'"<<num<<"\'' is \'"<<factorial<<"\'"<<endl;
return 0;
}
13. Write a program that inputs a number from the user and displays the table of that
number using do while loop.
#include <iostream>
using namespace std;
int main()
{
//using do while loop
cout<<"\n\tPRINT TABLE OF ANY NUMBER\n\n";
int counter=1, num, table;
cout<<"Enter a number:\n";
cin>>num;
do
{
cout<<"\t"<<num<<" * "<<counter<<" = "<<num *
counter<<endl;
counter++;
}
while (counter<=10);
return 0;
}
14. Write a program that gets starting and ending point from the user and displays all
odd numbers in the given range using do while loop.
#include <iostream>
using namespace std;
int main()
{
int counter, num1, num2;
cout<<"\tEnter starting number:\n";
cin>>num1;
counter = num1;
cout<<"\tEnter ending number:\n";
cin>>num2;
cout<<"\tHere're all the odd numbers b/w \'"<<num1<<" to
"<<num2<<"\'\n";
do
{
cout<<"\t"<<counter<<endl;
counter +=2;
}
while (counter<=num2);
return 0;
}
15. Write a program that gets two numbers from the user and displays the result of
first number raise to the power of second number using do while loop.
#include <iostream>
using namespace std;
int main() {
char choice;
do {
float a, b, power = 1;
cout << "Enter 1st number: ";
cin >> a;
cout << "Enter 2nd number: ";
cin >> b;
int i = 0;
while (i < b) {
power *= a;
i++;
}
cout << a << " raised to the power of " << b << " is " << power <<
endl;
return 0;
}
16. Write a program that displays the sum of the following series using do while loop.
1+1/3+1/5+1/7+…………. +1/99.
#include <iostream>
using namespace std;
int main() {
float sum = 0;
int counter = 1;
do
{
sum += 1.0 / counter;
counter += 2;
}
while (counter <= 99);
cout << "Sum of the series: " << sum << endl;
return 0;
}
17. Write a program to display counting from 1 to 10.
#include <iostream>
using namespace std;
int main()
{
#include <iostream>
using namespace std;
int main()
{
cout<<"\tAlphabets from A to Z\n\n";
int counter;
return 0;
}
19. Write a program that displays first ten numbers and their average using for loop.
#include <iostream>
using namespace std;
int main ()
{
int counter, sum=0;
float avg;
return 0;
}
20. Write a program to print the sum of odd numbers between 1 to 10.
#include <iostream>
using namespace std;
int main()
{
//using for loop
cout<<"\n\tSUM OF ODD NUMBERS BETWEEN 1 TO 10\n\n";
int counter, sum=0;
return 0;
}
21. Write a program to print the sum of odd numbers between 1 and 100.
#include <iostream>
using namespace std;
int main()
{
//using for loop
cout<<"\n\tSUM OF ODD NUMBERS BETWEEN 1 TO 100\n\n";
int counter, sum=0;
return 0;
}
22. Write a program that inputs a number from the user and displays the factorial of
that number using for loop.
#include <iostream>
using namespace std;
int main()
{
//using for loop
cout<<"\tFIND FACTORIAL OF ANY NUMBER\n\n";
int counter, num, factorial=1;
cout<<"Enter a number:\n";
cin>>num;
for (counter=1; counter<=num; counter++)
{
factorial = factorial * counter;
}
cout<<"Factorial of "<<num<<" is "<<factorial<<endl;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int fahrenheit;
double celsius;
cout << fahrenheit <<” Fahrenheit = ” << celsius <<” celsius”<< endl;
}
return 0;
}
24. Write a program that displays product of all odd numbers from 1 to 10 using for
loop.
#include <iostream>
using namespace std;
int main()
{
//using for loop
cout<<"\tPRODUCT OF ODD NUMBERS BETWEEN 1-10\n\n";
int counter, product=1;
return 0;
}
25. Write a program that inputs a number from the user and displays a table of that
number using for loop.
#include <iostream>
using namespace std;
int main()
{
//using for loop
cout<<"\n\tPRINT TABLE OF ANY NUMBER\n\n";
int counter, num, table;
cout<<"Enter a number:\n";
cin>>num;
return 0;
}
26. Write a program that inputs table number and length of table and then displays
the table using for loop.
#include <iostream>
using namespace std;
int main()
{
//using for loop
cout<<"\n\tPRINT TABLE OF ANY NUMBER\n\n";
int counter, num, length, table;
cout<<"Enter a number:\n";
cin>>num;
cout<<"Enter the length of the table:\n";
cin>>length;
return 0;
}
27. Write a program that inputs an integer and displays its table in descending order
using for loop.
#include <iostream>
using namespace std;
int main()
{
//using for loop
cout<<"\n\tPRINT TABLE OF ANY NUMBER IN DESCENDING ORDER\n\
n";
int counter, num, table;
cout<<"Enter a number:\n";
cin>>num;
return 0;
}
28. Write a program that finds the sum of squares of the integers from 1 to n. where n
is a positive value entered by the user (i.e., sum=12+22+33+……n2 ).
#include <iostream>
using namespace std;
int main()
{
int i, n, sum=0, square;
return 0;
}
29. Write a program that prints all odd positive integers less than 100 skipping those
that are exactly divisible by 7.
#include <iostream>
using namespace std;
int main()
{
int i;
return 0;
}
30. Write a program in C++ language to print the following series. 0 3 6 9 12 15.
#include <iostream>
using namespace std;
int main()
{
int i;
return 0;
}
31. Write a program that displays the sum of positive odd numbers and the product of
positive even numbers from 1 to 10.
#include <iostream>
using namespace std;
int main()
{
int i, sum=0, product=1;
return 0;
}
32. A class of 35 students takes an examinations in which marks range from 0 to 100.
Write a program which finds: a. average marks b. number of failed student (marks
below 50) c. number of students who scored 100 marks.
#include <iostream>
using namespace std;
int main()
{
int student, f_s=0, top_s=0, sum=0;
float marks, avg=0;
sum += marks;
if (marks==100)
{
top_s++;
}
if (marks<50)
{
f_s++;
}
}
avg = marks/35;
cout<<"average marks = "<<avg<<endl;
cout<<"Number of Students who failed = "<<f_s<<endl;
cout<<"Number of students who scored 100 = "<<top_s<<endl;
return 0;
}
33. Write a program that inputs a positive integer number from the keyboard and
display it is in reverse number. for example, the reverse of 345 is 543.
#include <iostream>
using namespace std;
int main()
{
int number, reverse = 0;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int i, j;
return 0;
}
35. Write a program that displays the following shape using nested loops. the outer loop
should be for loop and inner loop should while loop.
*****
****
***
**
*
#include <iostream>
using namespace std;
int main()
{
int i, j;
cout<<"\n";
}
return 0;
}
35. Write a program that displays the following using do while loop.
4444
333
22
1
#include <iostream>
using namespace std;
int main()
{
int i=4;
do
{
int counter = 1;
do
{
cout<<" "<<i;
counter++;
}
while (counter<=i);
i--;
cout<<"\n";
}
while (i>=1);
return 0;
}
36. Write a program that inputs an integer from the user. it displays the sum of
integers for each integer from 1 to the given integer.
#include <iostream>
using namespace std;
int main()
{
int counter=1, num, sum=0;
cout<<"Enter an integer:\n";
cin>>num;
while (counter<=num)
{
sum = sum + counter;
counter++;
}
cout<<"Sum of integers from \'1\' to \'"<<num<<"\' is "<<sum<<":\
n";
return 0;
}
37. Write a program that inputs an integer from the user. it displays the integer and its
factorial from 1 to the given integer.
#include <iostream>
using namespace std;
int main()
{
int counter, n, factorial=1;
cout<<"\tEnter an integer:\n";
cin>>n;
return 0;
}
38. Write a program that displays the following shape using nested for loop.
*
**
***
#include <iostream>
using namespace std;
int main()
{
int i, j;
return 0;
}
39. The following program display the following shape using nested for loops.
****
***
**
*
#include <iostream>
using namespace std;
int main()
{
int i, j;
return 0;
}
49. Write a program that displays the following shape using nested for loops
*
**
***
****
#include <iostream>
using namespace std;
int main()
{
int i, j;
return 0;
}