Friday, April 26, 2013

number in pyramid before space in C++


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

int a;
int b;
cout<<"enter the number pyramid limit:";
cin>>b;
int nPyramid = 1;

int k=b;
for(a=1;a<=k;a++)
{


for(int i=0;i<b;i++)
{                   //for the spaces
cout<<nPyramid<<"\t";

nPyramid++;         //Increase the pyramid value by 1
}

nPyramid--; //Remove this line and see the effect
cout<<endl; //Next line
for(int i=1;i<=a;i++)
{ cout<<"\t";
}
b--;
}
}
output:-

number in pyramid space after in C++


#include <iostream>
using namespace std;
int main(){
int a;
int b;
cout<<"enter the number pyramid limit:";
cin>>b;
int nPyramid = 1;
int k=b;
for(a=1;a<=k;a++)
{
for(int i=0;i<b;i++)
{                   //for the spaces
cout<<"\t"<<nPyramid;

nPyramid++;         //Increase the pyramid value by 1
}

nPyramid--;                 //Remove this line and see the effect
cout<<endl;               //Next line
b--;
}
}

Wednesday, March 20, 2013

displayind odd numbers upto 100 in C++

#include <iostream.h>
#include <conio.h>
int main()
{
int i;
for(int i=1;i <= 100 ;i++)
{

 if(i%2==1)
cout<<i<<endl;
}
getch();
return 0;

}

Saturday, December 08, 2012

Calender


#include <iostream>
#include <stdlib.h>
#include <iomanip>
using namespace std;
typedef int Bool;

// converts 0 to 'S', 1 to 'M', 2 for 'T',3 for 'W',4 for 'T',5 for 'F' ,6 for 'S'
char whatDay (int);

// not very good check for leap years
Bool isLeapYear (int);

// takes the number of the month, a flag saying whether year is leap
int numOfDaysInMonth (int,Bool);

void printHeader (int);

// takes the number of the month, and the first day, prints, and updates
// the first day of the next month
void printMonth (int, int&);

// prints the specified amount of spaces
void skip (int);

// prints leading spaces in monthly calendar
void skipToDay (int);

// terminates program in case of unrecoverable errors
void disaster ();

int main () {

  int year, firstDayInCurrentMonth;
  Bool leap;
  int currentMonth = 1; // start at Jan
  int numDays;

  cout << "What year do you want a calendar for? ";
  cin >> year;
  cout << "What day of the week does January 1 fall on?" << endl;
  cout << "Enter 0 for Sunday, 1 for Monday,2 for Tuesday,3 for Wednesday,4 for Thursday,5 for Friday ,6 for Saturday ";
  cin >> firstDayInCurrentMonth;

  leap = isLeapYear(year);
  skip(9); cout << year << endl;

  while (currentMonth <= 12) {
    numDays = numOfDaysInMonth(currentMonth,leap);
    printHeader(currentMonth);
    printMonth(numDays, firstDayInCurrentMonth);
    cout << endl << endl << endl;
    currentMonth = currentMonth + 1;
  }
  cout << endl;
}

void disaster () {
  cout << "Disaster! Exiting ..." << endl;
  exit (-1);
}

void skip (int i) {
  while (i > 0) {
    cout << " ";
    i = i - 1;
  }
}

char whatDay (int d) {
  if (d == 0) return('S');
  else if (d == 1) return('M');
  else if (d == 2) return('T');
  else if (d == 3) return('W');
  else if (d == 4) return('T');
  else if (d == 5) return('F');
  else if (d == 6) return('S');
  else disaster();
}

Bool isLeapYear (int y) {
  return ((y % 4) == 0); // simplified
}

void printHeader (int m) {

  if (m == 1) { skip(7); cout << "January" << endl; }
  else if (m == 2) { skip(7); cout << "February" << endl; }
  else if (m == 3) { skip(7); cout << "March" << endl; }
  else if (m == 4) { skip(7); cout << "April" << endl; }
  else if (m == 5) { skip(7); cout << "May" << endl; }
  else if (m == 6) { skip(7); cout << "June" << endl; }
  else if (m == 7) { skip(7); cout << "July" << endl; }
  else if (m == 8) { skip(7); cout << "August" << endl; }
  else if (m == 9) { skip(7); cout << "September" << endl; }
  else if (m == 10) { skip(7); cout << "October" << endl; }
  else if (m == 11) { skip(7); cout << "November" << endl; }
  else if (m == 12) { skip(7); cout << "December" << endl; }
  else disaster();

  cout << " S  M  T  W  T  F  S" << endl;
  cout << "____________________" << endl;
}

int numOfDaysInMonth (int m, Bool leap) {
  if (m == 1) return(31);
  else if (m == 2) if (leap) return(29); else return(28);
  else if (m == 3) return(31);
  else if (m == 4) return(30);
  else if (m == 5) return(31);
  else if (m == 6) return(30);
  else if (m == 7) return(31);
  else if (m == 8) return(31);
  else if (m == 9) return(30);
  else if (m == 10) return(31);
  else if (m == 11) return(30);
  else if (m == 12) return(31);
  else disaster();
}

void skipToDay (int d) {
  skip(3*d);
}

void printMonth (int numDays, int& weekDay) {
  int day = 1;

  skipToDay(weekDay);
  while (day <= numDays) {
    cout << setw(2) << day << " ";
    if (weekDay == 6) {
      cout << endl;
      weekDay = 0;
    }
    else weekDay = weekDay + 1;
    day = day + 1;
  }
}

Tuesday, December 04, 2012

GPA calculate in C++


// it is use in kust i calculate my gpa that is correct
#include <iostream>
using namespace std;

int main(){
    float nmbr, grade, credits, GPA = 0, totalCredits = 0;

    cout << "Enter The Number of Courses: ";
    cin >> nmbr;

    for(int i = 0; i < nmbr; i++){
        cout << "Enter the credit number for course No. " << i+1 << " : ";
        cin >> credits;
        totalCredits =totalCredits + credits;

        cout << "Enter the grade for course No. " << i+1 << " : ";
        cin >> grade;
         GPA =GPA+grade*credits;
    }

    GPA /= totalCredits;

    cout << "The GPA Is: " << GPA << " / ";
    if(GPA >=.7 && GPA <= 1)
        cout << "F oh no";
    else if(GPA >= 1 && GPA <= 1.33)
        cout << "D";
    else if(GPA >= 1.33 && GPA <= 1.7)
        cout << "D+";
    else if(GPA >=1.7 && GPA <= 2)
        cout << "C-";
    else if(GPA >= 2 && GPA <= 2.33)
        cout << "C";
    else if(GPA >= 2.33 && GPA <= 2.63)
        cout << "C+";
    else if(GPA >= 2.63  && GPA <= 3)
        cout << "B-";
    else if(GPA >= 3 && GPA <= 3.33)
        cout << "B";
    else if(GPA >=3.33 && GPA <= 3.63)
        cout << "B+";
    else if(GPA >=3.63 && GPA <= 4)
        cout << "A-";
    else if(GPA == 4)
        cout << "A oh....................\n";
 

    return 0;
}

Monday, December 03, 2012

program to find nCr using function in C++


#include<iostream>
 using namespace std;
long factorial(int);
long find_ncr(int, int);
long find_npr(int, int);

main()
{
   int n, r;
   long ncr, npr;

   cout<<"Enter the value of n and r\n";
  cin>>n>>r;

   ncr = find_ncr(n, r);
   npr = find_npr(n, r);

   cout<<n<<"C"<<r<<"=\n"<<ncr;
   cout<<"\n"<<n<<"P"<<r<<" =\n"<<npr;

   return 0;
}

long find_ncr(int n, int r)
{
   long result;

   result = factorial(n)/(factorial(r)*factorial(n-r));

   return result;
}

long find_npr(int n, int r)
{
   long result;

   result = factorial(n)/factorial(n-r);

   return result;
}

long factorial(int n)
{
   int c;
   long result = 1;

   for( c = 1 ; c <= n ; c++ )
      result = result*c;

   return ( result );
}

H.C.F and L.C.M in C++


#include <iostream>
 using namespace std;
int main() {
  int a, b, x, y, t, gcd, lcm;

  cout<<"Enter two integers\n";
  cin>>x>>y;

  a = x;
  b = y;

  while (b != 0) {
    t = b;
    b = a % b;
    a = t;
  }

  gcd = a;
  lcm = (x*y)/gcd;

  cout<<"Greatest common divisor of "<<x<<" and "<<y<<"=\n"<<gcd;
  cout<<"\nLeast common multiple of"<<x<<" and"<<y<<" =\n"<<lcm;

  return 0;
}