PF Assignment
PF Assignment
04
Submitted by:
Ahmad Sameet Bin Manzar
Roll No:
SU92-BIOTM-S24-015
PROGRAMMING FUNDAMENTALS
Submitted to:
Sir Nadeem Sarfraz
MAY 7, 2024
1. Write a C++ program that uses a for loop to print the squares of the first 10 positive integers.
#include <iostream>
using namespace std;
int main()
{
int a;
cout<<"quare of the first 10 positvie inttegers: "<<endl;
for(a=1; a <= 10 ; ++a)
{
cout<<" saquare of " <<a<< " is "<<a*a<<endl;
}
return 0;
}
2. Create a C++ program that calculates the average of a set of numbers entered by the user. The
program should first ask the user how many numbers will be entered, then use a for loop to input
those numbers and calculate their average.
#include <iostream>
using namespace std;
int main()
{
int numbercount;
double sum =0.0;
cout<<"enter the number of value ";
cin>>numbercount;
for(int i=0; i<numbercount;++i)
{
double num;
cout<<"enter number " <<i+1<< ": ";
cin>>num;
sum+=num;
}
double average =sum/ numbercount;
cout<<"the average of the entered number is : "<<average<<endl;
return 0;
}
3. Write a program to display the multiplication table up to 10 for a given number.
#include<iostream>
using namespace std;
int main ()
{
int number;
cout<<"enter number : "<<endl;
cin>>number;
for(int i=1;i<=10;++i)
{cout<<number<< " * "<< i <<" = " << (number*i) <<endl;
}
return 0;
}
4. Write a program that Calculate the factorial of a given number N.
#include<iostream>
using namespace std;
int main()
{
int num;
int factorial=1;
{ factorial *=i ;
}
cout<<"factorial of = " << num<< " is " <<factorial <<endl;
return 0;
5. Write a C++ program to determine whether a given number is a prime number or not.
#include<iostream>
int main()
int num;
char press;
do{
cin>>num;
if(num<=1){
isprime =false;
return 0;
}else{
for(int a=2;a*a<=num;++a){
if(num%a==0)
isprime=false;
break;
if(isprime){
cout<<num<<" is prime"<<endl;
}else{
cout<<"press Y TO DO IT AGAIN"<<endl;
cin>>press;
} while(press=='y'||press=='Y');
return 0;
6. Write a program that counts the number of even numbers between 1 and
100.
#include<iostream>
using namespace std;
int main()
{
int cnt=0;
for(int i=1;i<=100; i+=2)
{
cnt ++;
cout<<" = "<<i<<endl;
}
cout<<"the even number between 1 to 100 is : " <<cnt;
return 0;
}
7. Write a program that prompts the user to input an integer and then
outputs both the individual digits of the number and the sum of the
digits. For example, it should output the individual digits of 3456 as 3 4 5
6, output the individual digits of 8030 as 8 0 3 0, output the individual
digits of 2345526 as 2 3 4 5 5 2 6, output the individual digits of 4000 as 4
0 0 0, and output the individual digits of -2345 as 2 3 4 5.
#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"enter number : ";
cin>>num;
if(num<0){
num=-num;
}
int digit;
int sum=0;
cout<<"indiv digit";
while(num>0){
digit=num%10;
cout<<digit<< " ";
sum+=digit;
num/=10;
}
cout<< "sum of digit "<<sum;
return 0;
8. Write a program that prompts the user to enter two integers. The program outputs how many numbers
are multiples of 3 and how many numbers are multiples of 5 between the two integers.
#include<iostream>
int main()
{ int num1,num2;
int count5=0;
int count3=0;
cin>>num1;
cout<<" enter number ";
cin>>num2;
if(a%3==0)
count3++;
if(a%5==0)
count5++;
return 0;
9.Write a program that prompts the user to input a positive integer. It should
then output a message indicating whether the number is a prime number.
(Note: An even number is prime if it is 2. An odd integer is prime if it is not
divisible by any odd integer less than or equal to the square root of the
number.)
#include<iostream>
#include<cmath>
int main()
int number;
cin>>number;
if(number<=1){
return 0;
}else if(number==2){
else {
if (number%num==0){
return 0;
return 0;
10. Write a program that uses while loops to perform the following steps: a. Prompt the user to input two
integers: firstNum and secondNum (firstNum must be less than secondNum). b. Output all odd numbers
between firstNum and secondNum. c. Output the sum of all even numbers between firstNum and
secondNum.
#include<iostream>
int main()
int firstnum,secondnum;
cin>>firstnum;
cin>>secondnum;
if(firstnum>=secondnum){
return 1;
int current;
int sumeven=0;
current =firstnum+(!(firstnum%2));
while (current<=secondnum){
current+=2;
while(current<=secondnum){
sumeven+=current;
current+=2;
cout<< sumeven<<endl;
return 0;
#include<iostream>
int main()
int num1,num2;
int count3=0;
int count5=0;
char press;
cout<<"enter number";
cin>>num1;
cout<<"enter number";
cin>>num2;
for(int a=num1;a<=num2;++a){
if(a%3==0)
count3++;
if(a%5==0)
count5++;
return 0;
#include<iostream>
int main()
int num1,num2;
int count3=0;
int count5=0;
char press;
do{
cout<<"enter number";
cin>>num1;
cout<<"enter number";
cin>>num2;
for(int a=num1;a<=num2;++a){
if(a%3==0)
count3++;
if(a%5==0)
count5++;
cout<<"press Y to continue";
cin>>press;
}while(press='y'||press=='Y');
return 0;
13. Write a program that print Fibonacci sequence .The Fibonacci sequence is a series where the next
term is the sum of the previous two terms.
#include<iostream>
int main()
int num;
cin>>num;
int nextnum;
cout<<"fibonocci series";
for(int a=1; a<= num;++a){
cout<<first<<" ";
nextnum=first+second;
first=second;
second = nextnum;
return 0;
14. Write a program to print the multiplication table of the number entered by the user. The table should
get displayed in the following form. 29 * 1 = 29 29 * 2 = 58
#include<iostream>
int main ()
int num;
cin>>num;
return 0;
#include <iostream>
int main() {
int n;
cout << "Enter the number of rows: ";
cin >> n;
return 0;