Java Program to Print Sum of given Array Elements

In previous  post we learnt how to check given element is present in array or not. In this post we will learn how to print sum of given array elements. So let's get started... :)


  Step by step logic of the given program:


1. Accept array limit from user store it in variable say lim.

2. Accept elements from user. Run a for loop from 0 to arr.length-1 and store elements one by one in array:

Java Program to Check whether Given Element is Present in Array or not

In previous tutorial we learnt how to accept and print array elements. In this post we will learn is given element is present in array or not, Java program to check whether given element is present in array or not.

Factorial Program In Java

Factorial Program in Java:

Factorial of a number means the product of all positive descending integers. 
for example: If we want to calculate factorial of 5 then it will be written as 
5 x 4 x 3 x 2 x 1 = 120 . So 120 is the factorial of  5.
  

  Program to Print Factorial of a given number:

Fibonacci Series Program In Java

Fibonacci Series In Java:

The fibonacci series is a series where the next number is the sum of previous two numbers.  
for examples:  0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 etc

above you can see first two numbers of fibonacci series are 0 and 1.

Java program to calculate gross salary of an employee

Ex: Write a java program to calculate gross salary of an employee. How to write a java program to calculate gross salary of an employee using DA and HRA. Java program to calculate gross salary of an employee using DA and HRA.  

We can calculate gross salary   of an  employee using following DA and HRA. The  DA is   20%   of   the   basic   salary   while   the   HRA   is   30%   of   the   basic salary and gross salary is addition of basic salary, DA and HRA.

Java Program to check Given Number is Prime or not

EX: Write a Java Program to check given number is prime or not.


  What is prime number?

Prime number is a number that is greater than 1 and divided by 1 and itself only.
For example: 2, 3, 5, 7, 11, 13, 17, 19, 23... etc. are prime numbers.

Java Program to check Given Number is Palindrome or not

EX: Write a java program to check given number is palindrome or not. How to write a java program to check palindrome number.


  What is Palindrome Number?

A palindrome number is a number that is same after reverse. For example: 252, 676, 77577, 1234321 etc.

Basic Java Programming Examples and Solutions

In previous post we learnt what is java programming  or introduction of java programming, In this article you will learn basic of java programming with lot of  java programming exercises, examples and their solutions.

Top Programming Interview Questions in C

In this article I will share some most commonly asked interview questions for you.

There are lot of computer science students, engineering students and programmers applying for interview to get job and start their career in software industries.

There are many big companies like Microsoft, Google, Amazon, Facebook and many other big organizations hires new software developers and programmers every year.

C Program To Display User Details Using ID

EX: Write a C program to display user details using id. How to write a C program to display user details. C program to display student details using their ID.

In this article, we  will learn how to write a C program that displays user details based on a provided ID. In this program, we will use a switch case to print user details... But you can also do this program using if-else-if statements.


   Here's a step-by-step guide for creating this program:

Step 1: Include the Standard Input-Output Header

First, include the standard input-output library, which allows us to use functions like printf and scanf for input and output operations.

#include<stdio.h>

Step 2: Start the main Function

Every C program starts executing from the main function. This is where we'll write the logic of our program.

int main() {

Step 3: Declare an Integer Variable

We'll declare an integer variable id to store the ID entered by the user.

int id;

Step 4: Accept User Input

Prompt the user to enter their ID using printf and store the input in the id variable using scanf.

// Accept id from user
printf("Enter your id: ");
scanf("%d", &id);

Step 5: Use a switch Statement to Match the ID

The switch statement allows us to execute different code blocks based on the value of the id. We'll check for specific IDs and print the corresponding user details.

//Switch the value of id
switch(id) {

Step 6: Define Cases for Specific IDs

For each case, we'll match a specific ID and print the details for that user. If the ID doesn't match any case, the default case will prompt the user to enter a valid ID.

// Match id with cases
        case 11:
            printf("Information of id %d is:\n", id);
            printf("Name: Omkar Lokhande\nContact: 123456***\nAdd: Kalamb\n");
            break;

        case 12:
            printf("Information of id %d is:\n", id);
            printf("Name: Akash Sutar\nContact: 1234***\nAdd: Kalamb\n");
            break;

        case 13:
            printf("Information of id %d is:\n", id);
            printf("Name: Rusty Shackleford\nContact: 12456***\nAdd: California\n");
            break;

        default:
            printf("Please Enter Valid Id\n");   
    }

Step 7: End the main Function

Finally, we'll return 0 to indicate that the program has executed successfully.

return 0;
}


  C program to display user details using id :

Here, is the complete program to display user details using id.

#include<stdio.h>

int main()
{
    int id;
    //Accept id from user
    printf("Enter your id: ");
    scanf("%d",&id);
    //Switch the value of id
    switch(id) {
        //Match id with cases
        case 11:
        printf("Information of id %d is:\n",id);
        printf("Name: Omkar Lokhande\nContact: 123456***\nAdd: kalamb");
        break;

        case 12:
        printf("Information of id %d is:\n",id);
        printf("Name: Akash Sutar\nContact: 1234***\nAdd: kalamb");
        break;

        case 13:
        printf("Information of id %d is:\n",id);
        printf("Name: Rusty Shackleford \nContact: 12456***\nAdd: California");
        break;

        default:
        printf("Please Enter Valid Id");   
    }
    return 0;
}
Above program will show the following output:

Enter your id: 13
Information of id 13 is:
Name: Rusty shackleford
Contact: 123456***
Add: California


Share your thoughts in the comment section given below...  :)