Showing posts with label Loops practice using shapes. Show all posts
Showing posts with label Loops practice using shapes. Show all posts

Monday, 1 May 2017

Calculate the factorial in C++ || C++ Sample Programs

Leave a Comment
Question::

Write a program that asks the user to enter an integer number. The main() program will pass this number to a function called “ fact()”. This function will return the factorial of the input value. Show the calculated factorial of the given number on the screen.

Code:

/**************************************************|
/*************C++ Programs And Projects************|
***************************************************/
#include<iostream>
using namespace std;

int fact(int); // prototype of a function

void main() {
       int number;
       cout << "Enter a Number to find its factorial == ";
       cin >> number;
       cout<<"Factorial of "<<number<<"is == "<<fact(number);
}


//Body of a function

//Logic of factorial is pretty simple that is if we have a number like 5 then the formula of factorial
//will be like this 5*4*3*2*1
int fact(int myFactorial) {
       int result = 1;
       for (int i = 1; i <=myFactorial; i++)
       {
              if (result == 1) {
                     result = result*i;
              }
              else
              {
                     result = result*i;
              }
       }
       return result;
}




Related Articles:

C++ Primer Plus Sixth Edition Complete Solution Manual 

C++ Books Solution

  

Read More

Saturday, 6 February 2016

Write a C++ program to draw isosceles triangle using asterisks

1 comment
Questions:

Write a C++ program to draw isosceles triangle using asterisks

Explanation:

This C++ Program is example of nested loop.Below mention code is compiled in Visual Studio 2015 and output snap is attached.. If any problem you feel and want explanation feel free to contact.

Code:

#include<iostream>
using namespace std;
void main() {
       int value;
       cout << "Enter the size = ";
       cin >> value;
       int space = value;
       for (int i = 0; i < value; i++) {
              for (int j = space; j > 0; j--) {
                     cout << " ";
              }
              for (int star = 0; star <= i * 2; star++) {
                     cout << "*";
              }
              space--;
              cout << endl;
       }

}

Output:


Write a C++ program to draw isosceles triangle using asterisks


Related Articles:

C++ Shapes Codes
Read More

Friday, 7 August 2015

Left Triangle pattern using Capital Alphabet single in on line

Leave a Comment
Questions:

Left Triangle pattern using Capital Alphabet single in on line

Code:

#include<iostream>
using namespace std;
void main()
{

//Left Triangle pattern using Capital Alphabet single in on line
int square,space=0;
cout<<"Enter The Size of Square = "; // size
cin>>square;
space=square;
for(int i=1;i<=square;i++)
{
for(int j=1;j<=space;j++)
{
cout<<" ";
}
for(int l=1;l<=i;l++)
{
cout<<char(l+64);
}
cout<<endl;
space--;
}

}


Output


Read More

Left Triangle pattern using Capital Alphabet single in on line

Leave a Comment
Questions:

Left Triangle pattern using Capital Alphabet single in on line

Code:

#include<iostream>
using namespace std;
void main()
{

//Left Triangle pattern using Capital Alphabet single in on line
int square,space=0;
cout<<"Enter The Size of Square = "; // size
cin>>square;
space=square;
for(int i=1;i<=square;i++)
{
for(int j=1;j<=space;j++)
{
cout<<" ";
}
for(int l=1;l<=i;l++)
{
cout<<char(i+64);
}
cout<<endl;
space--;
}

}


Output


Read More

Left Triangle pattern using Numerical digits

Leave a Comment
Questions:

Left Triangle pattern using Numerical digits

Code:

#include<iostream>
using namespace std;
void main()
{

//Left Triangle pattern using Numerical digits
int square,space=0;
cout<<"Enter The Size of Square = "; // size
cin>>square;
space=square;
for(int i=1;i<=square;i++)
{
for(int j=1;j<=space;j++)
{
cout<<" ";
}
for(int l=1;l<=i;l++)
{
cout<<l;
}
cout<<endl;
space--;
}

}


Output


Read More

Left Triangle pattern using Numerical digits

Leave a Comment
Questions:

Left Triangle pattern using Numerical digits

Code:

#include<iostream>
using namespace std;
void main()
{

//Left Triangle pattern using Numerical digits
int square,space=0;
cout<<"Enter The Size of Square = "; // size
cin>>square;
space=square;
for(int i=1;i<=square;i++)
{
for(int j=1;j<=space;j++)
{
cout<<" ";
}
for(int l=1;l<=i;l++)
{
cout<<i;
}
cout<<endl;
space--;
}

}


Output


Read More

Left Triangle pattern using stars

Leave a Comment
Questions:

Left Triangle pattern using stars

Code:

#include<iostream>
using namespace std;
void main()
{

//Left Triangle pattern using stars
int square,space=0;
cout<<"Enter The Size of Square = "; // size
cin>>square;
space=square;
for(int i=1;i<=square;i++)
{
for(int j=1;j<=space;j++)
{
cout<<" ";
}
for(int l=1;l<=i;l++)
{
cout<<"*";
}
cout<<endl;
space--;
}

}


Output

Read More

Right Triangle pattern using capital alphabets E D C B A

Leave a Comment
Questions:

Right Triangle pattern using capital alphabets E D C B A

Code:

#include<iostream>
using namespace std;
void main()
{
//Right Triangle pattern using capital alphabets E D C B A
int square;
cout<<"Enter The Size of Square = "; // size
cin>>square;
for(int i = 1;i<=square;i++) // outer loop
{
for(int j=square;j>=i;j--) //inner loop
{
cout<<char(j+64)<<" "; // value that is output
}
cout<<endl;//new line for next line output
}

}


Output

Read More

Right Triangle pattern using capital Alphabets

Leave a Comment
Questions:

Right Triangle pattern using capital Alphabets

Code:

#include<iostream>
using namespace std;
void main()
{
//Right Triangle pattern using capital Alphabets
int square;
cout<<"Enter The Size of Square = "; // size
cin>>square;
for(int i = square;i>=1;i--) // outer loop
{
for(int j=1;j<=i;j++) //inner loop
{
cout<<char(i+64)<<" "; // value that is output
}
cout<<endl;//new line for next line output
}

}


Output


Read More

Right Triangle pattern using numerical digits

Leave a Comment
Questions:

Right Triangle pattern using numerical digits
Code:

#include<iostream>
using namespace std;
void main()
{
//Right Triangle pattern using numerical digits
//5 4 3 2 1
//5 4 3 2
//5 4 3
//5 4
//5

int square;
cout<<"Enter The Size of Square = "; // size
cin>>square;
for(int i = 1;i<=square;i++) // outer loop
{
for(int j=square;j>=i;j--) //inner loop
{
cout<<j<<" "; // value that is output
}
cout<<endl;//new line for next line output
}

}


Output


Read More

Right Triangle pattern using numerical digits

Leave a Comment
Questions:

Right Triangle pattern using numerical digits
Code:

#include<iostream>
using namespace std;
void main()
{
//Right Triangle pattern using numerical digits
int square;
cout<<"Enter The Size of Square = "; // size
cin>>square;
for(int i = square;i>=1;i--) // outer loop
{
for(int j=1;j<=i;j++) //inner loop
{
cout<<i<<" "; // value that is output
}
cout<<endl;//new line for next line output
}

}


Output

Read More

Right Triangle pattern using Capital alphabets in reverse triangle order

Leave a Comment
Questions:

Right Triangle pattern using Capital alphabets in reverse triangle order
Code:

#include<iostream>
using namespace std;
void main()
{
//Right Triangle pattern using Capital alphabets in reverse triangle order
int square;
cout<<"Enter The Size of Square = "; // size
cin>>square;
for(int i = square;i>=1;i--) // outer loop
{
for(int j=1;j<=i;j++) //inner loop
{
cout<<char(j+64)<<" "; // value that is output
}
cout<<endl;//new line for next line output
}

}


Output


Read More