Oct 28, 2021

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:



import java.util.Scanner;

public class Factorial {

	public static void main(String[] args) {
		
		int num, fact=1;
		
		Scanner sc = new Scanner(System.in);
		
		//Accepting input
		System.out.println("Enter a Number: ");
		num = sc.nextInt();
		
		for(int i=num; i>=1; i--) {
			fact = fact*i;
		}
		System.out.println("Factorial Of "+num+" is: "+fact);
	}

}



  Factorial Program using while loop:



import java.util.Scanner;

public class Factorial {

	public static void main(String[] args) {
		
		int num, fact=1;
		
		Scanner sc = new Scanner(System.in);
		
		//Accepting input
		System.out.println("Enter a Number: ");
		num = sc.nextInt();
		
		int i = num;
		
		while(i>=1) {
			fact = fact*i;
			i--;
		}
		System.out.println("Factorial Of "+num+" is: "+fact);
	}

}

  

Output:

Enter a Number: 7
Factorial of 7 is: 5040


continue reading Factorial Program In Java
Share This:    Facebook Twitter

Oct 26, 2021

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 print fibonacci series:



import java.util.Scanner;

public class Fibonacci {

	public static void main(String[] args) {

		int num1 = 0, num2 = 1, num3, i, input;
		Scanner sc = new Scanner(System.in);
        
		//Getting input limit from user
		System.out.print("Enter limit: ");
		input = sc.nextInt();
		
        //printing num1 means 0 and num2 means 1
		System.out.print(num1+" "+num2);
		
        /*for loop starts from 2 because first two values
        means 0 and 1 are already printed*/
		for(i=2;i<input; i++) {
			num3 = num1+num2;
			System.out.print(" "+num3);
			num1=num2;
			num2=num3;
		}
		
		
	}
}

Above Program will show the following Output:

Enter limit: 10
0 1 1 2 3 5 8 13 21 34



  Fibonacci Series Program using while loop:



import java.util.Scanner;

public class Fibonacci {

	public static void main(String[] args) {

		int num1 = 0, num2 = 1, num3, i, input;
		Scanner sc = new Scanner(System.in);
		
        //Getting input limit from user
		System.out.print("Enter limit: ");
		input = sc.nextInt();
		
        //printing num1 means 0 and num2 means 1
		System.out.print(num1+" "+num2);
        
	/*while loop starts from 2 because first two values
        means 0 and 1 are already printed*/
		i = 2;
		while(i<input) {
		        num3 = num1+num2;
			System.out.print(" "+num3);
			num1=num2;
			num2=num3;
			i++;
		}
		
		
	}
}

Output:

Enter limit: 10
0 1 1 2 3 5 8 13 21 34



  Above Same Program using do-while loop:



import java.util.Scanner;

public class Fibonacci {

	public static void main(String[] args) {

		int num1 = 0, num2 = 1, num3, i, input;
		Scanner sc = new Scanner(System.in);
		//Getting input limit from user
		System.out.print("Enter limit: ");
		input = sc.nextInt();
		
        //printing num1 means 0 and num2 means 1
		System.out.print(num1+" "+num2);
        
		//do while loop 
		i = 2;
		do{
		    num3 = num1+num2;
			System.out.print(" "+num3);
			num1=num2;
			num2=num3;
			i++;
		}while(i<input);
		
		
	}
}

Output:

Enter limit: 10
0 1 1 2 3 5 8 13 21 34


Learn more about do-while loop in our C programming tutorials...


continue reading Fibonacci Series Program In Java
Share This:    Facebook Twitter

Oct 21, 2021

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.


Program to calculate gross salary of an employee:

import java.util.Scanner;

public class GrossSalary {


    public static void main(String args[]) {

        Scanner sc = new Scanner(System.in);

        float b;
        float da;
        float hra;
        float gross;

        System.out.println("Enter the basic salary of an employee:");
        b = sc.nextInt();

        da = (b * 20) / 100;
        hra = (b * 30) / 100;

        gross= b + da + hra;

        System.out.println("Gross salary of an employee is: " +gross);

    }
}
Above java program shows the following output:

Enter the basic salary of an employee:  15000

Gross salary of an employee is: 22500.0

continue reading Java program to calculate gross salary of an employee
Share This:    Facebook Twitter

Oct 13, 2021

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.


Program:


import java.util.*;

public class PrimeNumber {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		int count = 0,t;
		System.out.println("Enter number: ");
		int num = sc.nextInt();
		
		if(num==0||num==1) {
			System.out.println(num+" is not prime number");
		}else {
		for(int i=1; i<=num;i++) {
			if(num%i==0)
				count++;
		}
		if(count>2)
			System.out.println("Given number is not prime");
		else
			System.out.println("Given number is prime");
		}
		
	}

}

Output:

Enter number: 
11
Given number is prime



continue reading Java Program to check Given Number is Prime or not
Share This:    Facebook Twitter

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.


  Program to print Given number is palindrome or not:


import java.util.Scanner;

public class PalindromeNumber {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		int temp,r,sum=0;
        
                System.out.println("Enter the number: ");
		int num = sc.nextInt();//Getting input
		temp = num;
		
		while(num>0) {
			r = num%10;
			sum = (sum*10)+r;
			num/=10;//=>num = num/10;
		}
		
		if(sum==temp) {
			System.out.println("Given number is palindrome");	
		}else {
			System.out.println("Given number is not palindrome");
		}
	}

}

Output:

Enter the number:
 575
Given number is palindrome



continue reading Java Program to check Given Number is Palindrome or not
Share This:    Facebook Twitter

Java Programming Examples and their 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.


  Basic of Java Programming:

Quick Links:

I have assembled a list of java programs to practice for both novice as well as intermediate programmers. Practice more to enhance your logical thinking.  After this exercises you will surely gain some confidence and knowledge about  java programming.



  Basic Java Programming Exercises For Beginners:


  Arrays In Java Programming Exercises:


I hope above java examples and their solutions will help you to understand and gain java programming knowledge :)



continue reading Java Programming Examples and their Solutions
Share This:    Facebook Twitter

Jun 7, 2021

Top Programming Interview Questions in C

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

c programming questions for interview, C interview questions, programming interview questions


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. But, Problem is... we don't know what kind of questions they will ask in interview.

In this article, I will share some frequently asked programming interview questions in C and also some most commonly asked interview questions for you.

You can practice this questions because, Once you practice these questions, you will feel confident enough to attend any interview.

So without wasting more time let's get started....


  Basic Programming Interview Questions:


1. Which header file  do you need  to include to use  typecasting? 
2. What is the difference between angular braces < > and double quotes ” “ in header files in C?
3. What is variables in programming? 
4. What is scope of variables? 
5. What are the types of variables in C programming? 
6. How variables are scoped in C? 
7. How to print “Hello World” without semicolon? 
8.  What will be the output if you will compile and execute the following C code?
#include<stdio.h>
  int main()
  {
       int a=5;
       float b;
       printf(“%d”,sizeof(++a+b));
       printf(“%d”,a);

       return 0;
 }

9.  What is memory leak?
10. What is the value  return to the operating system on the successful  completion of key program?




  Array Interview Questions:


11. What is the use of arrays? Why we use arrays?
12. How do you remove  duplicates  from a given array?
13. How do you reverse an array  in java?
14. How do you find all pairs of an integer array whose sum is equal to a given number?
15. How to find repeated numbers in an array?  
16. How do you find the duplicate number on a given integer array?
17. How do you sort an array in java ?
18. How to print numbers from array?
19. How do you find the largest and smallest number in an unsorted integer array?



  String Interview Questions:


20. Why we use strings in program?
21. How do you reverse words in a given string?
22. How to print duplicate characters from a string?
23. How to check if a given string is a palindrome?
24. How to find permutaions of  a string?
25. What will be the output of the following program ?

#include<stdio.h>
  int main()
  {
       int a=5;
       float b;
       printf(“%d”,sizeof(++a+b));
       printf(“%d”,a);

       return 0;
 }


26. Which string function adds one string to end of the  another string?
27. How do you find the maximum occurring character in a given string?
28. How do you  reverse String without recursion()?



  Linked List Interview Questions:


29. What is linked list?
30. How do you  find  length of a singly linked list?
31. How to reverse a linked list?
32. How do you remove duplicate noedes from unsorted linked list?
33. How many types of Linked List exist?
34. Explain Singly Linked List?
35. Explain difference between singly and doubly linked list?
36. Explain some disadvantages of linked list?




  Other Questions:


37. Why we use functions in our program ?
38.  What is pointer?
39. What is formal parameters?
40. Can a program compiles without main()?
41. What is the advantages of structures?
42. What is a preprocessor?
43.  What is Bubble Sort Algorithm? 
44. How do you implement Bubble Sort algorithm?
45. How do you multiply two matrices in java?
46. How do you reverse given intiger in java?
47. How do you print Number is Even or Odd without using if-else Statement?
48. How do you devide the intiger  number without using division '/' sign? 
49 How do you concatenate strings using Pointers?
50. How you can swap two numbers using pointers?
51. What is NULL pointer?
52. What is the meaning of base address of the array?


These are some of the most common questions that help you well in your interview. 


Quick Links: 


If you want to learn more about C programming with lot of examples and  their solutions.... then visit C Exercises that will help you to learn C from basics to intermediate level.


continue reading Top Programming Interview Questions in C
Share This:    Facebook Twitter

May 28, 2021

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.


We use switch case in this program to print user details....


  C 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


continue reading C program to display user details using id
Share This:    Facebook Twitter

May 27, 2021

How to write a id and password validation program in C

EX: Write a C program to print message only if id and password is correct. How to write a id and password validation program in C. C program for login using id and password.

In this program we will use Nested switch statement in C. In simple words Nested switch statement means switch statement defined inside the another switch statement. 


  Id and Password validation program in C:

#include<stdio.h>
void main(){

        //Declare variables
        int id= 1010;
        int pass = 1100;

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

        switch(id) {
            //If id matches with case then accept password
            case 1010:
                printf("Enter your password:\n ");
                scanf("%d",&pass);

                switch(pass){
                    //If password matches with case then print message
                    case 1100:
                        printf("Welcome To codeforhunger.com\n");
                        break;
                    //print default message for password
                    default:
                        printf("Password is incorrect...");
                        break;
                }
                break;
                //print default message for id
            default:
                printf("Id is incorrect...");
                break;
        }   
}

Above program shows the following output:

Enter your id: 1010
Enter your password: 1100
Welcome to codeforhunger.com


continue reading How to write a id and password validation program in C
Share This:    Facebook Twitter

May 23, 2021

C program to append data into a file

In this program we will learn How to append data into a file.

EX: Write a C program to append data into a file, How to write a C program to append contents into a file. C program to append data into a file.


  C Program to append data into a file:


#include<stdio.h>
#include<string.h>

int main()
{
    FILE *fptr;
    char str[100];

    //Open file in append mode
    fptr = fopen("file.txt","a");
    
    //Input data to append
    printf("Enter data to append:\n");
    gets(str);
    
    //Append data to a file
    fputs(str,fptr);

    printf("Your data appended in file successfully :) ");
    fclose(fptr);

    return 0;
}

File Contents before appending:

Output after appending data into the file:




continue reading C program to append data into a file
Share This:    Facebook Twitter