Pages

Showing posts with label OTHER. Show all posts
Showing posts with label OTHER. Show all posts

Monday, December 7, 2020

C++ program to print an lower triangle using stars.


#include<iostream.h>
#include<conio.h>
void main()
{
  int j,i,k,l;
  char c;
  clrscr();
  do
  {
     cout<<"Enter the number of stars in the base..";
     cin>>k;
     for (i=1;i<=k;i++)
     {    
         for (j=1;j<=i;j++)
            cout<<"* ";
         cout<<endl;
     }
     cout<<"\nWant to try more..(y/n)\n";
     cin>>c;
  }
  while(c=='y');
  getch();
}

Monday, November 9, 2020

Program to check if the given Value is of type Int in CPP

 

#include <iostream>
using namespace std;

bool isNumber(string s)
{
    for (int i = 0; i < s.length(); i++)
        if (isdigit(s[i]) == false)
            return false;
 
    return true;
}

int main() {
    // Saving the input in a string
    string str = "";
    
    cout << "Enter Value.\n";
    cin >> str;
 
    // Function returns 1 if all elements
    // are in range '0-9'
    if (isNumber(str))
        cout << "Entered Value is Integer";
 
    // Function returns 0 if the input is
    // not an integer
    else
        cout << "Entered Value is String";
        
    return 0;
}

Wednesday, December 19, 2018

C++ PROGRAM TO DISPLAY ODD NUMBERS FROM 1 TO N

#include <iostream>

using namespace std;

int main()
{
    int n;
    cout<<"Enter the Limit\n";
    cin>>n;
    cout<<"Odd numbers between 1 and "<<n<<" are\n";
    for(int i=1;i<=n;i++)
    {
        if(i%2 != 0)
            cout<<i<<"\n";
    }
    return 0;
}

 

Friday, March 29, 2013

Template Function

Write a template function that swaps the values of two arguments passed to it. In main( ), use the function with integers and characters.

#include<iostream.h>
using namespace std;
template <class T>
void swap(T &a,T &b)
{
    T temp=a;
    a=b;
    b=temp;
}
void func(int x,int y,char w,char z)
{
    cout<<"x and y before swap: "<<x<<y<<"\n";
    swap(x,y)
    cout<<"x and y after swap: "<<x<<y<<"\n";
    cout<<"w and z before swap: "<<w<<z<<"\n";
    swap(w,z)
    cout<<"w and z after swap: "<<w<<z<<"\n";

}
int main()
{
    fun(10,20,’s’,’S’);
    return 0;
}



You might also like:-

Saturday, December 8, 2012

C++ PROGRAM - FACTORIAL USING RECURSION.

Write a C++ program which calculates the factorial of a given number using recursion.

#include<conio.h>
#include<iostream.h>
long factorial (long a)
{
    if (a > 1)
        return (a * factorial (a-1));
    else
        return (1);
}
int main ()
{
    long ln;
    clrscr();
    cout << "Enter a number: ";
    cin >> ln;
    cout << ln << "!" << " = " << factorial (ln);
    getch();
    return 0;
}


OUTPUT








You might also like:-

Saturday, September 15, 2012

C++ Program to calculate average of 3 marks of 10 students.


Define a STUDENT class with USN, Name, and Marks in 3 tests of a subject. Declare an array of 10 STUDENT objects. Using appropriate functions, find the average of two better marks for each student. Print the USN, Name, and the average marks of all the students.


#include<iostream.h>
#include<conio.h>
#define SIZE 10
class emp
{
  float m1,m2,m3;
  char name[20],num[10];
  public:
  void getdata();
  void dispdata();
};
void emp::getdata()
{
  cout<<"\nEnter student USN: " ;
  cin>>name;
  cout<<"Enter student name: " ;
  cin>>num;
  cout<<"Enter student's 3 marks: " ;
  cin>>m1>>m2>>m3;
}
void emp::dispdata()
{
  float avg,low=m3;
  if(m2<low)
    low=m2;
  else if(m3<low)
    low=m3;
  avg=(m1+m2+m3-low)/2;
  cout<<"\n          student USN: "<<name
  <<"\n         student name: "<<num
  <<"\n      student average: "<<avg;
}
void main()
{
  clrscr();
  emp ob[SIZE];
  int n;
  cout<<"\n";
  cout<<"\n*******************************"
      <<"\n        Students Report"
      <<"\n*******************************"
      <<"\n Enter the number of students: ";
  cin>>n;
  for(int i=0;i<n;i++)
  {
    ob[i].getdata();
  }
  clrscr();
  cout<<"\n-----------------"
  <<"\nstudents Details::"
  <<"\n-----------------";
  for( i=0;i<n;i++)
  {
    cout<<"\n\n student:"<<i+1
    <<"\n ----------";
    ob[i].dispdata();
  }
  getch();
}


OUTPUT



Friday, August 17, 2012

ADDING 'TIME' OBJECTS AND DISPLAYING THE RESULT IN HH:MM:SS FORMAT USING MEMBER FUNCTION


Create a class called 'time' that has three integer data members for hours, minutes and seconds, define a member function to read the values, member function to add two time objects, member function to display time in HH:MM:SS format. Write a main function to create two time objects, add them and display the results in HH:MM:SS format.

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class time
{
  int h,m,s;
  public:
  void read();
  void print();
  void add(time t2);
};
void time::read()
{
  cout<<"\nEnter hour,minutes and seconds\n";
  cin>>h>>m>>s;
}
void time::print()
{
  cout<<"\nTime is-> "<<setfill('0')<<setw(2)<<h;
  cout<<":"<<setfill('0')<<setw(2)<<m;
  cout<<":"<<setfill('0')<<setw(2)<<s<<endl;
}
void time::add(time t2)
{
  h=h+t2.h;
  m=m+t2.m;
  s=s+t2.s;
}
void main()
{
  clrscr();
  time t1,t2;
  t1.read();
  t1.print();
  t2.read();
  t2.print();
  t1.add(t2);  // member function call.
  cout<<"\nTime1+ Time2:\n";
  t1.print();
  getch();
}

output
                                                                                                                                                                                                           

Thursday, August 2, 2012

C++ PROGRAM TO READ AND PRINT THE DETAILS OF 'N' EMPLOYEES

This program describes the use of object array.

#include<iostream.h>
#include<conio.h>
class employee
{
   int emp_id;
   char name[25],post[30];
   public:
   void read();
   void print();
};
void employee::read()
{
   cout<<"Enter employee id\n";
   cin>>emp_id;
   cout<<"Enter employee name\n";
   cin>>name;
   cout<<"Enter employee post\n";
   cin>>post;
}
void employee::print()
{
   cout<<emp_id<<"\t"<<name<<"\t"<<post<<endl;
}
void main()
{
   int n;
   clrscr();
   employee e[20]; // object array creation
   cout<<"Enter the number of employees\n(less than or equal to    10)\n";
   cin>>n;
   for(int i=0;i<n;i++) //read function call for 'n' employees
     e[i].read();
   cout<<"\n\nID\tNAME\tPOST\n";
   for(i=0;i<n;i++) //print function call for 'n' employees
     e[i].print();
   getch();
}

output


 



PROGRAM TO FIND THE AREA OF A RECTANGLE USING FUNCTION DEFINITION OUTSIDE CLASS



#include<iostream.h>
#include<conio.h>
class rectangle
{
   int length,breadth,area;
   public:
   void readvalues();
   void recarea();
};
void rectangle::readvalues()  
//function definition outside class
{
   cout<<"Enter length and breadth\n";
   cin>>length>>breadth;
}
void rectangle::recarea()  //function definition outside class
{
   area=length*breadth;
   cout<<"Area is : "<<area;
}
void main()
{
   clrscr();
   rectangle r1; // object creation
   r1.readvalues(); //function call using dot operator
   r1.recarea(); //recarea() function call
   getch();
}

PROGRAM TO FIND THE AREA OF A RECTANGLE USING FUNCTION DEFINITION INSIDE CLASS


#incluce<iostream.h>
#include<conio.h>
class rectangle
{
   int length,breadth,area;
   public:
   void readvalues()
   {
       cout<<"Enter length and breadth\n";
       cin>>length>>breadth;
   }
   void recarea()
  {
      area=length*breadth;
      cout<<"Area is : "<<area;
  }
};
void main()
{
    clrscr();
    rectangle r1;   // object creation
    r1.readvalues();  //function call using dot operator
    r1.recarea();            //recarea() function call
    getch();
}

FIRST C++ PROGRAM

#include<iostream>
using namespace std;
int main()
{
   cout<<"Hello World"<<endl;
   return 0;
}