Week 1 Programming Questions

Note: Try to solve these questions on your own before checking the solutions.

Filter:
Sort:

Question 1

Write a program that takes two integers as input from the user and performs the following operations:

  1. Addition
  2. Subtraction
  3. Multiplication
  4. Division
  5. Modulus

The program should then print the results of each operation in a formatted manner.

Note: The solution does not handle division by zero as conditional statements have not been covered yet.

Example:

Enter first integer: 10
Enter second integer: 3
Addition: 10 + 3 = 13
Subtraction: 10 - 3 = 7
Multiplication: 10 * 3 = 30
Division: 10 / 3 = 3
Modulus: 10 % 3 = 1
Solution
#include <stdio.h>

int main() {
    int num1, num2;

    printf("Enter first integer: ");
    scanf("%d", &num1);

    printf("Enter second integer: ");
    scanf("%d", &num2);

    printf("Addition: %d + %d = %d\n", num1, num2, num1 + num2);
    printf("Subtraction: %d - %d = %d\n", num1, num2, num1 - num2);
    printf("Multiplication: %d * %d = %d\n", num1, num2, num1 * num2);
    printf("Division: %d / %d = %d\n", num1, num2, num1 / num2);
    printf("Modulus: %d %% %d = %d\n", num1, num2, num1 % num2);

    return 0;
}

                        
#include <iostream>
using namespace std;

int main() {
    int num1, num2;

    cout << "Enter first integer: ";
    cin >> num1;

    cout << "Enter second integer: ";
    cin >> num2;

    cout << "Addition: " << num1 << " + " << num2 << " = " << num1 + num2 << endl;
    cout << "Subtraction: " << num1 << " - " << num2 << " = " << num1 - num2 << endl;
    cout << "Multiplication: " << num1 << " * " << num2 << " = " << num1 * num2 << endl;
    cout << "Division: " << num1 << " / " << num2 << " = " << num1 / num2 << endl;
    cout << "Modulus: " << num1 << " % " << num2 << " = " << num1 % num2 << endl;

    return 0;
}

                        

Question 2

Write a program that takes a character input from the user, an integer input, and a float input. The program should then print the ASCII value of the character, the square of the integer, and the cube of the float in a formatted manner.

Example:

Enter a character: A
Enter an integer: 4
Enter a float: 2.5
The ASCII value of 'A' is 65
The square of 4 is 16
The cube of 2.50 is 15.62
Solution
#include <stdio.h>

int main() {
    char ch;
    int integer;
    float decimal;

    printf("Enter a character: ");
    scanf(" %c", &ch);

    printf("Enter an integer: ");
    scanf("%d", &integer);

    printf("Enter a float: ");
    scanf("%f", &decimal);

    printf("The ASCII value of '%c' is %d\n", ch, ch);
    printf("The square of %d is %d\n", integer, integer * integer);
    printf("The cube of %.2f is %.2f\n", decimal, decimal * decimal * decimal);

    return 0;
}

                        
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    char ch;
    int integer;
    float decimal;

    cout << "Enter a character: ";
    cin >> ch;

    cout << "Enter an integer: ";
    cin >> integer;

    cout << "Enter a float: ";
    cin >> decimal;

    cout << "The ASCII value of '" << ch << "' is " << (int)ch << endl;
    cout << "The square of " << integer << " is " << integer * integer << endl;
    cout << "The cube of " << fixed << setprecision(2) << decimal << " is " << decimal * decimal * decimal << endl;

    return 0;
}

                        

Question 3

Write a program that reads a 4-digit integer and prints the decimal value for each digit.

Example:

please enter number of 4 digits
5678
5678=5000+600+70+8
Solution
#include <stdio.h>

int main() {
  int n;
  int d1,d2,d3,d4;
  printf("please enter number of 4 digits\n");
  scanf("%d",&n);
  d1=n%10;
  d2=(n/10)%10;
  d3=((n/10)/10)%10;
  d4=((n/10)/10/10%10);
  printf("%d=%d+%d+%d+%d\n",n,d4*1000,d3*100,d2*10,d1);
  return 0;
}

                        
#include <iostream>
using namespace std;

int main() {
  int n;
  int d1, d2, d3, d4;
  
  cout << "please enter number of 4 digits\n";
  cin >> n;
  
  d1 = n % 10;
  d2 = (n / 10) % 10;
  d3 = ((n / 10) / 10) % 10;
  d4 = ((n / 10) / 10 / 10) % 10;
  
  cout << n << "=" << d4 * 1000 << "+" << d3 * 100 << "+" << d2 * 10 << "+" << d1 << endl;
  
  return 0;
}

                        

Question 4

Write a program that reads a date (year, month, and day) and calculates the total number of days from year 0 up to that date.

Note: Assume a year has 360 days and a month has 30 days.

Example:

Enter the year: 2023
Enter the month (1-12): 1
Enter the day (1-31): 25
Total number of days from year 0 is: 728305
Solution
#include <stdio.h>

int main() {
    int year, month, day;
    int totalDays;

    printf("Enter the year: ");
    scanf("%d", &year);

    printf("Enter the month (1-12): ");
    scanf("%d", &month);

    printf("Enter the day (1-31): ");
    scanf("%d", &day);

    totalDays = (year * 360) + ((month - 1) * 30) + day;

    printf("Total number of days from year 0 is: %d\n", totalDays);

    return 0;
}

                        
#include <iostream>
using namespace std;

int main() {
    int year, month, day;
    int totalDays;

    cout << "Enter the year: ";
    cin >> year;

    cout << "Enter the month (1-12): ";
    cin >> month;

    cout << "Enter the day (1-31): ";
    cin >> day;

    totalDays = (year * 360) + ((month - 1) * 30) + day;

    cout << "Total number of days from year 0 is: " << totalDays << endl;

    return 0;
}

                        

Question 5

Write a program that takes three integers as input, calculates their average, and prints the result as a float with 2 decimal places.

Example:

Enter the first integer: 10
Enter the second integer: 20
Enter the third integer: 34
The average is: 21.33
Solution
#include <stdio.h>
 
int main() {
    int num1, num2, num3;
    float average;
 
    // Input three integers
    printf("Enter the first integer: ");
    scanf("%d", &num1);
    
    printf("Enter the second integer: ");
    scanf("%d", &num2);
    
    printf("Enter the third integer: ");
    scanf("%d", &num3);
 
    // Calculate the average
    average = (float)(num1 + num2 + num3) / 3;
 
    // Print the average as a float
    printf("The average is: %.2f\n", average); // %.2f to display 2 decimal places
 
    return 0;
}

                        
#include <iostream>
#include <iomanip>
using namespace std;
 
int main() {
    int num1, num2, num3;
    float average;
 
    // Input three integers
    cout << "Enter the first integer: ";
    cin >> num1;
    
    cout << "Enter the second integer: ";
    cin >> num2;
    
    cout << "Enter the third integer: ";
    cin >> num3;
 
    // Calculate the average
    average = (float)(num1 + num2 + num3) / 3;
 
    // Print the average as a float
    cout << "The average is: " << fixed << setprecision(2) << average << endl; // setprecision(2) to display 2 decimal places
 
    return 0;
}

                        

Question 6

Write a program that takes an integer representing an amount of money in dollars input from the user and breaks it down into the minimum number of bills of $100, $20, $5, and $1.

Example:

Enter amount in dollars: 288
$100 bills: 2
$20 bills: 4
$5 bills: 1
$1 bills: 3
Solution
#include <stdio.h>

int main() {
    int amount;

    
    printf("Enter amount in dollars: ");
    scanf("%d", &amount);

    
    int hundreds = amount / 100;
    amount %= 100;

    int twenties = amount / 20;
    amount %= 20;

    int fives = amount / 5;
    amount %= 5;

    int ones = amount;

    printf("$100 bills: %d\n", hundreds);
    printf("$20 bills: %d\n", twenties);
    printf("$5 bills: %d\n", fives);
    printf("$1 bills: %d\n", ones);

    return 0;
}

                        
#include <iostream>
using namespace std;

int main() {
    int amount;

    
    cout << "Enter amount in dollars: ";
    cin >> amount;

    
    int hundreds = amount / 100;
    amount %= 100;

    int twenties = amount / 20;
    amount %= 20;

    int fives = amount / 5;
    amount %= 5;

    int ones = amount;

    
    cout << "$100 bills: " << hundreds << endl;
    cout << "$20 bills: " << twenties << endl;
         cout << "$5 bills: " << fives << endl;
    cout << "$1 bills: " << ones << endl;

    return 0;
}

                        

Question 7

Write a program that takes a positive floating-point number input from the user and rounds it to the nearest integer.

Note: You are not allowed to use any functions from <math.h> (like round(), floor(), ceil()) or conditional statements.

Example:

Enter a floating-point number: 5.5
Rounded value: 6
Solution
#include <stdio.h>

int main() {
    float num;

    
    printf("Enter a floating-point number: ");
    scanf("%f", &num);

    
    int rounded = (int)(num + 0.5f);

    
    printf("Rounded value: %d\n", rounded);

    return 0;
}

                        
#include <iostream>
using namespace std;

int main() {
    float num;

    cout << "Enter a floating-point number: ";
    cin >> num;

    
    int rounded = (int)(num + 0.5f);

    
    cout << "Rounded value: " << rounded << endl;

    return 0;
}

                        

Question 8

Write a program that reads two integers from the user into two variables, swaps their values without using any additional or temporary variables, and prints the swapped values.

Example:

Enter first integer (A): 10
Enter second integer (B): 20
After swapping: A = 20, B = 10
Solution
#include <stdio.h>

int main() {
    int a, b;

    
    printf("Enter first integer (A): ");
    scanf("%d", &a);

    printf("Enter second integer (B): ");
    scanf("%d", &b);

    
    a = a + b;
    b = a - b;
    a = a - b;

    
    printf("After swapping: A = %d, B = %d\n", a, b);

    return 0;
}

                        
#include <iostream>
using namespace std;

int main() {
    int a, b;

    
    cout << "Enter first integer (A): ";
    cin >> a;

    cout << "Enter second integer (B): ";
    cin >> b;

    
    a = a + b;
    b = a - b;
    a = a - b;

    
    cout << "After swapping: A = " << a << ", B = " << b << endl;

    return 0;
}