write a program to input a number having three or four digits halve it and find the sum of the individual halves and check whether they are equal or not
Answers
Answer: I will use the most basic programming language Scratch to do this. Other programming languages are quite similar.
when green flag clicked
ask [number?] and wait
set [count] to (0)
delete (all) of [list]
repeat (length of (answer)) {loop start}
change [count] by (1)
add ((digit (count) of (answer)/2)) to [list] {loop end}
set [sum] to (0)
repeat (length of [list]) {loop start}
change [sum] by (item (1) of [list])
delete item (1) of [list] {loop end}
{script end}
Program: {JAVA}
import java.util.*;
public class Brainly
{
static void main()
{
Scanner sc=new Scanner(System.in);
int n,f,l,s;
System.out.print("Enter an integer: ");
n=sc.nextInt();
if(n>=1000 && n<=9999)
{
f=n/100; //first 2 digits
l=n%100; //last 2 digits
s=f+l;
System.out.println("Sum of the 2 halves:"+s);
}
else
System.out.println("Not a 4-digit number");
}
}