Friday, October 28, 2016

C++(Absolute value)

Find the absolute value of a number entered by the user. 


int main()
{
      int a;
      cout<<"Enter any number:";
      cin>>a;
 
      if(a>0)
            cout<<"The absolute value of number is:"<<a;
      else
            cout<<"The absolute value of number is:"<<-(a);
 
      getch();
      return 0;

}

C++ basic program

Any integer is input by the user. Write a program to find out whether it is an odd number or even number. 

 int main()
{
      int a;
      cout<<"Enter any number : ";
      cin>>a;
 
      if(a%2==0)
            cout<<"The number is even";
      else
            cout<<"The number is odd";
      
      getch();
      return 0;

}

C++(Year,Months and Days)

Write a program which accepts days as integer and display total number of years, months and days in it.
for example :  If user input as 856 days the output should be 2 years 4 months 6 days.
 

#include<iostream.h>
#include<conio.h>
 
int main()
{
      int days,y,m,d;
      cout<<"Enter no. of days : ";
      cin>>days;
      y=days/365;
      days=days%365;
      m=days/30;
      d=days%30;
      cout<<"Years : "<<y<<"\nMonths : "<<m<<"\nDays : "<<d;
 
      getch();
      return 0;

}

C++(Display Next Character)

Write a program which accepts a character and display its next character. 
Sol:  #include<iostream.h>
#include<conio.h>
 
int main()
{
      char ch;
      cout<< "\nEnter any character : ";
      cin>>ch;
      ch++;
      cout<<"Next character is : "<<ch;
 
      getch();
      return 0;

}

C++(Display No. Of Notes)

Write a program which accepts amount as integer and display total number of Notes of Rs. 500, 100, 50, 20, 10, 5 and 1.
For example, when user enter a number, 575,
the results would be like this...
500: 1
100: 0
50: 1
20: 1
10: 0
5: 1
1: 0
 
Sol:
#include<iostream.h>
#include<conio.h>
 
int main()
{
      int amt,R500,R100,R50,R20,R10,R5,R1;
      cout<<"Enter amount : ";
      cin>>amt;
      R500=amt/500;
      amt=amt%500;
      R100=amt/100;
      amt=amt%100;
      R50=amt/50;
      amt=amt%50;
      R20=amt/20;
      amt=amt%20;
      R10=amt/10;
      amt=amt%10;
      R5=amt/5;
      amt=amt%5;
      R1=amt;
      cout<<"Rs.500 : "<<R500<<"\nRs.100 : "<<R100<<"\nRs. 50 : "<<R50<<
            "\nRs. 20 : "<<R20<<"\nRs. 10 : "<<R10<<"\nRs.  5 : "<<R5<<"\nRe.  1 : "<<R1;
 
      getch();
      return 0;

}

C++(Display Largest Number)


Write a program which input three numbers and display the largest number using ternary operator.

 #include <iostream.h>
#include <conio.h>
 
int main()
{
      int a,b,c,greatest;
      cout<<"Enter three numbers : ";
      cin>>a>>b>>c;
      greatest=(a>b&&a>c)?a:(b>c)?b : c;
      cout<<"Greatest number is "<<greatest;
 
      getch();
      return 0;
}

C++(Check Even or Odd)

10
-Write a program to check whether the given number is even or odd (using ? : ternary operator ) 

#include<iostream.h>
#include<conio.h>
 
int main()
{
      int a;
      cout<<"Enter the Number : ";
      cin>>a;
      (a%2==0)?cout<<"Number is even":cout<<"Number is odd";
 
      getch();
      return 0;
}

C++(check Positive or Negative)

9
-Write a program to check whether the given number is positive or negative  (using ? : ternary operator ) 

#include<iostream.h>
#include<conio.h>
 
int main()
{
      int a;
      cout<<"Enter any non-zero Number : ";
      cin>>a;
      (a>0)?cout<<"Number is positive":cout<<"Number is negative";
 
      getch();
      return 0;
}

C++(Calculate Area)

8
- Write a program to calculate area of circle. 

#include<iostream.h>
#include<conio.h>
int main()
{
      float r,area;
      cout<< "\nEnter radius of circle : ";
 
      cin>>r;
      area = 3.14*r*r;
 
      cout<<"Area of circle : "<<area;
 
      getch();
      return 0;
}

C++(Swap The Values)

7-
Write a program to swap the values of two variables. 

#include<iostream.h>
#include<conio.h>
 
int main()
{
      int a,b,temp;
      cout<<"\nEnter two numbers : ";
      cin>>a>>b;
      temp=a;
      a=b;
      b=temp;
      cout<<"\nAfter swapping numbers are : ";
      cout<<a<<" "<<b;
 
      getch();
      return 0;
}

C++(Display ASCII Value)

6 -
Write a program which accepts a character and display its ASCII value. 

#include<iostream.h>
#include<conio.h>
 
int main()
{
      char ch;
      cout<< "\nEnter any character : ";
      cin>>ch;
      cout<<"ASCII equivalent is : "<<(int)ch;
 
      getch();
      return 0;
}

C++(Simple Intrest)

5-
Write a program which accept principle, rate and time from user and print the simple interest. 

#include<iostream.h>
#include<conio.h>
 
int main()
{
      int p,r,t,i;
      cout<<"Enter Principle : ";
      cin>>p;
      cout<<"Enter Rate : ";
      cin>>r;
      cout<<"Enter Time : ";
      cin>>t;
      i=(p*r*t)/100;
      cout<<"Simple interest is : "<<i;
 
      getch();
      return 0;
}

C++(Calculate theTemperature)

4- 
Write a program which accept temperature in Farenheit and print it in centigrade. 

#include<iostream.h>
#include<conio.h>
 
int main()
{
      float F,C;
      cout<< "\nEnter temperature in Farenheit : ";
      cin>>F;
      C=5*(F-32)/9;
      cout<<"Temperature in celcius is : "<<C;
 
      getch();
      return 0;
}