1. A prime number is an integer greater than one and divisible only by itself and one.
The first seven prime numbers are 2, 3, 5, 7, 11, 13, and 17. Write a program that displays all the
prime numbers between 1 and 100
2. Write another program that accepts a number from the user and returns th Fibonacci value of that
number. You should use recursion in here.
3. Write a program that accepts a number and determine whether the number is prime or not.
4. Write a C++ code that computes the sum of the following series.
Sum = 1! + 2! + 3! + 4! + …n!
The program should accept the number from the user.
5. Write a C++ code to display only even numbers found between 222 and 180.
6. Write and run a program that reads a positive integer n and then prints a diamond of numbers Use for loop.
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int IsPrime(int n)
{
int i, FlagPrime = 1;
if (n == 0 || n == 1) FlagPrime=0;
else
{
for(i = 2; i <= n/2; ++i)
{
if(n % i == 0) {FlagPrime = 0; break;}
}
}
return (FlagPrime);
}
// calculating factorial of given number n
int fact(int n)
{
int k = 1;
for (int i = 2; i <= n; i++) k *= i;
return k;
}
int main()
{
/*
1. A prime number is an integer greater than one and divisible only by itself and one.
The first seven prime numbers are 2, 3, 5, 7, 11, 13, and 17. Write a program that displays all the
prime numbers between 1 and 100
*/
int Sum=0;
int n=0,m=1,a,b,c,i,j,s;
cout<<"\nSolution-1";
while(n<7)
{
if(IsPrime(m) ==1) {cout<<"\nPrime No. "<<n+1<<" = "<<m; n++;}
m++;
}
/*
2. Write another program that accepts a number from the user and returns th Fibonacci value of that
number. You should use recursion in here.
*/
cout<<"\nSolution-2";
cout<<"\n\nEnter a number: "; cin>>n;
a=0; b=1; m=2;
cout<<"\nFibonacci Term-1 = "<<a;
cout<<"\nFibonacci Term-2 = "<<b;
for(m=2;m<=n;m++)
{
c=a+b;
cout<<"\nFibonacci Term-"<<m+2<<" = "<<c;
a=b;
b=c;
}
/*
3. Write a program that accepts a number and determine whether the number is prime or not.
*/
cout<<"\nSolution-3";
cout<<"\n\nEnter a number: "; cin>>n;
if(IsPrime(m) ==1) cout<<"\nThe number "<<n<<" is a PRIME number.";
else cout<<"\nThe number "<<n<<" is a NOT a PRIME number.";
/*
4. Write a C++ code that computes the sum of the following series.
Sum = 1! + 2! + 3! + 4! + …n!
The program should accept the number from the user.
*/
cout<<"\nSolution-4";
cout<<"\n\nEnter a number: "; cin>>n;
cout<<"Sum = ";
for(m=1;m<=n;m++)
{
Sum = Sum + fact(m);
cout<<"!"<<m<<"+";
}
cout<<" = "<<Sum;
/*
5. Write a C++ code to display only even numbers found between 222 and 180.
*/
cout<<"\nSolution-5";
cout<<"\nEven Numbers between 222 and 180: ";
for(n=180;n<=222;n++)
{
if(n%2==0) cout<<n<<", ";
}
/*
6. Write and run a program that reads a positive integer n and then prints a diamond of numbers Use for loop.
*/
cout<<"\nSolution-6";
cout<<"\n\nEnter a number: "; cin>>n;
s = n-1;
for(a=1; a<=n; a++)
{
for(b=1; b<=s; b++) cout<<" ";
s--;
for(b=1; b<=(2*a-1); b++) cout<<"*";
cout<<endl;
}
s = 1;
for(a=1; a<=(n-1); a++)
{
for(b=1; b<=s; b++) cout<<" ";
s++;
for(b=1; b<=(2*(n-a)-1); b
++) cout<<"*";
cout<<endl;
}
cout<<endl;
}
Comments
thank you so much assignmentexpert community you solved my assignment problem!!!!
Leave a comment