sample programs on c++

Upload: roopa-hegde

Post on 08-Aug-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/22/2019 Sample Programs on c++

    1/78

    Sample programs continued..

    Programs related to

    Basic data types

    Basic Operators

    Operator precedence

    Loops

    If

    Switch

    Case

    Cin

    Cout

  • 8/22/2019 Sample Programs on c++

    2/78

    WRITE C++ CODE TO SWAP TWO INTEGER

  • 8/22/2019 Sample Programs on c++

    3/78

    #include

    using namespace std;int main(){

    int a, b, c;couta;

    coutb;

    c=a;

    a=b;b=c;cout

  • 8/22/2019 Sample Programs on c++

    4/78

    FIND THE GREATER OF TWO GIVEN NU

  • 8/22/2019 Sample Programs on c++

    5/78

    note

    cout numberr; // to read through keyboard//***** if you use quotes

    //message will be displayed

    //extraction operator

  • 8/22/2019 Sample Programs on c++

    6/78

    #includeusing namespace std;

    int main(){

    int num1, num2;cout > num1;cout > num2;

    if (num1>num2){

    cout

  • 8/22/2019 Sample Programs on c++

    7/78

    Write c++ code to find greatest, second greatesand the least of three numbers

    //G O S

  • 8/22/2019 Sample Programs on c++

    8/78

    //GREATER OF THREE NUMBERS#includeusing namespace std;int main(){int a, b, c;

    couta;coutb; // ********* or convert into single line ex: cin>>a>>bcoutc;if(a>b && a>c && b>c){

    cout

  • 8/22/2019 Sample Programs on c++

    9/78

    else if(b>a && b>c && a>c){cout

  • 8/22/2019 Sample Programs on c++

    10/78

    else if(c>a && c>b && a>b){cout

  • 8/22/2019 Sample Programs on c++

    11/78

    Operator precedence

  • 8/22/2019 Sample Programs on c++

    12/78

    Continued..

  • 8/22/2019 Sample Programs on c++

    13/78

    find x

    X=5+83

    X=6+7>=12&&(3+4)>2*4

  • 8/22/2019 Sample Programs on c++

    14/78

    Answer..

    X=1

    X=0

  • 8/22/2019 Sample Programs on c++

    15/78

    //TO FIND THE SUM OF TWO NUMBERS

  • 8/22/2019 Sample Programs on c++

    16/78

    #includeusing namespace std;

    int main (){int no1,no2,sum;coutno1;coutno2;sum=no1+no2;cout

  • 8/22/2019 Sample Programs on c++

    17/78

    // TO DIVIDE A NUMBER WITH ANOTHER

  • 8/22/2019 Sample Programs on c++

    18/78

    #includeusing namespace std;

    int main(){float quotient, remainder;int no1,no2;

    cout no1;coutno2;quotient=no1/no2;remainder=no1%no2;cout

  • 8/22/2019 Sample Programs on c++

    19/78

    // CLASSIFY THE TOTAL NUMBER OF DAYS INPUT INTO YEAR, WEEK AND

  • 8/22/2019 Sample Programs on c++

    20/78

    #includeusing namespace std;int main(){long num, temp, days, week, year;char ch;

    do{coutnum;year=num/365;temp=num%365;week=temp/7;days=temp%7;cout

  • 8/22/2019 Sample Programs on c++

    21/78

    DISPLAY MONTH CORRESPONDING TOINPUT, USING IF-ELSECONSTRUCT

    // DISPLAY MONTH CORRESPONDING TO INPUT,

  • 8/22/2019 Sample Programs on c++

    22/78

    USING IF-ELSECONSTRUCT#includeusing namespace std;

    int main(){int n;coutn;

    while(n>12||n>n;}

    if(n==1)cout

  • 8/22/2019 Sample Programs on c++

    23/78

    cout

  • 8/22/2019 Sample Programs on c++

    24/78

    MONTH DISPLAY USING SWITCH

    #include

  • 8/22/2019 Sample Programs on c++

    25/78

    int main(){int m;coutm;while(m>12||m

  • 8/22/2019 Sample Programs on c++

    26/78

    {case 1 : cout

  • 8/22/2019 Sample Programs on c++

    27/78

    //DISPLAY MEANING OF GRADES USING SWICH CASESTRUCTURE

    i i

  • 8/22/2019 Sample Programs on c++

    28/78

    #include#includevoid main(){clrscr();char ch;

    coutch;switch(ch){case 'a':case 'A':cout

  • 8/22/2019 Sample Programs on c++

    29/78

    case c :case 'C':cout

  • 8/22/2019 Sample Programs on c++

    30/78

    // SIMPLE CALCULATOR FOR 2 FRACTIONS

    #include#i l d < i h>

  • 8/22/2019 Sample Programs on c++

    31/78

    #include#includevoid main(){float num1,num2,den1,den2,result; char sign, choice;do

    {clrscr();cout

  • 8/22/2019 Sample Programs on c++

    32/78

    { case : result ((num1/den1) (num2/den2)); break;case '-': result=((num1/den1)-(num2/den2)); break;case '*': result=((num1/den1)*(num2/den2)); break;case '/': result=((num1/den1)/(num2/den2)); break;default :cout

  • 8/22/2019 Sample Programs on c++

    33/78

    // A SAMPLE PROGRAM TO PRINT

  • 8/22/2019 Sample Programs on c++

    34/78

    #includeUsing namespace std;Int main (){int num;for (num=1; num

  • 8/22/2019 Sample Programs on c++

    35/78

    // TO DETERMINE A PRIME NUMBER.

  • 8/22/2019 Sample Programs on c++

    36/78

    #includeUsing namespace std;int main (){long num, fact; int count;cout > num;for ( fact=1, count=0; fact

  • 8/22/2019 Sample Programs on c++

    37/78

    // PRINT THE MULTIPLES OF A NUMBER USING A DO-WHILE LOOP;

  • 8/22/2019 Sample Programs on c++

    38/78

    #include#includevoid main(){int num, multi=0;clrscr();coutnum;cout

  • 8/22/2019 Sample Programs on c++

    39/78

    // PRINT THE MULTIPLICATION TABLE OF A NUMBERUSING A FOR LOOP;

  • 8/22/2019 Sample Programs on c++

    40/78

    #include#includevoid main(){int num, multi;

    clrscr();coutnum;cout

  • 8/22/2019 Sample Programs on c++

    41/78

    // TO DISPLAY SERIES : 2,6,12,20,30,42,56.......N

  • 8/22/2019 Sample Programs on c++

    42/78

    #include#includevoid main(){int sum=0, i,n, t;clrscr();coutn;cout

  • 8/22/2019 Sample Programs on c++

    43/78

    // PRINT THE FIBONACCI SERIES

    #include#includevoid main()

  • 8/22/2019 Sample Programs on c++

    44/78

    void main(){/*FIBONACCI SERIES */char choice;do{

    clrscr();long double num, a=1, b=0, count;coutnum;cout

  • 8/22/2019 Sample Programs on c++

    45/78

    /* SUM OF AN ARITHMETIC PROGRESSION WHOSEFIRST TERM AND LAST TERMS AREENTERED BY THE USER AND THE COMMON DIFFERENCEIS 1 */

    #include#includevoid main()

  • 8/22/2019 Sample Programs on c++

    46/78

    void main(){long double first, last, sum ; char choice;do{clrscr();

    coutfirst;coutlast;for(sum=0;first

  • 8/22/2019 Sample Programs on c++

    47/78

    // TO CALCULATE THE SUM OF DIGITS IN A NUMBERRETURN TOINDEX#include#include

    void main (){long num, digit, sum;clrscr();cout > num;for (sum=0; num!=0; num=num/10){

    digit=num%10;sum=sum+digit;}cout

  • 8/22/2019 Sample Programs on c++

    48/78

    julian 365;switch(month){case 1: julian -= 0;case 2: julian -= 31;case 3: if ( (year%100!=0 && year%4==0) ||

    year%400==0)julian -= 29;else

    julian -= 28;case 4: julian -= 31;case 5: julian -= 30;case 6: julian -= 31;case 7: julian -= 30;

    case 8: julian -= 31;case 9: julian -= 31;case 10: julian -= 30;case 11: julian -= 31;case 12: julian -= 30;}

    julian+=date;fours=(year-1)/4;hundreds= ear-1 100

    // INCREMENT AND DECREMENT OPERATORSRETURN

  • 8/22/2019 Sample Programs on c++

    49/78

    RETURNTO INDEX#includeUsing namespace std;Int main();{

    clrscr();int num, a,b,c,d;coutnum;a=num;b=num;c=num;d=num;cout

  • 8/22/2019 Sample Programs on c++

    50/78

    // SUM OF ALL TERMS OF AN ARRAY INPUT BY THEUSER RETURN TO INDEX#include#include

    void main(){clrscr(); long float num[10], sum=0, y=1;cout

  • 8/22/2019 Sample Programs on c++

    51/78

    clrscr();//*********input**************cout

  • 8/22/2019 Sample Programs on c++

    52/78

    \ \ \ ;cin.getline(str, 70);len=strlen(str);cout

  • 8/22/2019 Sample Programs on c++

    53/78

    yp#includevoid main(){clrscr();char str[50];

    int flag=1;cout

  • 8/22/2019 Sample Programs on c++

    54/78

    exit(0);}cout

  • 8/22/2019 Sample Programs on c++

    55/78

    INDEX#include#includevoid main(){

    float cube(float);float x,y;clrscr();coutx;y=cube(x);cout

  • 8/22/2019 Sample Programs on c++

    56/78

    float large(float array[],int n);clrscr();for(i=0;i

  • 8/22/2019 Sample Programs on c++

    57/78

    #includevoid main(){int num;void suffix(int);void prefix(int);clrscr();cin>>num;prefix(num);suffix(num);getch();}void prefix(int n)

    {cout

  • 8/22/2019 Sample Programs on c++

    58/78

    void pyramid(){static int n=0;int p, m ,q;n++;

    for(p=1;p

  • 8/22/2019 Sample Programs on c++

    59/78

    int i,j 2,flag 1;if(a>b)i=a;elsei=b;while((j

  • 8/22/2019 Sample Programs on c++

    60/78

    #include iostream.h#includeint summat(int first,int count);void main(){clrscr();unsigned long a, b, sum;couta;coutb;sum=summat(a,b);cout

  • 8/22/2019 Sample Programs on c++

    61/78

    RETURN TO INDEX#include#includevoid main(){

    clrscr();int change(int);int original=10;cout

  • 8/22/2019 Sample Programs on c++

    62/78

    #includevoid main(){void swap(int &, int &);int a=7, b=9;

    clrscr();cout

  • 8/22/2019 Sample Programs on c++

    63/78

    #include#include#includevoid func(void);void main(){clrscr();func();getch();}void func(void){

    char name[25];cout

  • 8/22/2019 Sample Programs on c++

    64/78

    void err_msg(void);do{coutmarks;

    if(marks100)err_msg();}while(marks100);return marks;}void err_msg(void){cout

  • 8/22/2019 Sample Programs on c++

    65/78

    for(r3=7; r3

  • 8/22/2019 Sample Programs on c++

    66/78

    //TERNARY OPERATOR#include

    #includevoid main(){int count=0;int num1=7, num2=4;clrscr();count=(num1

  • 8/22/2019 Sample Programs on c++

    67/78

    //NESTED TERNARY OPERATOR RETURN TO INDEX#include

    #includevoid main(){int count=0;int num1=2;clrscr();count=(num10)?7:9;cout

  • 8/22/2019 Sample Programs on c++

    68/78

    #include

    #include using namespace std;int main (){string user_name;cout > user_name;cout

  • 8/22/2019 Sample Programs on c++

    69/78

    g#include #include using namespace std;int main ()

    {string user_first_name;string user_last_name;cout > user_first_name;cout > user_last_name;string user_full_name;

    user_full_name = user_first_name + " " + user_last_name;cout

  • 8/22/2019 Sample Programs on c++

    70/78

    onto another, you can use the +sign:#include #include using namespace std;int main ()

    {string user_first_name;string user_last_name;cout > user_first_name;cout > user_last_name;string user_full_name = user_first_name + " " + user_last_name;cout

  • 8/22/2019 Sample Programs on c++

    71/78

    return is detected

    getline( cin, user_first_name, '\n' );

    // it neglects new line character

    The little secret of floating point numbersI want to let you in on something about floating point numbers like float or doubletheygood in practice, since they can represent a huge range of values. The maximum valuerepresent is about 1.8 10^308. Thats a number with 308 zeros at the end of it. But a dou

  • 8/22/2019 Sample Programs on c++

    72/78

    p 8 0 308 308bytesdoesnt that mean it can store only2^32 (18,446,744,073,709,551,616) possible vaLOT of zeros, but its not 308 zeros.) Yes, thats right! In fact, a double can only representquintillion numbers. Thats very largeso large I had to look up what was the name for azeros. But its still not 308 zeros. Floating point numbers allow you to exactly represent on

    of the values that are in their actual overall range by using a format similar to scientific nIn scientific notation, you write out numbers in the form 10^. The x usually stores the fof the number, and the y value, the exponent, stores the power to raise the number to.

    you could write the distance between the sun and the earth as 9.295610^7miles (approximately 93 million miles).

  • 8/22/2019 Sample Programs on c++

    73/78

    This allows the computer to store really big numbers by putting a very large number inBut the non-exponent part cant store 300 digits, it can only store about 15 digits. So yodigits of precision when you work with floating point numbers. If youre dealing with revalues, then the difference between the number the computer stores, and the actua

    very small. If youre working with huge values, well, youre going to have large absoluthe relative error is small. For example, if I had only two digits of precision, I could write9.3 10^7 miles from the sun. Thats very close, in relative terms, to the right value (lessBut in absolute terms, its 44 thousand miles! Thats nearly twice the circumference of tcourse, thats using only 2 digits of precision. With 15, you can get a lot closer for numbmillion. In most cases, the inexactness of floating point numbers wont affect you unleserious number crunching or scientific computing, but when it matters, it matters.

    String comparisonsC++ string objects allow you to use all of the comparisons that you learned about eathis chapter.

  • 8/22/2019 Sample Programs on c++

    74/78

    By comparing string objects, we can write our password checker!#include #include using namespace std;

    int main (){string password;cout

  • 8/22/2019 Sample Programs on c++

    75/78

    C++ allows you to store the results of comparisons byusing a special type called a bool.10The bool typeis not that different from an integer, but it has one

    advantage: it makes it very clear that you are onlyever going to use two possible values, true and false.These keywords, and the bool type, make yourintentions more clear. The result of all comparisonoperators is a Boolean.int x;cin >> x;bool is_x_two = x == 2; // note double-equals forcomparisonif ( is_x_two ){// take some action because x is two!}

    Lets look at an example of using Boolean and to create a passwordprogram that checks for both ausername and a password.#include

  • 8/22/2019 Sample Programs on c++

    76/78

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

    string username;string password;cout

  • 8/22/2019 Sample Programs on c++

    77/78

    TO INDEX#include#include#include

    #includevoid main(){float a,b,c,root1,root2,delta;char choice;do{clrscr();

    cout

  • 8/22/2019 Sample Programs on c++

    78/78

    delta=(b*b)-(4*a*c);if(delta