Numerology Program in java

Question:

Write a program to find the numerological value for a given name.
Note: Store the numerological number and the corresponding character in a 2-D array(2*26). Always the given name should be in a capital case,else the name is not valid. Check for the valid name, if the name is invalid print the message “Invalid name”.There should not be any space in the name provided.
For example:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
1 2 3 4 5 8 3 5 1 1 2 3 4 5 7 8 1 2 3 4 6 6 6 5 1 7

Sample Input 1:
Enter your name:
SUDHA

Sample Output 1:

Your numerology no is:19

Sample Input 2:
Enter your name:
kiran

Sample Output 2:

Invalid name

Sample Input 3:
Enter your name:
ANI34

Sample Output 3:

Invalid name

CODE:

import java.util.*;
public class Numerology 
{
    char[][] num_val={{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H','I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'},
                    {1, 2, 3, 4, 5, 8, 3, 5, 1, 1, 2, 3, 4, 5, 7, 8, 1, 2, 3, 4, 6, 6, 6, 5, 1, 7}};
                    
    public char numerological_value(char chr)
    {
        for(int i=0;i<26;i++)
        {
            if(chr==(num_val[0][i]))
            {
                return num_val[1][i];
            }
        }
        return 0;
    }
    
    public static void main (String[] args) {
        Scanner sc=new Scanner(System.in);
        Numerology obj=new Numerology();
        
        System.out.println("Enter your name:");
        String name=sc.nextLine();
        
        int len=name.length();
        int flag=0, val=0;
        
        for(int i=0; i<len;i++)
        {  
            
            char r=name.charAt(i);
            int temp=(obj.numerological_value(r));
            if(temp==0)
            {
                System.out.println("Invalid name");
                flag++;
                break;
            }
            else
            {
                val+=(int)temp;
            }
        }
        if(flag==0)
        {
            System.out.println("Your numerology no is:"+val);
        }
    }
}
public class Student 
{
    private int id;
    private String name;
    private int[] marks;
    private float average;
    private char grade;
    
    public void setId(int id)
    {
        this.id=id;
    }
    
    public int getId()
    {
        return this.id;
    }
    
    public void setName(String name)
    {
        this.name=name;
    }
    
    public String getName()
    {
        return this.name;
    }
    
    public void setMarks(int[] marks)
    {
        this.marks=marks;
    }
    
    public int[] getMarks()
    {
        return this.marks;
    }
    
    public void calculateAvg()
    {   float sum=0;
        
        for(int i=0;i<this.marks.length;i++)
        {
            sum+=this.marks[i];
        }
        
        average=(float)(sum/(this.marks.length));
        setAverage(average);
    }
    
    public void findGrade()
    {int flag=0;
        for(int i=0;i<this.marks.length;i++)
        {
            if(this.marks[i]<50)
            {
                flag++;
            }
        }
        if(flag>0)
        {
            grade='F';
        }
        else
        {
            if(this.average>=80 && this.average<=100)
            {
                grade='O';
            }
            else if(this.average>=50 && this.average<=79)
            {
                grade='A';
            }
            else
            {
                grade='F';
            }
        }
        setGrade(grade);
        
    }
    
    public void setAverage(float average)
    {
        this.average=average;
    }
    
    public void setGrade(char grade)
    {
        this.grade=grade;
    }
    
    public float getAverage()
    {
        return this.average;
    }
    
    public char getGrade()
    {
        return this.grade;
    }
}
Next:
  1. InitCap
  2. Array Compatiblilty
  3. Sum of the maximum and the minimum element
  4. String Concatenation

Leave a Comment

Scroll to Top