0% found this document useful (0 votes)
11 views

PF Lab A4

jao rakh lo kya yaad kro gy
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)
11 views

PF Lab A4

jao rakh lo kya yaad kro gy
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/ 47

Assignment #03

PF-LAB

Submitted by: Tahira Inam


(Roll No: 105)
Instructor: Maham Sajjad
1st Semester (Blue)

Department of Software Engineering


University of Sialkot
PROGRAMING FUNDAMENTALS
C++ LOOPS

1. write a program that displays counting from 1 to 10 using while loop.

#include <iostream>
using namespace std;

int main()
{
cout<<"\n\t COUNTING 1-10\n\n";
int counter=1;

//using while loop


while (counter<=10)
{
cout<<"\t"<<counter<<endl;
counter++;
}

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;

cout<<"\tEnter starting number:\n";


cin>>num1;
counter = num1;
cout<<"\tEnter ending number:\n";
cin>>num2;
cout<<"\tHere're all the even numbers b/w \'"<<num1<<" to
"<<num2<<"\'\n";

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;

for (counter=1; counter<=50; counter+=2)


{
cout<<"\t"<<counter<<endl;
}

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;

cout << "Do you want to continue (y/n)? ";


cin >> choice;
} while (choice == 'y' || choice == 'Y');

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()
{

cout<<"\tcounting from 1 to 10\n";


int counter=1;

//using for loop


for (counter; counter<=10; counter++)
{
cout<<counter<<endl;
}
return 0;
}
18. Write a program to display alphabets from A to Z.

#include <iostream>
using namespace std;

int main()
{
cout<<"\tAlphabets from A to Z\n\n";
int counter;

for (counter=65; counter<=90; counter++)


{
cout<<"\t"<<char(counter)<<endl;
}

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;

for (counter=1; counter<=10; counter++)


{
cout<<"\t"<<counter<<"\n"<<endl;
sum = sum + counter;
avg = sum / counter;
}
cout<<"\tThe average of 1st 10 numbers is "<<avg<<endl;

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;

for (counter=1; counter<=10; counter+=2)


{
sum = sum + counter;
}
cout<<"\tThe sum of odd numbers between 1 to 10 is "<<sum<<endl;

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;

for (counter=1; counter<=100; counter+=2)


{
sum = sum + counter;
}
cout<<"\tThe sum of all the odd numbers between 1 to 100 is
"<<sum<<endl;

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;
}

23. Write a program to displays a table of equivalent temperatures in Fahrenheit and


Celsius from 50F to 100F with an increment of 5.

#include <iostream>
using namespace std;

int main()
{
int fahrenheit;
double celsius;

cout << "Fahrenheit & Celsius" << endl;

for (fahrenheit = 50; fahrenheit <= 100; fahrenheit += 5)


{

celsius = (fahrenheit - 32) * 5.0 / 9.0;

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;

for (counter=1; counter<=10; counter+=2)


{
product = product * counter;
}
cout<<"\tThe Product of all the odd numbers between 1-10
is \'"<<product<<"\'";

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;

for (counter=1; counter<=10; counter++)


{
cout<<"\t"<<num<<" * "<<counter<<" = "<<num *
counter<<endl;
}

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;

for (counter=1; counter<=length; counter++)


{
cout<<"\t"<<num<<" * "<<counter<<" = "<<num *
counter<<endl;
}

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;

for (counter=10; counter>=1; counter--)


{
cout<<"\t"<<num<<" * "<<counter<<" = "<<num *
counter<<endl;
}

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;

cout<<"Enter a positive number:\n";


cin>>n;

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


{
square = i*i;
sum = sum + square;
}
cout<<"The sum of squares of integers from 1 to "<<n<<"
is \'"<<sum<<"\'\n";

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;

for (i=1; i<=100; i++)


{
if (i%7!=0)
{
cout<<"\t"<<i;
}
else
{
cout<<" \n";
}
}

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;

for (i=0; i<=16; i+=3)


{
cout<<"\t"<<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;

for (i=1; i<=10; i++)


{
if (i%2!=0)
{
sum = sum + i;
}
else
{
product = product * i;
}
}
cout<<"\t Sum of all odd numbers b/w 1-10 \t= \'"<<sum<<"\'\n";
cout<<"\t Product of all even numbers b/w 1-10 \
t= \'"<<product<<"\'\n";

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;

for (student=1; student<=35; student++)


{
cout<<"Enter marks of student "<<student<<endl;
cin>>marks;

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;

cout << "Enter a positive integer: ";


cin >> number;

while (number > 0) {


reverse = reverse * 10 + number % 10;
number /= 10;
}

cout << "Reversed number: " << reverse << endl;

return 0;
}

34. Write a problem to produce the following output:


0
01
014
0149
0 1 4 9 16
0 1 4 9 16 25

#include <iostream>
using namespace std;

int main()
{
int i, j;

for (i=0; i<=5; i++)


{
for (j=0; j<=i; j++)
{
cout<<j*j<<"\t";
}
cout<<"\n";
}

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;

for (i=5; i>=1; i--)


{
for (j=1; j<=i; j++)
{
cout<<"* ";
}

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;

for (counter=1; counter<=n; counter++)


{
factorial = factorial*counter;
cout<<"\tfactorial of "<<counter<<" is = "<<factorial<<"\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;

for (i=1; i<=3; i++)


{
for (j=1; j<=i; j++)
{
cout<<" * ";
}
cout<<"\n";
}

return 0;
}

39. The following program display the following shape using nested for loops.
****
***
**
*

#include <iostream>
using namespace std;

int main()
{
int i, j;

for (i=4; i>=1; i--)


{
for (j=1; j<=i; j++)
{
cout<<" * ";
}
cout<<"\n";
}

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;

for (i=1; i<=4; i++)


{
for (j=1; j<=i; j++)
{
cout<<" * ";
}
cout<<"\n";
}

return 0;
}

You might also like