Write a program to accept a set of any 10 characters in an array. Calculate and print the sum of
ASCII codes of the characters.
Answers
Question:-
Write a program to accept a set of any ten characters in an array. Calculate and print the sum of ASCII values of the character.
Code:-
import java.util.*;
class Characters
{
public static void main(String s[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the characters....");
char ch[]=new char[10];
int sum=0;
for(int i=0;i<=9;i++)
{
System.out.print("Enter: ");
ch[i]=sc.next().charAt(0);
sum+=(int)ch[i];
}
System.out.println("Sum of ASCII values is: "+s);
}
}
import java.util.Scanner;
public class ASCII_Values {
public static void main(String[ ] args) {
char[ ] array = new char[10];
int sum = 0;
System.out.println("Enter 10 characters - ");
for (int i = 0; i < 10; i++) {
array[i] = new Scanner(System.in).next( ).charAt(0);
sum += array[i];
}
System.out.println("Sum of ASCII codes - " + sum);
}
}