Coders PlayGround

Learn to code and change the future!

Saturday, 19 August 2017

Conditional Statement in Java

August 19, 2017

Program to calculate the percentage of given 3 subjects and print the appropriate grade.

The if construct:

if(condition)
{
    // The desired operation
}




The if-else construct:

if(condition)
{
    // The desired operation
}else{
  // The desired operation
}



if - else if - else

if(condition1)
{
    // The desired operation
}else if(condition2)
{
    // The desired operation
}else
{
    // The desired operation
}


public class Grade
{
 public static void main(String args[])
 {
  int phy,chem,math;
  double a;
  phy=69;
  chem=81;
  math=80;
  a=(phy+chem+math)/3;
  System.out.println("Percentage: "+a);
  if(a>=75)
  {
   System.out.println("Grade:A");
  }
  else if(a>=50)
  {
   System.out.println("Grade:B");
  }
  else if(a>=35)
  {
   System.out.println("Grade:C");
  }
  else
  {
   System.out.println("Fail");
  }
 }
}
OUTPUT
Percentage: 76.0
Grade:A

Read More

Factorial of a number in Java

August 19, 2017
Hello everyone,

The factorial is denoted by "!"

So when we say, find 3 factorial, it is mathematically written as 3!

Now let us look into the formula as to how to find the factorial of any number.
Factorial of a number n is given by n! = n(n-1)(n-2)!

Suppose, factorial of 5 is 5 x 4 x 3 x 2 x 1 = 120.

The two important facts you should know about factorial: 
  • The factorial of 1! = 1
  • The factorial of 0! = 1

Now let us write to program to find the find the factorial of a given number.

import java.util.Scanner;
public class Factorial
{
 public static void main(String args[])
 {
  int i, fact=1;
  System.out.println("Enter the Number:");
  Scanner in= new Scanner(System.in);
  int a=in.nextInt();
  for(i=1;i<=a;i++)
  {
   fact=fact*i;
  }
  System.out.println(factorial +"!="+fact);
 }
}
OUTPUT
Enter the Number:
5
Factorial != 120


Read More

Hello world in Java

August 19, 2017

Hello Friends,
This is a program to print a simple message:

Remember: The file name should be same as the class Name.

In this case, the file name will be First.java

class First
{
    public static void main(String args[ ])
    {
        System.out.println("hello World");
    }
}
OUTPUT
hello World
Read More

Sunday, 6 August 2017

Stack using link list implementation

August 06, 2017
Hello Friends,

#include<stdio.h>
#include<stdlib.h>
struct stack
{
 int data;
 struct stack *next;
};
struct stack *top = NULL;
struct stack *getNode()
{
 return (struct stack*)malloc(sizeof(struct stack));
}
void push()
{
 int val;
 struct stack *newnode;
 printf("\n Enter data :");
 scanf("%d",&val);
 newnode = getNode();
 newnode->data = val;
 if(top == NULL)
 {
  top = newnode;
  newnode->next = NULL;
 }
 else
 {
  newnode->next = top;
  top = newnode;
 }
}
void pop()
{
 int x;
 struct stack *del,*temp;
 temp = top;
 if(temp== NULL)
  printf("\n stack underflow");
 else
 {
  x=temp->data;
  top = temp->next;
  free(temp);
  printf("\n Deleted element is %d",x);
 }
}
void peek()
{
 if(top == NULL)
  printf("\n Stack Underflow");
 else
 {
  printf("Peek value : %d \n ",top->data);
 }
}
void disp()
{
 struct stack *temp=top;
 if(top == NULL)
  printf("\n Stack underflow \n");
 else
 {
  printf("\n Stack elements are : ");
  while(temp!=NULL)
  {
   printf("%d \t",temp->data);
   temp = temp->next;
  }
 }
}
void quit()
{
 printf("\n Thank you!!! \n\n");
}
void main()
{
 int ch;
 do{
  printf("\n == MENU ==");
  printf("\n 1. PUSH ");
  printf("\n 2. POP ");
  printf("\n 3. PEEK ");
  printf("\n 4. DISPLAY ");
  printf("\n 5. QUIT");
  printf("\n Enter your choice :");
  scanf("%d",&ch);
  switch(ch)
  {
   case 1 : push();
      break;
   case 2 : pop();
      break;
   case 3 : peek();
      break;
   case 4 : disp();
      break;
   case 5 : quit();
      break;
   default : printf("\n Invalid input");
  }
 }while(ch!=5);
}
/*
OUTPUT : 



 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :1

 Enter data :50

 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :1

 Enter data :20

 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :1

 Enter data :30

 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :1

 Enter data :40

 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :1

 Enter data :10

 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :4

 Stack elements are : 10  40  30  20  50  
 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :3
Peek value : 10 
 
 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :2

 Deleted element is 10
 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :4

 Stack elements are : 40  30  20  50  
 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :2

 Deleted element is 40
 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :2

 Deleted element is 30
 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :4

 Stack elements are : 20  50  
 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :2

 Deleted element is 20
 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :3
Peek value : 50 
 
 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :4

 Stack elements are : 50  
 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :3
Peek value : 50 
 
 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :2

 Deleted element is 50
 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :3

 Stack Underflow
 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :4

 Stack underflow 

 == MENU ==
 1. PUSH 
 2. POP 
 3. PEEK 
 4. DISPLAY 
 5. QUIT
 Enter your choice :5

 Thank you!!! 

*/


Read More
PropellerAds