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 Exercises with their Solutions

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

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:

  1.  Write a first java program which prints message on the output screen.
  2. Write a java program to print given number is even or odd.
  3. Write a java program to swap two numbers.
  4. Write a java program to calculate net salary of an employee using TA, DA and HRA.
  5. Write a java program to print addition, subtraction, multiplication and division of given two numbers.
  6. Write a java program to find area of circle.
  7. Write a java program to find area of triangle.
  8. Write a java program to accept five subjects marks from student and print total and percentage on the output screen.
  9. Write a java program to compute expression and print on the output screen.
  10. Write a java program to calculate inch to meters.
  11. Write a java program to calculate foot to centimeters.
  12. Write a java program to check given number is palindrome or not.
  13. Write a java program to check given number is prime or not.


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



continue reading Java Programming Exercises with their Solutions
Share This:    Facebook Twitter

Jun 7, 2021

Top Programming Questions Asked In Interview

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. 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 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 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 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 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>
void main()
{
    printf(5+"Good Morning");
}

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 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 Questions Asked In Interview
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....


Program:


#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.


We write this program using Nested switch statement in C. In simple words Nested switch statement means switch statement defined inside the another switch statement. 


Program:

#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.

Program:

#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