Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Method 1: C++ program to check if a number is

divisible by 5 and 11 by using a if-else block:


Below is the complete program:

#include <iostream>

using namespace std;

int main()
{
int no;

cout << "Enter a number: " << endl;


cin >> no;

if (no % 5 == 0 && no % 11 == 0)
{
cout << "It is divisible by 5 and 11" << endl;
}
else
{
cout << "It is not divisible by 5 and 11" <<
endl;
}
}

SIMPLE CALCULATOR

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

char op;
float num1, num2;

cout << "Enter operator: +, -, *, /: ";


cin >> op;

cout << "Enter two operands: ";


cin >> num1 >> num2;

switch(op) {

case '+':
cout << num1 << " + " << num2 << " = " << num1 + num2;
break;

case '-':
cout << num1 << " - " << num2 << " = " << num1 - num2;
break;

case '*':
cout << num1 << " * " << num2 << " = " << num1 * num2;
break;

case '/':
cout << num1 << " / " << num2 << " = " << num1 / num2;
break;

default:
// If the operator is other than +, -, * or /, error message is shown
cout << "Error! operator is not correct";
break;
}

return 0;
}

APHABETIC IS CHARACTER OR NOT

#include<iostream>
using namespace std;
int main()
{
char num;
cout<<"Enter a character:"<<endl;
cin>>num;
switch(num>='a'&& num<='z'||num>='A'&& num<='Z')
{
case 1:
cout<<"It's alphabet"<<endl;
break;
case 0:
switch(num>='0' && num<='9')
{
case 1:
cout<<"It's digit"<<endl;
break;
case 0:
cout<<"It's not alphabet and not digit"<<endl;
break;
}
break;
}
}

C++ Source Code to calculate profit or loss


– switch statement
#include<iostream>
using namespace std;
int main()
{
int purchase,sale,profit_loss;
cout<<"Enter Your Purcahsing Price : ";
cin>>purchase;
cout<<"Enter Your Selling Price : ";
cin>>sale;
switch(sale>purchase)
{
case 1:
profit_loss=sale-purchase;
cout<<"You got the Profit  and your profit is: "<<profit_loss;
break;
case 0:
profit_loss=sale-purchase;
cout<<"You got loss and Your Loss is : "<<profit_loss;
break;
}
}

Output
PROFIT AND LOSS
// C++ code to demonstrate Profit and Loss

#include <iostream>

using namespace std;

// Function to calculate Profit.

int Profit(int costPrice, int sellingPrice)

    int profit = (sellingPrice - costPrice);

    return profit;

// Function to calculate Loss.

int Loss(int costPrice, int sellingPrice)

    int Loss = (costPrice - sellingPrice);

    return Loss;

// Driver Code.
int main()

    int costPrice = 1500, sellingPrice = 2000;

    if (sellingPrice == costPrice)

        cout << "No profit nor Loss";

    else if (sellingPrice > costPrice)

        cout << Profit(costPrice, sellingPrice) << " Profit ";

    else

        cout << Loss(costPrice, sellingPrice) << " Loss ";

    return 0;

OUTPUT : 
 
500 Profit

C program to find profit and loss, given cost price and


selling price
/*
 * Given cost price and selling price, Write a
 * C program to calculate Profit or loss
 */ 
   
#include <stdio.h> 
   
int main() { 
    int costPrice, sellingPrice;
       
    /*
     * Take costPrice and SellingPrice as input from user
     */ 
    printf("Enter Cost Price and Selling Price\n"); 
    scanf("%d %d", &costPrice, &sellingPrice);
       
    if(costPrice > sellingPrice) {
        /* Loss */   
        printf("Loss = %d\n", costPrice - sellingPrice); 
    } else if(sellingPrice > costPrice) { 
        /* Profit or Gain*/ 
        printf("Profit = %d\n", sellingPrice - costPrice); 
    } else {
     /* No Profit or Loss*/
        printf("No Profit and No Loss\n"); 
    } 
   
    return 0; 
}

Program to take the hours and minutes and


the show that whether it is AM or PM by
using the switch statements
#include<iostream>
using namespace std;
int main()
{
int h,m;
cout<<"Input The time in 24 hours format in Given Sequence  "<<endl;
cout<<"input Hours : ";
cin>>h;
cout<<"Input Minutes : ";
cin>>m;
switch(h>=12 && h<=24)
{
case 1:
cout<<"The time is in PM "<<endl;
break;
case 0:
switch (h>=0 && h<=11)
{
case 1:
cout<<"Time is in AM "<<endl;
break;
case 0:
cout<<"Input is Wrong  "<<endl;
break;
}
break;
}
}

You might also like