Tuesday, 27 December 2016
Thursday, 22 December 2016
Mohammad Tayyab
16:33
Statement:
C++ program to take log^n
Code :
#include<iostream>
using namespace std;
void main()
{
int i, n, s = -1;
float sum = 0;
cout << "Enter The Value Of n : ";
cin >> n;
for (i = 1; i <= n; i++)
{
s = s * -1;
sum += s*1.0 / i;
}
cout << "Log Of 2 : "
<< sum;
cout << "\n\n";
system("pause");
Mohammad Tayyab
16:30
Statement :
C++ Program to make a simple calculator having all basic functions (+,-,*, /, %,^,!)
Code :
C++ Program to make a simple calculator having all basic functions (+,-,*, /, %,^,!)
Code :
//
C++ Program to make a simple calculator having all basic functions (+,-,*, /,
%,^,!)
#include<iostream>
using namespace std;
void main()
{
cout << "\t\t\tCalculator\n\n";
int a, b;
char op;
float res=1;
do
{
cout << "Enter Operational operator (+,-,*, /, %,^,!) :
";
cin >> op;
if (op == '!')
{
cout << "Enter Number Of Which You Want To Take Factorial :
";
cin >> a;
while (a > 0)
{
res = res*a;
a--;
}
}
else if (op == '-')
{
cout << "Enter First Number : ";
cin >> a;
cout << "Enter Second Number : ";
cin >> b;
if (a > b)
{
res = a - b;
}
else
{
res = b - a;
}
}
else if (op == '/')
{
cout << "Enter First Number : ";
cin >> a;
cout << "Enter Second Number : ";
cin >> b;
res = a / b;
}
else if (op == '*')
{
cout << "Enter First Number : ";
cin >> a;
cout << "Enter Second Number : ";
cin >> b;
res = a * b;
}
else if (op == '+')
{
cout << "Enter First Number : ";
cin >> a;
cout << "Enter Second Number : ";
cin >> b;
res = a + b;
}
else if (op == '%')
{
cout << "Enter First Number : ";
cin >> a;
cout << "Enter Second Number : ";
cin >> b;
res = a % b;
}
else if (op == '^')
{
cout << "Enter Number : ";
cin >> a;
cout << "Enter Power : ";
cin >> b;
res = pow(a, b);
}
else
{
cout << "Please Rnter A Valid Operator.";
res = 0;
}
cout << "\n\n\nResult : "
<< res;
cout << "\n\nPress Q For Quit The Program OR Press Any Key To
Run Again.";
cin >> op;
} while (op != 'Q' ||op != 'q');
cout << "\n\n";
system("pause");
Mohammad Tayyab
16:29
Statement :
Program to display number of vowels, consonants, digits and white spaces in a string.
Code :
Program to display number of vowels, consonants, digits and white spaces in a string.
Code :
//
Program to display number of vowels, consonants, digits and white spaces in a
string.
#include<iostream>
#include<string>
using namespace std;
void main()
{
cout << "\tDisplay Number Of
Vowels, Consonants, Digits And Spaces In A String\n\n";
string str;
int v=0, c=0, d=0, s=0;
cout
<< "Enter A Line Of String : ";
getline(cin, str);
for (int i = 0; i!='\0'; i++)
{
if (str[i] == 'A' || str[i] == 'a' || str[i] == 'E' || str[i] == 'e' || str[i] == 'I' || str[i] == 'i' || str[i] == 'O' || str[i] == 'o' || str[i] == 'U' || str[i] == 'u')
{
v++;
}
else if ((str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= 'a' && str[i] <= 'z'))
{
c++;
}
else if (str[i] == ' ')
{
s++;
}
else if (str[i] >= '0' && str[i] <= '9')
{
d++;
}
else
{
//other cahracters
}
}
cout << "Number Of Spaces : " << s << endl;
cout << "Number Of Vowels : " << v << endl;
cout << "Number Of Consonant : " << c << endl;
cout << "Number Of Digits : " << d << endl;
cout << "\n\n";
system("pause");
Mohammad Tayyab
16:26
Statement:
C++ Program to display pyramid of digits
Code :
C++ Program to display pyramid of digits
Code :
//C++
Program to display pyramid of digits. and rows entered by user
#include<iostream>
using namespace std;
int main()
{
cout << "\t\t\tPyramid Of digits\n\n";
int num,spc,i, j, k, l;
k = 0; l = 0;
cout<<"Enter the number of rows: ";
cin >> num;
for (i = 1; i <= num; i++) //loop for control user entered number and newline
{
for (spc = 1; spc <= num - i;
spc++) //spc is a variable for spaces and this
loop is to control spaces
{
cout << " ";
k++;
}
for (j=0; j != 2 * i - 1;j++) //this loop will print numbers by condtion
{
if (k <= num - 1) // this condtion will print numbers brfore maximum
interval
{
cout<<i
+ j;
k++;
}
else //print numbers after maximum number
{
l++;
cout<<(i
+ j - 2 * l);
}
}
l = 0; k = 0; j = 0; //updating vluse of i j and k before next printing
cout<<"\n"; //add newline after once line print
}
cout << "\n\n";
system("pause");
return 0;
}
Subscribe to:
Posts (Atom)