In: Computer Science
Must make a "Calculator" using C. The calculator must subtract, add, divide, and multiply inputs, and tell whether a number is prime or not. The user chooses how many inputs go into the calculator.
For example, the code will ask the user what function they want. If the user chooses to subtract, the the code will then ask the user how many numbers they want to subtract. After, the code will ask the user to input as many numbers as they chose in the last step. Lastly, the code must subtract the first input - the second input - the third input and so on, and must print the answer.
In case of any queries, please revert back.
I have attached the screenshots below the code:-
#include <stdio.h>
#include <math.h>
int main(){
while(1){
int choice;
printf("Please choose the operation. \n 1.Addition\n
2.subtraction\n 3.multiplication\n 4.division\n 5.Prime\n
6.End\n");
scanf("%d",&choice);
if(choice==6){
break;
}
else if(choice==1){
printf("Enter number of numbers to
add. \n");
int n;
scanf("%d",&n);
int result=0;
for(int i=0;i<n;i++){
printf("Enter
the %d number ",i+1);
int temp;
scanf("%d",&temp);
result=result+
temp;
}
printf("Added Result is
%d\n",result);
}
else if(choice==2){
printf("Enter number of numbers to
subtract. \n");
int n;
scanf("%d",&n);
int result=0;
for(int i=0;i<n;i++){
printf("Enter
the %d number ",i+1);
int temp;
scanf("%d",&temp);
if(i==0){
result=temp;
}
else{
result=result- temp;
}
}
printf("Subtracted Result is
%d\n",result);
}
else if(choice==3){
printf("Enter number of numbers to
multiply. \n");
int n;
scanf("%d",&n);
int result=1;
for(int i=0;i<n;i++){
printf("Enter
the %d number ",i+1);
int temp;
scanf("%d",&temp);
result=result*
temp;
}
printf("Mulitiplied Result is
%d\n",result);
}
else if(choice==4){
printf("Enter number of numbers to
Divide. \n");
int n;
scanf("%d",&n);
int result;
for(int i=0;i<n;i++){
printf("Enter
the %d number ",i+1);
int temp;
scanf("%d",&temp);
if(i==0){
result=temp;
}
else{
result=result/
temp;
}
}
printf("Divided Result is
%d\n",result);
}
else if(choice==5){
printf("Enter number \n");
int n,flag=1;
scanf("%d", &n);
for (int i = 2; i <= n / 2; i++)
{
if (n % i == 0)
{
flag = 0;
break;
}
}
if (flag == 1) {
printf("%d is a
prime number\n", n);
}
else {
printf("%d is
not a prime number\n", n);
}
}
}
return 0;
}
Screenshots:-
Activities Terminal Wed 01:00 ujjwalshadow:--/Documents/cprogs File Edit View Search Terminal Help ujjwal shadow:-/Documents/cprogs$ QCC -0 output calc.c ujjwalashadow /Documents/cprogs$ ./output Please choose the operation. 1. Addition 2. subtraction 3.nultiplication 4.division 5. Prime 6. End Enter nunber of nunbers to multiply. Enter the 1 number 58 Enter the 2 nunber 19 Enter the 3 number 10 Mulltiplled Result is 5008 Please choose the operation. 1. Addition 2. subtraction 3.multiplication 4.diviston 5. Prime 6. End Enter number of numbers to Divide. Enter the 1 number 5880 Enter the 2 number 18 Enter the 3 number 183 Divided Result is 5 Please choose the operation. 1. Addition 2. subtraction 3.nultiplication 4.division 5. Prime 6. End SOC Enter nunber 20 is not a prime number please choose the operation. 1. Addition 2. subtraction 3.multiplication 4.diviston 5. Prime 6. End ujjwalashadow:-/Documents/cprogs$
Activities Geany Wed 01:02 calc.c- /home/u]wal/Documents/cprogs - Geany File Edit Search View Document Project Build Tools Help 3sort.cpp * calcc X include <stdio.h> include chath.h> ant main whilellt int choice; printf("Please choose the operation 11 1. Addi Lionn 2.subiraclioni 3.multiplicaliun. 4.divisionin 5. Prime 6. End\n"); scanf("sd", schoice); if (choices break else if i choice printf("Enter number of numbers to add. In '); int n; scanf(" , ); int result ; forint 1-3;i<n;i++){ printf("Enter the d number 1-1); int temp; scanf( ", lemp): result result temp: printf("Added Result is dyn", result); else if choicce=2) printf("Enter number of numbers to subtract. In); int n; scant( '.n); int result-o; forint 1-3:16:14+ printf("Enter the nurber 1+11: int temp; scanf( ster): result-result. temp: printf("Subtracted Result is dyn", result); else if (choice=3){ printf "Enter number of numbers to multiply.In); int n; scanf("d",6); Status 00:557041 File /home/ u s /progs/calcit saved Come 00:55:20: File /home/ujjual/Documents/cproas/calc. saved. Messages 09:56:18: File /homerujual/Documents/cprogs/calc.c saved. 19:58:35: File /home/jjal/Documents/cprogs/calc. saved. Scribble 3:59:18: File /home/ujjual/Documents/progs/calc. saved Terminal 10 : File home l /UCuments/progs/calc. saved line: 26/96 col: 46sel: O INSTAB mode:LF encoding: UTF-Filetype: C scape: main
Activities Geany Wed 01:02 calc.c- /home/u]wal/Documents/cprogs - Geany File Edit Search View Document Project Build Tools Help 3sort.cpp * calcc * else ifichoice-41 printf("Enter nunber of numbers to Divide. \n"); int n; scanf("d",& ; int result; for(int i=0;ien: i++){ printr('Enter the number 1+1): int tep: scanf"d", terp): if(i= ) resultatetp: else result-result/ top: printf("Divided Result 15 vin' result); a else ifichoice--511 printf("Enter number n'); int n, flag=1; scand ", &11): A n / 2; i++) { for (int i = 2; i if in i = flag = 0; ) it flag - 11 printf('\ 1s a prime number\n', n); else { printf("ud is not a prin number : return ; Status00155:04: File fhome/ujjal/UCNs/pr /Calcit saved Come 00:55:20: File /home/ujjual/Documents/cproas/calc. saved. Messages 08:56:18: File /home/ujjal/Documents/cprogs/calc.c saved. 19:58:35: File /home/jjal/Documents/cprogs/calc. saved. Scribble 13:59:18: File /home/ujjual/Documents/eprogs/calc. saved. Terminal 10 : File /home /cuments/progs/calc. saved. line:26/96 col: 40 sel: DINS TAB mode:LF encoding: UTF-8 Filetype: C scape: main