Sunday, 24 May 2020

How to create only 5 instances of a class in java

How to create only 5 instances of a class in java:

class DemoExample {   
   private static DemoExample d;
                private static int count=0;
               
   private DemoExample ()       {         
                System.out.println ("OBJECT GOT CREATED  "); 
   }    

  
   public static DemoExample create ()  {   
                                if (count<5)  {           
                                                d=new DemoExample ();           
                                                count++;
                                }           
                                else    {       
                                                System.out.println ("Cant create more than five OBJECTS");  
                                                }
                                return (d);    
  }
};
class Demomain {   
   public static void main (String [] args)       {   
         DemoExample d1=DemoExample.create ();        
                                 DemoExample d2=DemoExample.create ();       
                                 DemoExample d3=DemoExample.create ();     
                                 DemoExample d4=DemoExample.create ();       
                                 DemoExample d5=DemoExample.create ();       
                                 DemoExample d6=DemoExample.create ();       
                               
                                 }
                 };
                 
                 
                 
Above is the example code for below for which we can create five objects :
                
1) Can you give Java Code to limit the maximum creation of objects as 5?
2) Only 5 instances of a class in java?
3) Write a java class to have only five instance

 

Saturday, 23 May 2020

C program to find factorial using recursion


C program to find  factorial using recursion:
2! = 1*2 =2
                3! = 1*2*3 =6
                4! = 1*2*3*4 = 24
                5! = 1*2*3*4*5 = 120
                6! = 1*2*3*4*5*6 = 720
                7! = 1*2*3*4*5*6*7 = 5040
                8! = 1*2*3*4*5*6*7*8 = 40320

 Below is the c program to find factorial using recursion.


#include <stdio.h>
int factorial(int n){
                int i, fact=1;
                                if( n==0 || n== 1)
                                                fact=1;
                                else
                                                fact = n * factorial( n-1);
               
                return fact;
}
void main(){
   int number, i, fact =1;
   printf("provide the number to find factorial. ");
   scanf("%d",&number);
  
   // check number is greater than zero or not.
   if( number>=0){
          //  calling the factorial function to find factorial.
       fact= factorial(number);
                   printf("factorial of given number is %d", fact);
   }
   else{
                printf("factorial not defined for negative values....");
                printf("please enter positive numbers ");
                }
}

Output:
provide the number to find factorial.
5
factorial of given number is 120

Output:
provide the number to find factorial.
   1
factorial of given number is 1

Output:
provide the number to find factorial.
0
factorial of given number is 1

Output:
provide the number to find factorial.
     -5
  factorial not defined for negative values....
  please enter positive numbers


5! =    5 * factorial ( 4 )
                  4 *  factorial ( 3 )
                                2 *  factorial ( 2 )
                                                2 *  factorial ( 1 )
                                                                1

Comments in python programming

Comments in python programming:

Comments are used to describe the code in program. These  comments wont get executed.
In python we have two types of comments. They are as follows.

  •  single line comments
  •   multi line comments
Using single line comment we can specify single line of comment we cant specify multiple lines if comments in our program.
Using multi line comment we can specify multi line of comment .
 
                single line comments
                                     single line comments are specified using # symbol.
                                       syntax
                                               #             single line comments are specified using # symbol.
                                                              print('hello')
                multi line comments
                                     multi line comments are specified using  '''  symbol.
                                Syntax.
                                                '''
                                                 this is a multi line comment in python programming language.
                                                  multi line comments are in python programming language.
                                                  using this we describe multiple lines
                                                 '''
                                                                 print('hello')

Friday, 22 May 2020

Factorial program in c


Factorial program in c:
The factorial of a non negative number value is the product of all the integers from 1 to that particular number.
                2! = 2*1 =2
                3! = 3*2*1 =6
                4! = 4*3*2*1 = 24
                5! = 5*4*3*2*1 = 120
                6! = 6*5*4*3*2*1 = 720
                7! = 7*6*5*4*3*2*1 = 5040
                8! = 8*7*6*5*4*3*2*1 = 40320
               
                above are some factorials.
                Factorials are calculated for non negative numbers.
 
There are several ways or methods to find the factorial of given number like 
  • using loops 
  • factorial using functions 
  • factorial using recursion.
First read a number and then whether that given number is greater than or equal to zero or not. If greater then find the factorial of given number else print the message as not defined as number is negative. Zero factorial is 1. One factorial is also 1 (i.e.; 0! =1 and 1! = 1).
Below is the program to find the factorial program in c .
 
Factorial program using for loop:

#include <stdio.h>
void main(){
   int number, i, factorial =1;
   printf("provide the number to find factorial. ");
   scanf("%d",&number);
  
   // check number is greater than zero or not.
   if( number>=0){
       for(i=1; i<=number; i++){
                                                factorial= factorial * i;
                   }
                   printf("factorial of given number is %d", factorial);
   }
   else{
                printf("factorial not defined for negative values....");
                printf("please enter positive numbers ");
                }
}

output1:
                provide the number to find factorial
                5
                factorial of given number is 120
output2:
                provide the number to find factorial
                6
                factorial of given number is 720
output3:
                provide the number to find factorial
                7
                factorial of given number is 5040
output4:
                provide the number to find factorial
                0
                factorial of given number is 1
output5:
                provide the number to find factorial
                1
                factorial of given number is 1
output6:
                provide the number to find factorial
                -5
                factorial not defined for negative values....
                please enter positive numbers
output7:
                provide the number to find factorial
                9
                factorial of given number is ********* (unknown value)


In above program we can find the factorial of a given number upto 7 but if we try to find the factorial of 8 we are getting some unknown value because it is crossing the range of int datatype.
Hence instead of using int we can use long int.


#include<stdio.h>
void main(){
   long int number, i, factorial=1;
   printf("provide the number to find factorial. ");
   scanf("%ld", &number);
  
   // check number is greater than zero or not.
   if(number>=0){
       for(i=1; i<=number; i++){
                                                factorial =factorial*i;
                   }
                   printf(" factorial of given number is %ld", factorial);
   }
   else{
                printf(" factorial not defined for negative values....");
                printf(" please enter positive numbers ");
                }
}





factorial program using while loop:


#include<stdio.h>
void main(){
   int number, i, factorial=1;
   printf("provide the number to find factorial. ");
   scanf("%d",&number);
  
   // check number is greater than zero or not.
   if(number>=0){
                                i=1;
       while( i<=number ){
                                                factorial= factorial * i;
                                                i++;
                   }
                   printf(" factorial of given number is %d", factorial);
   }
   else{
                printf(" factorial not defined for negative values....");
                printf(" please enter positive numbers ");
                }
}



we can also write the factorial of number using recursion.