week 2: print patterns 1.
#include <iostream>
using namespace std;
int main()
{
int i,j;
int n;
cin >>n;
for(i=n;i>=0;i--)
{
for(j=1;j<=i;j++)
{
cout<<((char)(('A')+j-1));
}
cout<<endl;
}
return 0;
}
output:-
5
ABCDE
ABCD
ABC
AB
A
week 2: print patterns 2.
#include<iostream>
using namespace std;
int main()
{
int i,j,num=1;
for(i=0;i<5;i++)
{
for ( j = 0; j <=i; j++)
{
cout<<num<<" ";
num++;
}
cout<<endl;
}
}
output:-
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
week 2: print patterns 2.
#include<iostream>
using namespace std;
int main()
{
int i,j;
int n=5;
for(i=n;i>=0;i--)
{
for(j=0;j<=i;j++)
cout<<"*"<<" ";
cout<<"\n";
}
return 0;
}
output:-
* * * * * *
* * * * *
* * * *
* * *
* *
*
week 2: prime number between two numbers.
// C++ Program to Display Prime Numbers Between Two Intervals
#include <iostream>
using namespace std;
int main(){
int min, max, isPrime;
// Asking for input
cout << "Enter the first number: ";
cin >> min;
cout << "Enter the last number: ";
cin >> max;
// Displaying prime numbers between min and max
cout << "Prime numbers between " << min << " and " << max << " are: " << endl;
for (int i = min + 1; i < max; i++){
isPrime = 0;
for (int j = 2; j < i/2; j++){
if (i % j == 0){
isPrime = 1;
break;
}
}
if (isPrime == 0){
cout << i << endl;
}
}
return 0;
}
week 2: print n number of fibonacci series.
#include <iostream>
using namespace std;
int main() {
int n1=0,n2=1,n3,i,number;
cout<<"Enter the number of elements: ";
cin>>number;
cout<<n1<<" "<<n2<<" "; //printing 0 and 1
for(i=2;i<number;++i) //loop starts from 2 because 0 and 1 are already printed
{
n3=n1+n2;
cout<<n3<<" ";
n1=n2;
n2=n3;
}
return 0;
}
week 2: perfect cube root or not.
#include<iostream>
using namespace std;
int main()
{
int i , n ,cube=0;
cin>>n;
for(i=1;i<=n;i++)
{
cube = i*i*i;
if(cube == n)
{
cout<<n<<" is a perfect cube, cube root is "<<i;
return 0;
}
else if( cube > n)
{
cout<<"no";
return 0;
}
}
}
Comments
Post a Comment