.1. Print the first 7 odd numbers in descending order using for loop
2. Print the product of first 5 natural numbers using for loop
3. Print your name 10 times in different lines using while loop
4. Print the square of first 10 even numbers in descending order using while loop
5. Print the cube of first 8 multiples of 6 in descending order using do-while loop.
6. Print the twice of first 12 natural number in ascending order using do-while
loop.
PART II [3x5=15 marks]
Answers
- #include<stdio.h>
int main() {
int n, i, a;
printf("Enter a number:");
scanf("%d", &n);
printf("List in descending order:");
for (i = n - 1; i >= 0; i--) {
a = 1 + i * 2;
printf("%d ", a);
}
return 0;
}
2.// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function that returns true if n is prime
bool isPrime(int n)
{
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
// This is checked so that we can skip
// middle five numbers in below loop
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
// Function that return true if the product
// of the first n natural numbers is divisible
// by the sum of first n natural numbers
bool isDivisible(int n)
{
if (isPrime(n + 1))
return false;
return true;
}
// Driver code
int main()
{
int n = 6;
if (isDivisible(n))
cout << "Yes";
else
cout << "No";
return 0;
3.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a=1;
while(a<=10)
{
a++;
cout<<"Sahaj"<<endl;
}
getch();
}
Answer:
1. 11 9 7 5 3 1
2. 6 5 3 2 1