FYBCS C Exercise_5

Set A . Write C programs for the following problems. 

 1. Write a program to display all prime numbers between ____ and ____.

 2. Write a program to display multiplication tables from ___ to ___ having n multiples each. The output should be displayed in a tabular format. For example, the multiplication tables of 2 to 9 having 10 multiples each is shown below.  
2 × 1 = 2     3 × 1 = 3 ………….9 × 1 = 9  
2 × 2 = 4     3 × 2 = 6…………..9 × 2 = 18
 …………. …………. ................................. 
2 × 10 = 20 3 × 10 = 30………..9 × 10 = 90  

 3. Modify the sample program 1 to display n lines as follows (here n=4).             
 A      B      C      D              
E      F      G              
H      I              


Set B. Write C programs for the following problems. 

 1. Write a program to display all Armstrong numbers between 1 and 500. (An Armstrong number is a number such that the sum of cube of digits = number itself Ex. 153 = 1*1*1 + 5*5*5+ 3*3*3 

 2. Accept n numbers and display the number having the maximum sum of digits

 3.Display all perfect numbers below 500[A perfect number is a number, such that the sum of its factors is equal to the number itself]. Example: 6 (1 + 2 + 3), 28 (1+2+4+7+14)  

Post a Comment

4 Comments

  1. class BankAccount:
    def __init__(self, owner, balance=0):
    self.owner = owner
    self.balance = balance

    def deposit(self, amount):
    self.balance = self.balance + amount
    print("Deposited",amount , "New balance:", self.balance)

    def withdraw(self, amount):
    self.balance = self.balance - amount
    print("Withdrew", amount, "New balance: ",self.balance)

    def check_balance(self):
    print("Current balance: ", self.balance)
    return self.balance

    account = BankAccount("Alice", 100)
    account.check_balance()
    account.deposit(50)
    account.withdraw(30)
    account.withdraw(200)
    account.check_balance()

    ReplyDelete
  2. # Program to perform division with user input and exception handling
    while(True):
    try:
    numerator = float(input("Enter the numerator: "))
    denominator = float(input("Enter the denominator: "))
    result = numerator / denominator

    except ValueError:
    print("Error: Please enter valid numbers only.")

    except ZeroDivisionError:
    print("Error: Division by zero is not allowed.")

    else:
    print(f"The result of division is: {result:.2f}")

    finally:
    print("Program execution completed. \n")

    ReplyDelete
  3. # Program to perform division with user input and exception handling
    class NegativeDenominatorError(Exception):
    """Custom exception raised when the denominator is negative."""
    pass
    while(True):
    try:
    numerator = float(input("Enter the numerator: "))
    denominator = float(input("Enter the denominator: "))
    if denominator <= 0:
    raise NegativeDenominatorError("Error: Denominator cannot be negative! and cannot be zero itself")
    result = numerator / denominator

    except NegativeDenominatorError as e:
    print(e)

    else:
    print(f"The result of division is: {result:.2f}")

    finally:
    print("Program execution completed. \n")

    ReplyDelete
  4. #include
    using namespace std;

    // Define the structure for a BST node
    struct Node {
    int data;
    Node* left;
    Node* right;
    Node(int val) : data(val), left(nullptr), right(nullptr) {}
    };

    // Function to insert a value into the BST
    Node* insert(Node* root, int val) {
    // If tree is empty, create a new node
    if (root == nullptr) {
    return new Node(val);
    }

    // Insert into left or right subtree based on value
    if (val < root->data) {
    root->left = insert(root->left, val);
    }
    else if (val > root->data) {
    root->right = insert(root->right, val);
    }
    // If val == root->data, we skip insertion (no duplicates in BST)

    return root; // Return unchanged root pointer
    }

    // Inorder traversal to verify insertion
    void inorder(Node* root) {
    if (root != nullptr) {
    inorder(root->left);
    cout << root->data << " ";
    inorder(root->right);
    }
    }
    void preorder(Node* root) {
    if (root != nullptr) {
    cout << root->data << " ";
    preorder(root->left);
    preorder(root->right);
    }
    }

    void postorder(Node* root) {
    if (root != nullptr) {
    postorder(root->left);
    postorder(root->right);
    cout << root->data << " ";
    }
    }
    int main() {
    Node* root = nullptr;

    // Insert values into BST
    root = insert(root, 50);
    root = insert(root, 30);
    root = insert(root, 70);
    root = insert(root, 20);
    root = insert(root, 40);
    root = insert(root, 60);
    root = insert(root, 80);

    cout << "Inorder traversal of BST: ";
    inorder(root);
    cout << endl;
    cout << "Preorder traversal of BST: ";
    preorder(root);
    cout << endl;
    cout << "Postorder traversal of BST: ";
    postorder(root);
    cout << endl;


    return 0;
    }

    ReplyDelete