Lab Manual # 7 Assignment: - Name: Shaikh Nehal Ahmed - Roll No.:028

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

LAB MANUAL # 7

ASSIGNMENT
• Name: Shaikh Nehal Ahmed
• Roll No.:028
IN-LAB TASKS
Task 1: Write a program that prints the first 50 even numbers by using for loop

STATEMENTS:
OUTPUT:
int main()
{
int x;

for (x = 0; x <= 50; x += 2)


cout << x << endl;
}
Task 2: Write a C++ program which will print first 10 natural numbers by using while loop.

STATEMENT:
int main() OUTPUT:
{
int i;

i = 1;

while(i<=10)
{
cout << i << endl;
i++;
}
}
Task 3: Write a C++ program which will print even numbers from 20 to 1 by using for loop

STATEMENT:
int main() OUTPUT:
{
int i;

for (i = 20; i >= 1; i-=2)


{
cout << i << endl;

}
}
Task 4: Write a C++ program which will take ten numbers from user and for each number display whether the given number is
even or odd

STATEMENT:
int main() OUTPUT:
{
int num,i;

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


{
cout << "Enter any number: ";
cin >> num;

if (num % 2 == 0)
cout << "Given number is even\n";
else
cout << "Given number is odd\n";
}
}
Task 5: Write a C++ program which will print first 10 numbers which are multiple of 5.

STATEMENT:
int main()
{ • OUTPUT:
int x,result;

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


{
result = 5 * x;
cout << "5 x " << x << " = " << result << endl;
}
}
Task 6: Write a C++ program that prints the sum of first 10 natural numbers.

STATEMENT:
int main()
{ • OUTPUT:
int x,add;
add = 0;

cout << "First 10 naturalnumbers\n";


for (x = 1; x <= 10;x++)
{
cout << x << endl;

add=add+x;
}
cout << "Sum of first 10 natural numbers: " << add<<endl;
}
Task 7: Write a C++ program that prints the series 1 2 4 8 16 32 64 128.

STATEMENT:
int main() • OUTPUT:
{
int x;

for (x =1; x <=128; x*=2)


cout << x << endl;

}
POST-LABS
Task 1: Write a C++ program that takes starting and ending value from user and prints only even numbers from start to end
values.
OUTPUT:
STATEMENT:
int main()
{
int start, end;

cout << "enter starting value: ";


cin >> start;
cout << "enter ending value: ";
cin >> end;

if (start % 2 == 1)
{
start++;
}
for (start; start <= end; start += 2)
{
cout << start << endl;
}
}
Task 2: Write a C++ program that takes input ‘N’ from user and print previous 10 odd numbers.
STATEMENT:
int main() OUTPUT:
{
int N, i;

cout << "enter any number: ";


cin >> N;
i = 1;

if (N % 2 == 0)
{
N--;
}

while (i <= 10)


{
N -= 2;
cout << N << endl;
i++;
}
}
Task 3: Write a C++ program which take a number from user and print its factorials by using while loop.

STATEMENT:
int main()
{
int x, y=1, z=1; OUTPUT:
cout << "Enter any number: ";
cin >> x;

while (x >= y)
{
z = z * y;
y++;
}
cout << "The factorial of " << x << " is " << z;
}
Task 4: Write a C++ program which will take two numbers from user, say start and end. Print the numbers from start to end that
are divisible by 3.

STATEMENT:
int main()
{
OUTPUT:
int start, end;

cout << "Enter starting value: ";


cin >> start;
cout << "Enter ending value: ";
cin >> end;

while (start % 3 >= 1)


{
start++;
}

for (start; start <= end; start += 3)


{
cout << start << endl;
}

}
Task 5: Write a C++ program to find whether a given number is odd or even. The program should continue as long as the user
wants. Ask user to press ‘e’ or ‘E’ to exit the program.

STATEMENT:
int main()
{
int x; OUTPUT:
char alp;

do
{
cout << "Enter any number: ";
cin >> x;
if (x % 2 == 0)
{
cout << "Given number is even" << endl;
}
else
{
cout << "Given number is odd" << endl;
}

cout << "Enter e or E to exit: ";


cin >> alp;
}
while (alp != 'e' && alp != 'E');
}
Task 6: Write a program that checks whether a number is prime or not by using if else structure and while loop.

STATEMENT:
int main()
OUTPUT:
{

int i, num;
cout << "enter any number(must be positive): ";
cin >> num;

i = 2;
while (i <= num - 1)
{
if (num % i == 0)
{
cout << "Given value is not a prime number\n";
break;
}
i++;
}
if (num == i)
cout << "Given value is a prime number\n";

You might also like