Printing "Diamond " with full commenting

10:06 0 Comments A+ a-

Problem:
                       Print Diamond on Console
Solution:
# include <iostream>
using namespace std;
int main()
{
int s, p, n; //s is used for spacing, p is used for printing *, n is taken as integer for size of diamond
cout << "Enter the size of Diamond (must be odd and >=5) : " ;
cin >> n;

while (n % 2 == 0 || n<5)
{
cout << "The size n should be odd number (& >=5)" << endl;
cout << "Enter the size of Diamond again (must be odd and >=5) : ";
cin >> n;
}
s = n / 2; //s is taken n/2 as space for the first line starric is n/2
p = 1; // p is taken 1 as only 1 * is to be printed in 1st line

for (int i = 0; i<n / 2 + 1; i++) //loop for the upper half of diamond including middle line
{
for (int a = 0; a<s; a++) //loop for spacing in upper half
{
cout << " ";
}
s--; //1 is subtracted from s each time to reduce spacing on left side of pattern by 1
for (int b = 0; b<p; b++) //loop for printing *
{
cout << "*";
}
p += 2; //2 is added to p each time as in upper half the * are to increase 2 by each time until n
cout << endl;
}
s = 1; //s is again initalized 1 as only one space appear in the line below the central line 
p = n - 2; //2 is deducted from p each time to reduce the number of starric by 2 each time until 1
for (int j = 0; j<n / 2; j++) // loop for lower half of diamond excluding the central line
{
for (int c = 0; c<s; c++) //loop for spacing in the lower half
{
cout << " ";
}
s++; //1 is added to spacing each time as the space increases each time by 1 on left side in the lower half
for (int d = 0; d<p; d++) //loop for printing *
{
cout << "*";
}
cout << endl;
p = p - 2; //2 is subtracted from p each time to reduce number of starric * each time by 2
}


system("pause");
return 0;
}

Printing Rectangle

09:48 0 Comments A+ a-

Problem:
                     Printing Rectangle on Console
Solution:
# include <iostream>
# include <iomanip>
using namespace std;
int main()
{
int n,a;
cout<<"Enter the size of rectangle : ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"-";
}
cout<<endl;
a=n-2;
for(int j=0;j<a;j++)
{
cout<<"|"<<setw(n-1)<<"|"<<endl;
}
for(int k=0;k<n;k++)
{
cout<<"-";
}
cout<<endl;

return 0;

}

Printing Inverted "K"

09:33 0 Comments A+ a-

Problem:
                   Print inverted"K" on the console.
Solution:
# include <iostream>
# include <iomanip>
using namespace std;
int main()
{
int n,l,s,t;
cin>>n;
l=n/2;
int a=1;
s=n/2;
t=1;

for(int i=0;i<l;i++)
{
cout<<setw(a)<<"*";
cout<<setw(s)<<"*";
cout<<endl;
a++;
s--;
}
cout<<setw((n/2)+1)<<"*"<<endl;
int b=n/2;
for(int i=0;i<l;i++)
{
cout<<setw(b)<<"*";
cout<<setw(t)<<"*";
cout<<endl;
b--;
t++;
}

return 0;

}

Printing "K"

09:30 0 Comments A+ a-

Problem:
                   Print "K" on the console.
Solution:
# include <iostream>
# include <iomanip>
using namespace std;
int main()
{
int s1, n, n1;
cin >> n;
n1 = n;
s1 = 0;
for (int i = 0; i < (n / 2); i++)
{
while (i < n / 2)
{
cout <<"*"<< setw(n1 - 1) << "*";
n1 = n1 - 2;
i++;
cout << endl;
}

}
for (int i = 0; i < (n / 2); i++)
{

while (i < n / 2)
{
cout <<"*"<< setw(n1 +1) << "*";
n1 = n1 +2;
i++;
cout << endl;
}

}


return 0;

}

Print "Greater Than" (>) Pattern

09:26 0 Comments A+ a-

Problem:
                    Print the Greater Than sign on console.
Solution:
# include <iostream>
# include <iomanip>
using namespace std;
int main()
{
int n,l;
cin>>n;
l=n/2;
int a=1;
for(int i=0;i<l;i++)
{
cout<<setw(a)<<"*";
cout<<endl;
a++;
}
cout<<setw((n/2)+1)<<"*"<<endl;
int b=n/2;
for(int i=0;i<l;i++)
{
cout<<setw(b)<<"*";
cout<<endl;
b--;
}



return 0;

}

Print CROSS Pattern (with full commenting)

09:21 0 Comments A+ a-

Problem:
                     Print the pattern of a cross on console.
Solution:
# include <iostream>
# include <iomanip>
using namespace std;
int main()
{
int n, l, s, t; //n is integer taken, l is used to print upper and lower half, s is used to set the value of setw of upper half right *, t is also used to set the value of setw of lower half right *
cout << "Please enter the value of size of Cross (must be odd and >=3)";
cin >> n;
while (n%2==0 || n<3)
{
cout << "Invalid input for Size of pattern of Cross" << endl;
cout << "Please enter the value of size of Cross again (must be odd and >=3)";
cin >> n;
}
l = n / 2; //l is n/2 as i want to print upper and lower half and the middle line separately
int a = 1; //a is taken to set the value of setw on the left hand side *
s = n - 1; //s is taken 1 less than n because 1 * is already printed
t = 2; //t is taken 2 as because after the middle line the * are two spaces away from each other.

for (int i = 0; i<l; i++) //for loop to control upper half rows i.e n/2 excluding middle line
{
cout << setw(a) << "*"; //setw is taken 1 as the upper left starric is printed on the full left, and then 1 is added in "a" to diplace * by 1 place after
cout << setw(s) << "*"; //
cout << endl;
a++;
s = s - 2; //2 is subtracted from s each time this loop runs as the upper left * moves right by 1 value and right * moves left by 1 each time
}
cout << setw((n / 2) + 1) << "*" << endl; //to display center row
int b = n / 2; //b is taken n/2 as the * lower left to centre row is on setw(n/2)
for (int i = 0; i<l; i++) //for loop to control lower half rows i.e n/2 excluding middle line
{
cout << setw(b) << "*";
cout << setw(t) << "*";
cout << endl;
b--; //1 is subtracted from b each time as the the lower left * moves left in each turn
t += 2; //2 is added in t as the lower right and left * moves away by value 1 each so 1+1=2
}




system("pause");
return 0;
}

Display Following Pattern

09:19 0 Comments A+ a-

Problem:
Write a program that takes size from user a displays the following pattern:
Sample Run 1:
Enter Size: 3
Output:
1 2 3
1 2
1
Solution:
# include <iostream>
using namespace std;
int main()
{
int size;
int j;
int temp;
cout<<"Please enter the size of pattern : ";
cin>>size;
temp=size;
for(int i=0;i<size;i++)
{
j=1;
while (j<=temp)
{
cout<<j<<" ";
j++;
}
cout<<endl;
temp--;

}


return 0;

}

Display Following Pattern

09:16 0 Comments A+ a-

Problem:
Write a program that takes size from user a displays the following pattern:
Sample Run 1:
Enter Size: 3
Output:
1
1 2
1 2 3
Solution:
# include <iostream>
using namespace std;
int main()
{
int size;
int j;
int b=1;
cout<<"Please enter the size of pattern : ";
cin>>size;
for(int i=0;i<size;i++)
{
j=1;
while (j<=b)
{
cout<<j<<" ";
j++;
}
cout<<endl;
b++;

}


   return 0;

}

Printing a Matrix of n*n

09:14 0 Comments A+ a-

Problem:
Write a program that takes size "n" from user and displays n x n matrix of stars.
Sample Run 1:
Enter Size: 3
Matrix:
***
***

***
Solution:
# include <iostream>
using namespace std;
int main()
{
int num; //num represents size of pattern
cout<<"Please enter the integer";
cin>>num;
for(int i=0;i<num;i++)
{
for(int j=0;j<num;j++)
{
cout<<"*";
}
cout<<endl;
}



return 0;

}

Separate and Count The Odd and Even Integers in a entered Integer

09:08 0 Comments A+ a-

Problem:
Write a program that take an integer number from user and print whether every digit of that
number is odd or even. 

For example if user enter the number 5784696 then output should be as:
Odd numbers: 5 7 9 
Even numbers: 8466 
Total odd number: 3 
Total even number: 4 

Note: use increment operator to count the total odd and total even number. 
Hint: You can pull apart a number into its digits using / 10 and % 10

Solution:
# include <iostream>
using namespace std;
int main()
{
int num;
int num1;
int Odd=0;
int Even=0;
int r1=0;
int r2=0;
int x,y;
cout<<"Enter the Number"<<endl;
cin>>num;
while(num!=0){
num1=num%10;
if(num1%2==0){
cout<<num1<<"is even"<<endl;
Even++;
r1=(r1*10)+num1;
}
else{
cout<<num1<<"is odd"<<endl;
Odd++;
r2=(r2*10)+num1;
}
num=num/10;
}


cout<<"The Number of Even Digits are:"<<Even<<endl;
cout<<"The Number of Odd Digits are:"<<Odd<<endl;
x=r1;
y=r2;
cout<<"The Even Numbers are:";
while(x>=1)
{ r1=x%10;
cout<<r1<<" ";
x=x/10;
}
cout<<endl;
cout<<"The Odd Numbers are:";
while(y>=1)
{ r2=y%10;
cout<<r2<<" ";
y=y/10;
}
cout<<endl;
return 0;

}

Check whether the number is Even or Odd

08:47 0 Comments A+ a-

Problem:
Write a program that should take several integer numbers from the user, for each number it should check whether it is odd or even and display “Even” or “Odd” accordingly. Your program should keep taking numbers and displaying messages unless user presses 0.
Solution:
# include <iostream> using namespace std; int main() { int num; cout<<"Enter the Number"<<endl; cin>>num; while(num!=0){ if(num%2==0) cout<<"Number entered is even"<<endl; else cout<<"Number entered is odd"<<endl; cout<<"Enter the Number Again"<<endl; cin>>num; } return 0; }

Sum of as many numbers as you Require

08:39 0 Comments A+ a-

Problem:
Write a program that calculates the sum of real numbers. Your program should keep taking values and adding them unless user presses -999. Your program should display the sum before the program finishes.
Solution:
# include <iostream> using namespace std; int main() { double num; double sum=0; cout<<"Enter the number"<<endl; cin>>num; while(num!=-999){ sum+=num; cout<<"Enter the number again"<<endl; cin>>num; } cout<<"The sum of all the Real numbers entered is:"<<sum<<endl; return 0; }

Program to show Next and Previous 5 Integers from Given One.

08:37 0 Comments A+ a-

Problem:
Write a program that takes an integer number from user and shows the next 5 and then previous 5 numbers on the screen.
Solution:
# include <iostream> using namespace std; int main() { int num; int i=0; int temp; cout<<"Enter the number"<<endl; cin>>num; temp=num; cout<<endl; while(i<5){ cout<<++num<<endl; i++; } cout<<endl; i=0; while(i<5){ cout<<--temp<<endl; i++; } return 0; }

Find Your GPA in a Semester

08:34 0 Comments A+ a-

Problem:
Write a Program that takes your average obtained mark of a course and then displays your GPA according to the following Grading System. Also mention that whether the student is on probation or not. [Hint: Use if-else structure]

GRADING SYSTEM

AVERAGE MARKS------------   GRADE POINTS

85-100 ------------------------------- 4.00

80-84 ---------------------------------3.70

75-79--------------------------------- 3.30

70-74--------------------------------- 3.00

65-69 ---------------------------------2.70

61-64--------------------------------- 2.30

58-60--------------------------------- 2.00

55-57--------------------------------- 1.70

50-54--------------------------------- 1.00

Below 50 ----------------------------0.00

A student attains Probation Status if his/her CGPA becomes 1.70 or more but less than 2.00.
Solution:
# include <iostream> using namespace std; int main() { double marks; double GPA; cout<<"Enter the Marks"<<endl; cin>>marks; if(marks>=85 && marks<=100){ cout<<"Your GPA is:"<<"4.00"<<endl; GPA=4.00;} else if(marks>=80){ cout<<"Your GPA is:"<<"3.70"<<endl; GPA=3.70;} else if(marks>=75){ cout<<"Your GPA is:"<<"3.30"<<endl; GPA=3.30;} else if(marks>=70){ cout<<"Your GPA is:"<<"3.00"<<endl; GPA=3.00;} else if(marks>=65){ cout<<"Your GPA is:"<<"2.70"<<endl; GPA=2.70;} else if(marks>=61){ cout<<"Your GPA is:"<<"2.30"<<endl; GPA=2.30;} else if(marks>=58){ cout<<"Your GPA is:"<<"2.00"<<endl; GPA=2.00;} else if(marks>=55){ cout<<"Your GPA is:"<<"1.70"<<endl; GPA=1.70;} else if(marks>=50){ cout<<"Your GPA is:"<<"1.00"<<endl; GPA=1.00;} else if(marks<50){ cout<<"Your GPA is:"<<"0.00"<<endl; GPA=0.00;} if(GPA>=1.70 && GPA<=2.00) cout<<"You are on Probation"<<endl; else if (GPA>2.00) cout<<"You are not on Probation"<<endl; return 0; }

Find the Day Against a Date of Year

08:29 0 Comments A+ a-

Problem:
Find the day (i.e. Monday, Tuesday etc.) against the date entered by the user. Suppose it was Tuesday on January 1.
Solution:
cout<<"The day number for"<<" "<<date<<"-"<<month<<" "<<"is"<<" "<<date<<endl; dayName=((date-1)%7); } else if(month==2){ days=31+date; cout<<"The day number for"<<" "<<date<<"# include <iostream>
using namespace std; int main() { int month; int date; int days; int dayName; cout<<"Enter the Month"<<endl; cin>>month; cout<<"Enter the Date"<<endl; cin>>date; if(month==1){-"<<month<<" "<<"is"<<" "<<days<<endl; dayName=((days-1)%7); } else if(month==3){ days=59+date; cout<<"The day number for"<<" "<<date<<"-"<<month<<" "<<"is"<<" "<<days<<endl; dayName=((days-1)%7); } else if(month==4){ days=90+date; cout<<"The day number for"<<" "<<date<<"-"<<month<<" "<<"is"<<" "<<days<<endl; dayName=((days-1)%7); } else if(month==5){ days=120+date; cout<<"The day number for"<<" "<<date<<"-"<<month<<" "<<"is"<<" "<<days<<endl; dayName=((days-1)%7); } else if(month==6){ days=151+date; cout<<"The day number for"<<" "<<date<<"-"<<month<<" "<<"is"<<" "<<days<<endl; dayName=((days-1)%7); } else if(month==7){ days=181+date; cout<<"The day number for"<<" "<<date<<"-"<<month<<" "<<"is"<<" "<<days<<endl; dayName=((days-1)%7); } else if(month==8){ days=212+date; cout<<"The day number for"<<" "<<date<<"-"<<month<<" "<<"is"<<" "<<days<<endl; dayName=((days-1)%7); } else if(month==9){ days=243+date; cout<<"The day number for"<<" "<<date<<"-"<<month<<" "<<"is"<<" "<<days<<endl; dayName=((days-1)%7); } else if(month==10){ days=273+date; cout<<"The day number for"<<" "<<date<<"-"<<month<<" "<<"is"<<" "<<days<<endl; dayName=((days-1)%7); } else if(month==11){ days=304+date; cout<<"The day number for"<<" "<<date<<"-"<<month<<" "<<"is"<<" "<<days<<endl; dayName=((days-1)%7); } else if(month==12){ days=334+date; cout<<"The day number for"<<" "<<date<<"-"<<month<<" "<<"is"<<" "<<days<<endl; dayName=((days-1)%7); } else cout<<"Months not in range"<<endl; if (dayName==0) cout<<"Tuesday"<<endl; if (dayName==1) cout<<"Wednesday"<<endl; if (dayName==2) cout<<"Thursday"<<endl; if (dayName==3) cout<<"Friday"<<endl; if (dayName==4) cout<<"Saturday"<<endl; if (dayName==5) cout<<"Sunday"<<endl; if (dayName==6) cout<<"Monday"<<endl; return 0; }

Check the number of days past in an year on a Specific Date

08:25 0 Comments A+ a-

Problem:
The day of year is a number between 1 and 365 (January 1 is day 1) for a common (i.e. non leap) year. Today is 10th April which is day number 100. Write a Program that asks the user to enter month (in numeric) and date and displays the corresponding day number.
Solution:
# include <iostream> using namespace std; int main() { int month; int date; int days;  
cout<<"Enter the Month"<<endl; cin>>month; cout<<"Enter the Date"<<endl; cin>>date; if(month==1)
{
cout<<"The day number for"<<" "<<date<<"-"<<month<<" "<<"is"<<" "<<date<<endl;
} else if(month==2)
{ days=31+date; cout<<"The day number for"<<" "<<date<<"-"<<month<<" "<<"is"<<" "<<days<<endl; } else if(month==3)
{
days=59+date; cout<<"The day number for"<<" "<<date<<"-"<<month<<" "<<"is"<<" "<<days<<endl; } else if(month==4)
{ days=90+date; cout<<"The day number for"<<" "<<date<<"-"<<month<<" "<<"is"<<" "<<days<<endl; } else if(month==5)
{ days=120+date; cout<<"The day number for"<<" "<<date<<"-"<<month<<" "<<"is"<<" "<<days<<endl; } else if(month==6)
{ days=151+date; cout<<"The day number for"<<" "<<date<<"-"<<month<<" "<<"is"<<" "<<days<<endl; } else if(month==7)
{ days=181+date; cout<<"The day number for"<<" "<<date<<"-"<<month<<" "<<"is"<<" "<<days<<endl; } else if(month==8)
{ days=212+date; cout<<"The day number for"<<" "<<date<<"-"<<month<<" "<<"is"<<" "<<days<<endl; } else if(month==9)
{ days=243+date; cout<<"The day number for"<<" "<<date<<"-"<<month<<" "<<"is"<<" "<<days<<endl; } else if(month==10)
{ days=273+date; cout<<"The day number for"<<" "<<date<<"-"<<month<<" "<<"is"<<" "<<days<<endl; } else if(month==11)
{ days=304+date; cout<<"The day number for"<<" "<<date<<"-"<<month<<" "<<"is"<<" "<<days<<endl; } else if(month==12)
{ days=334+date; cout<<"The day number for"<<" "<<date<<"-"<<month<<" "<<"is"<<" "<<days<<endl; } else cout<<"Months not in range"<<endl;
return 0; }

Finding Type and Value of Roots of Quadratic Equation

08:19 0 Comments A+ a-

Problem:
Write a Program that calculates the roots of a quadratic equation. For this, you are required to get the values of a, b and c from user and use quadratic formula for finding the roots. Your program should also display whether the roots are real or non-real.
 If Discriminant is positive then roots are real. 
 If Discriminant is negative then roots are non-real.
Solution:
# include <iostream>
# include <cmath>
using namespace std;
int main()
{
double a,b,c,x,y,z;
double root1;
double root2;

cout<<"Enter a:"<<endl;
cin>>a;
cout<<"Enter b:"<<endl;
cin>>b;
cout<<"Enter c:"<<endl;
cin>>c;
x=((pow(b,2))-4*a*c);
if(x>0)
{
cout<<"Roots are real"<<endl;
root1=(-b+(sqrt(x))/(2*a));
root2=(-b-(sqrt(x))/(2*a));
cout<<"Roots from the quadratic formula are:"<<endl;
cout<<root1<<endl;
cout<<root2<<endl;
}
else
cout<<"Roots are Non-Real"<<endl;

return 0;

}




Conversion of Weight from Kg to Pound with use of Setprecision

08:12 3 Comments A+ a-

Problem:
Write a program that prompts the user to enter the weight of a person in kilograms and outputs the equivalent weight in pounds. Output both the weights rounded to two decimal places. (Note that 1 kilogram = 2.2 pounds.) Format your output with two decimal places.
Solution:
# include <iostream> # include <iomanip> using namespace std; int main() { double weight; double poundWeight; cout<<"Enter the weight in kilogram"<<endl; cin>>weight; poundWeight=(weight*2.2); cout<<"The weight in kilograms is:"<<setprecision(2)<<fixed<<weight<<endl; cout<<"The weight in pounds is:"<<setprecision(2)<<poundWeight<<endl; return 0; }