Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
9 views

PF Assignment

Uploaded by

myr7hcxt5g
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

PF Assignment

Uploaded by

myr7hcxt5g
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

ASSIGNMENT NO.

04

Submitted by:
Ahmad Sameet Bin Manzar

Roll No:
SU92-BIOTM-S24-015

PROGRAMMING FUNDAMENTALS

THE SUPERIOR UNIVERSITY, LAHORE


Department Of Information & Technology
Section: BSIOT-1A

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;

cout<<"enter number for factorial : "<<endl;


cin>>num;

for(int i= 1 ; i <=num; ++i )

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

using namespace std;

int main()

int num;

char press;

do{

bool isprime = true ;

cout<<"enter number : ";

cin>>num;
if(num<=1){

cout<<num<<" nmber is not prime ";

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<<num<<" is not prime"<<endl;

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>

using namespace std;

int main()

{ int num1,num2;

int count5=0;

int count3=0;

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<<"multiples of "<<num1<<" and "<<num1<<" is "<<count3<<endl;

cout<<"multiples of "<<num2<<" and "<<num2<<" is "<<count5<<endl;

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>

using namespace std;

int main()

int number;

cout<<"enter positive number : ";

cin>>number;

if(number<=1){

cout<<number<<" is not prime ";

return 0;

}else if(number==2){

cout<<number<<" number is prime";


return 0;

else {

for(int num=3;num<=sqrt(number);num +=2){

if (number%num==0){

cout<<number<<" is no0t a prime";

return 0;

cout<<number <<" bumber is a prime number ";

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>

using namespace std;

int main()

int firstnum,secondnum;

cout<<"enter first number : ";

cin>>firstnum;

cout<<"enter second number greater then first : ";

cin>>secondnum;
if(firstnum>=secondnum){

cout<<"error enter second number greater then first "<<endl;

return 1;

int current;

int sumeven=0;

cout<<"odd number between : "<<firstnum<<" o r " <<secondnum<<" are "<<endl ;

current =firstnum+(!(firstnum%2));

while (current<=secondnum){

cout<<current<< " "<<endl;

current+=2;

cout<<"sum of even number between"<<firstnum<< " o r "<<secondnum<< " is "<<endl;

current = firstnum + (!(firstnum % 2));

while(current<=secondnum){

sumeven+=current;

current+=2;

cout<< sumeven<<endl;

return 0;

11. Redo Programming Exercise 8 using for loops.

#include<iostream>

using namespace std;

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

cout<<"multiples of 3 between "<<num1<<" and "<<num2<<" is "<<count3<<endl;

cout<<"multiples of 5 between "<<num1<<" and "<<num2<<" is "<<count5<<endl;

return 0;

12. Redo Programming Exercise 8 using do...while loops.

#include<iostream>

using namespace std;

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<<"multiples of 3 between "<<num1<<" and "<<num2<<" is "<<count3<<endl;

cout<<"multiples of 5 between "<<num1<<" and "<<num2<<" is "<<count5<<endl;

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>

using namespace std;

int main()

int num;

cout<<"enter number : ";

cin>>num;

int first =0;

int second =1;

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>

using namespace std;

int main ()

int num;

cout<<"enter number : ";

cin>>num;

for(int a=1;a<=10; ++a){

cout<<num<<" * "<<a<<" = "<<num*a<<endl;

return 0;

15. Write a program to produce the following output:

#include <iostream>

using namespace std;

int main() {

int n;
cout << "Enter the number of rows: ";

cin >> n;

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

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

cout << " ";

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

cout << j << " ";

for (int j = i - 1; j >= 1; --j) {

cout << j << " ";

cout << endl;

for (int i = n - 1; i >= 1; --i) {

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

cout << " ";

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

cout << j << " ";

for (int j = i - 1; j >= 1; --j) {


cout << j << " ";

cout << endl;

return 0;

16. Write a program to produce the following output:

You might also like