Programming Assignment

Tasty foods
0
                                 
Programming Assignment 



   **Assignment # 5**

                   -------------(1)---------------

Write a C++ program that takes a positive integer as input and prints the last digit of the number using the modulus operator.
Answer:

#include <iostream>
using namespace std;
int main() {
    int a;
    cout << "Enter positive integer: ";
    cin >> a;
    if (a > 0) {
        int lastDigit = a % 10;
        cout << " last digit of the number is: " << lastDigit << endl;
    }
     else 
        cout << " enter positive integer." << endl;
    return 0;
}

            -------------------(2)----------------------

Write a C++ program that calculates the sum of digits of a positive integer using the modulus operator ( *%* ).
Input: A positive integer n (e.g., n = 12345) 
Output: The sum of the digits of n (e.g., 15)
Answer:
#include <iostream>
using namespace std;

int main() {
    int a;
    cout << "Enter positive integer: ";
    cin >> a;
    if (a <= 0) {
        cout << " Enter positive integer." << endl;
    }
    int sum = 0;
    while (a > 0)
     {
        sum += a % 10; 
        a /= 10; 
    }
    cout << " sum of the digits is: " << sum;

    return 0;
}

                      --------------(3)-------------
Write a C++ program that initializes two variables, a and b, with values 5 and 10 respectively. Then, calculate the result of the expression (a++ + --b) and display the result. Finally, display the updated values of a and b.
Input: None
Output: The result of (a++ + --b) and the updated values of a and b.
Answer:
#include <iostream>
using namespace std;

int main() {
    int a = 5, b = 10 , result ;
     result = (a++ + --b);
    cout << "Result of (a++ + --b): " << result << endl;
    cout << "Updated value of a: " << a << endl;
    cout << "Updated value of b: " << b << endl;

    return 0;
}
----------------------------------------
output==>>
Result of (a++ + --b): 14
Updated value of a: 6
Updated value of b: 9

Tags
  • Newer

    Programming Assignment

  • Older

    Programming Assignment

Post a Comment

0Comments

Post a Comment (0)