Lab_Assignment3_solution
Lab_Assignment3_solution
Solution:
// prime number is only divisible by 1 and itself
// composite number can have factors other than 1 and the number itself
#include <iostream>
using namespace std;
int main()
{
int number;
int i;
return 0;
}
2. Write a C++ program to check whether the entered character is a
vowel or consonant.
Solution:
#include <iostream>
using namespace std;
int main()
{
char ch;
switch(ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
cout << ch << " is a vowel." << endl;
break;
default:
cout << ch << " is a consonant." << endl;
}
return 0;
}
3. Write a C++ program that takes a positive integer from the user and
displays the value of the sum of n natural numbers in the series:
1+2+3+....+n.
Solution:
#include <iostream>
using namespace std;
int main()
{
int n;
int sum = 0;
return 0;
}
Solution:
#include <iostream>
using namespace std;
int main()
{
int n = 5;
int factorial = 1.0;
for(int i = 1; i <= n; i++)
{
factorial *= i; // factorial = factorial * i;
}
cout << "Factorial of " << n << " = " << factorial;
return 0;
}
*
***
*****
*******
*********
Solution:
// There are 5 lines in the pyramid. each line has (2*line number - 1) stars.
// We need a total of 3 loops - one outer loop to track the line;
//2 inner loops for spaces and stars to be printed on each line
#include <iostream>
using namespace std;
int main()
{
int line = 5;
int space = 5;
int star = 0;
int i,j,k;
k = 0;
// nested while loop for no of stars in each line
while(k < star)
{
cout << "*";
k++;
}
return 0;
}