C program to accept two integers and perform following operation (+ - * /) - IProgramX

Q. Write a program, which accepts two integers and an operator as a character (+ - * /), performs the corresponding operation and displays the result



Program

#include<stdio.h>
int main()
{
  int a,b,res;
  char c;

  printf("\n Enter any one operator +, -, *, / :");
  scanf("%c",&c);
  switch(c)
  {
    case '+':
printf("\n Enter two numbers: ");
    scanf("%d%d",&a,&b);
res=a+b;
    printf("\n The sum is %d",res);
    break;
    case '-': 
printf("\n Enter two numbers: ");
    scanf("%d%d",&a,&b);
res=a-b;
    printf("\n The difference is %d",res);
    break;
    case '*':
printf("\n Enter two numbers: ");
    scanf("%d%d",&a,&b);
    res=a*b;
    printf("\n The product is %d",res);
    break;
    case '/':
printf("\n Enter two numbers: ");
    scanf("%d%d",&a,&b);
    res=a/b;
    printf("\n The quotient is %d",res);
    break;
    default: printf ("\n Invalid entry");
  }
}

Output:

 Enter any one operator +, -, *, / :*

 Enter two numbers: 6 4

 The product is 24

Post a Comment

2 Comments

  1. 6

    SQL



    CREATE DATABASE student_db;

    USE student_db;

    CREATE TABLE U_register(
    id INT PRIMARY KEY,
    name VARCHAR(50),
    email VARCHAR(100),
    password VARCHAR(50)
    );




    6 registration.jsp





    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%

    <html
    <body

    <form action="save.jsp" method="post"

    ID
    <input type="number" name="id"$<br<br

    Name
    <input type="text" name="name"$<br<br

    Email
    <input type="email" name="email"$<br<br

    Password
    <input type="password" name="password"$<br<br

    <input type="submit" value="Submit"

    </form

    </body
    </html






    6 save.jsp





    <%@ page import="java.sql.*" %$

    <%
    String id=request.getParameter("id");
    String name=request.getParameter("name");
    String email=request.getParameter("email");
    String password=request.getParameter("password");

    try{

    Class.forName("com.mysql.cj.jdbc.Driver");

    Connection con=
    DriverManager.getConnection(
    "jdbc:mysql://localhost:3306/student_db",
    "root",
    "root");

    PreparedStatement ps=
    con.prepareStatement(
    "INSERT INTO U_register(id,name,email,password) VALUES(?,?,?,?)");

    ps.setString(1,id);
    ps.setString(2,name);
    ps.setString(3,email);
    ps.setString(4,password);

    int i=ps.executeUpdate();

    if(i$0){
    out.println("<h3Registration Successful!</h3");
    }else{
    out.println("<h3Registration Failed!</h3");
    }

    }catch(Exception e){
    out.println(e);
    }
    %$

    <br
    <a href="display.jsp"$View Entry</a







    6 display.jsp





    <%@ page import="java.sql.*" %

    <html
    <body

    <h2Registered Users</h2

    <table border="1"

    <tr
    <thID</th
    <thName</th
    <thEmail</th
    <thPassword</th
    </tr

    <%
    try{

    Class.forName("com.mysql.cj.jdbc.Driver");

    Connection con=
    DriverManager.getConnection(
    "jdbc:mysql://localhost:3306/student_db",
    "root",
    "root");

    Statement st=con.createStatement();

    ResultSet rs=
    st.executeQuery("SELECT * FROM U_register");

    while(rs.next()){
    %$

    <tr
    <td<%= rs.getInt("id") %$</td
    <td<%= rs.getString("name") %$</td
    <td<%= rs.getString("email") %$</td
    <td<%= rs.getString("password") %$</td
    </tr

    <%
    }

    con.close();

    }catch(Exception e){
    out.println(e);
    }
    %$

    </table

    </body
    </html

    ReplyDelete
  2. Download MySQL Connector J.

    You need the JDBC jar from the official MySQL site:

    MySQL Connector/J Downloads

    Download the ZIP and extract it.

    Add jar in Eclipse
    Right click Practical6
    Click Properties
    Click Java Build Path
    Click Libraries
    Click Classpath
    Click Add External JARs
    Select:
    mysql-connector-j-xxx.jar
    Click Apply
    Click Apply and Close

    ReplyDelete