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

CLASS ACTIVITY 1

Uploaded by

24-cs-193
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)
2 views

CLASS ACTIVITY 1

Uploaded by

24-cs-193
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/ 8

CLASS ACTIVITY

Programming Fundamentals

Submitted By:

Chaudhry Mehroz

Reg. No: 24-CS-193

Section: A

Submitted to:

Ma’am Veena Dilshad

Dated:

15-10-2024

Department of Computer Science,

HITEC University, Taxila


STATEMENTS:
1. Write a program to print the multiplication table of a given number
using a for loop. The table should go from 1 to 10.
Solution:
 Input:
#include <iostream>
using namespace std;

int main() {
int number;

cout << "Enter a number to print its multiplication


table: ";
cin >> number;

cout << "Multiplication Table for " << number << ":"
<< endl;
for (int i = 1; i <= 10; i++) {
cout << number << " x " << i << " = " << number *
i << endl;
}

return 0;
}
 Output:
Enter a number to print its multiplication table: 7
Multiplication Table for 7:
7x1=7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
2.Write a program that prints all prime numbers between
1 and 50 using a for loop.
Solution:
 Input:
#include <iostream>
using namespace std;

bool isPrime(int num) {


if (num <= 1) return false;
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
int main() {
cout << "Prime numbers between 1 and 50:" << endl;

for (int num = 1; num <= 50; num++) {


if (isPrime(num)) {
cout << num << " ";
}
}

cout << endl;


return 0;
}
 Output:
Prime numbers between 1 and 50:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47

3. Write a program to calculate the average of n numbers


input by the user using a for loop.
Solution:
 Input:
#include <iostream>
using namespace std;

int main() {
int n;
float sum = 0.0;

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


cin >> n;

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


float number;
cout << "Enter number " << i << ": ";
cin >> number;
sum += number;
}

float average = sum / n;

cout << "The average of the entered numbers is: " <<
average << endl;

return 0;
}
 Output:
Enter the number of elements: 3
Enter number 1: 5
Enter number 2: 10
Enter number 3: 25
The average of the entered numbers is: 13.3333

You might also like