You are on page 1of 4

LAB REPORT#6

Junaid Ahmed
BSEE-11807-2021
Sec-A
Dated:24-11-2021

OBJECTIVES:
1. To recall previous knowledge of c++ and apply it in solving problems
2. Algorithm forming
3. Practicing your skills
4. Getting to know new libraries
5. Using loops efficiently
Task#1:
Write a program that prints a diamond where the length of diamond is decided by
the user.
Code:
#include <iostream> cout << endl;
using namespace std; }
int main() for (i = r - 1; i >= 1; --i)
{ {
int r,i,j,s,z,num; s = r - i; //number of spaces
cin >> r; num = (2 * (i - 1) + 1); //number of
for (i = 1; i <= r; ++i) asterisks
{ for (z = 1; z <= s; z++) //for spaces
s = r - i; //number of spaces {
num = (2 * (i - 1) + 1); //number of cout << " ";
asterisks }
for (z = 1; z <= s; z++) //for spaces for (j = 1; j <= num; ++j) //for asterisks
{ {
cout << " "; cout << "*";
} }
for (j = 1; j <= num; ++j) //for asterisks cout << endl;
{ }
cout << "*"; return 0;
} }
Description:
I declared int type variables r,I,j,s,z,num as follow:
i = number of row num=number of asterisks s= number of spaces r=number of
rows entered by user
I used formula s=r-I and num=2*(i-1)+1. When for example r=3, then I run a loop for(int
i) to print rows and at each row I print num number of asterisks using another for loop
for(int j). To print spaces at each row, I used another for loop for (int j) inside for(int i).
This allows me to print half of diamond. So to print a full dimond I used a backward loop
starting from i=r-1.

Result:

Task#2:

Write a program that prints first n prime numbers where n is input by the user.
Code:
#include <iostream>
using namespace std;
int main()
{
int n, i = 3, counter, num;
cout << "Enter the number of desired prime numbers\n";
cin >> n;
if(n >= 1)
{
cout << "First " << n << " prime numbers are : \n";
cout << "2 ";
}
for (counter = 2; counter <= n; i++) {
for (num = 2; num < i; num++) {
if(i % num == 0)
break;
}
if(num == i)
{
cout << i << " ";
counter++;
}
}
return 0;
}
Description:
I declared a variable n to store number of desired prime number enter by user. Next I
make an if statement that n should be greater than or equal to one. Next I use a nested
for loop. First loop is run n times and second loop checks for prime number. When
i=num, then we break out of loop using break statement because such number cannot
be prime whose remainder by 2 is zero. So one by one we print prime numbers until
counter = n.

Result:

Task#3
Write a program that checks whether a 5 or 7 digit number is palindrome or not
Code:
#include<iostream>
using namespace std;
int main()
{
int i, length, pflag = 0;
char arr[100];
cin >> arr;
length = strlen(arr);
for (i = 0; i < length/2; i++)
{
if (arr[i] != arr[length - i - 1])
pflag = 1;
}
if (pflag == 1)
cout << "Not Palindrome";
else
cout << "Palindrome";
return 0;}
Description:
I declared a char type array of size 100 and input any number from user. Say input is
12321. Then using strlen(arr) function, I found the length of array and stored it in int
length and so now I know the last index position which is length-1. Next I run a loop to
check two characters on first (i=0) and last index (i=length-1) position and then during
next loop run I move from i=0 to i=1 and from length -1 to length -1-1 i.e. length-2.
Hence I keep it checking until i < length. If at any hydration arr[i] is not equal to
arr[length - i - 1], then I assign value of 1 to an int type variable pflag whose initial value
of 0. At the end, if pflag changes to 1, then I printed “Not Palindrome” using if
statement, else I print “Palindrome”.

Result:

You might also like