Sunday, March 10, 2024

C Language Recursive Program To Check Palindrome

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

int rev(char [], int,int);

int main()
{
   char ch[100];
   printf("Enter a string:");
   scanf("%s",ch);
   printf("\nEntered string is %s",ch);
   int x=rev(ch,0,strlen(ch)-1);
   if(x==1)
   printf("\nPalindrome");
   else
   printf("\nNot Palindrome");
    return 0;
}

int rev(char ch[],int start,int end)
{
   
    if(ch[start]!=ch[end])
    return 0;
    else if(start==end)
    return 1;
    start++;
    end--;
    rev(ch,start,end);
}

C Language Recursive Function Program To Reverse A String

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

void rev(char [],int, int);

int main()
{   char ch[100];
   printf("Enter a string:");
   scanf("%s",ch);
   printf("\nEntered string is %s",ch);
 rev(ch,0,strlen(ch)-1); 
      return 0;
}

void rev(char ch[],int i,int end)
{
    char c;
   if(end<=i)
   {
      printf("\nReverse=%s",ch);
       return ;
   }
      c=ch[i];
       ch[i]=ch[end];
       ch[end]=c;
   i++;
   end--;
   rev(ch,i,end);    
}

Tuesday, January 30, 2024

Java Program Pell Number Series

 What is the Pell series?

Pell series starts with the first term as 0 and the second term as 1. The next term will be an addition to the second last term and two times to the last term. Now, if we compute the third term it will be the sum of the second last term (0), and twice of the last term (21) is 2 (0+2). 

The fourth term will be the sum of the second last term (1) and twice of the last term (22) is 5 (1+4).

 We can find more terms using the same method.

Relation of Pell series number: T(n) = T(n-2) + 2T(n-1)

T(1) = 0

T(2) = 1

T(3) = 0+2*1 = 0+2 = 2

T(4) = 1+2*2 = 1+4 = 5

T(5) = 2+2*5 = 2+10 = 12

.

.

.

T(n) = T(n-2) + 2*T(n-1)

import java.util.*;

class Pell

{

   Scanner sc=new Scanner(System.in);

    void main()

    {

       int i,n,a=0,b=1,next; 

       System.out.print("\nHow many Elements:");

       n=sc.nextInt();

         System.out.print("Series as follows "+a+" "+b);  

       for(i=0;i<n-2;i++)

       {

           next=a+2*b;           

           System.out.print(" "+next);

           a=b;

           b=next;

        }     

    }

Java Program Triangular Number Series

 The first 10 terms of the Triangular number series:

1 3 6 10 15 21 28 36 45 55 


class Triangular

{ 

    void main()

    {      

         int i,n=1;          

         System.out.println("Series as follows ");  

       for(i=0;i<10;i++)

       {

           n=n+i;

           System.out.print(" "+n);

           n++;

        }     

    }

}  

Sunday, December 10, 2023

2 D Array To Store Only Composite Numbers - BlueJ Program

 A class Composite contains a two-dimensional array of order [m x n]. The maximum values possible for both ‘m’ and ‘n’ is 20. Design a class Composite to fill the array with the first (m x n) composite numbers in column wise. [Composite numbers are those which have more than two factors.]The details of the members of the class are given below:

Class name: Composite

Data members/instance variables :arr[ ] [ ] :integer array to store the composite numbers column wise

 m        :integer to store the number of rows 

n:integer to store the number of columns 

Member functions/methods:

Composite(int mm, int nn ):to initialize the size of the matrix, m=mm and n=nn

 int isComposite( int p ):to return 1 if the number is composite otherwise returns 0

void fill ( ):to fill the elements of the array with the first (m × n) composite numbers in column wise

void display( ):to display the array in a matrix form Specify     the     class Composite giving     details of     the constructor(int,int), int isComposite(int), void fill( ) and void display( ). Define a main( ) function to create an object and call all the functions accordingly to enable the task.


import java.util.*;
class Composite
{
int m,n,arr[][];
Scanner sc=new Scanner(System.in);
Composite(int mm, int nn)
{
    m=mm;
    n=nn;
    arr=new int [m][n];
}
private int isComposite( int p )
{
   int i;
   for(i=2;i<p;i++)
   {
        if(p%i==0)
        break;
   }
   if(i==p)
   return 0;
   else
   return 1;
}
private void display()
{
   int i,j;
   for(i=0;i<m;i++)
   {
        for(j=0;j<n;j++)
        {
            System.out.print(" "+arr[i][j]);
        }
        System.out.println();
   }
}
void fill ( )
{
    int i,j;
   for(i=0;i<m;i++)
   {
        for(j=0;j<n;j++)
        {
            System.out.print("\nEnter Number:");
            int d=sc.nextInt();
            if(isComposite(d)==1)
            arr[i][j]=d;
            else
            {
                System.out.print("\nEnter Composite Number "+d + "  is not composite");
                j--;
            }   
        }
    }
}
public static void main(String args[])
{
    int m,n;
    Scanner sc=new Scanner(System.in);
    while(true)
    {
        System.out.print("\nEnter Row Number:");
        m=sc.nextInt();
        if(m>0 && m<=20)
        break;
    }
    while(true)
    {
        System.out.print("\nEnter Column Number:");
        n=sc.nextInt();
        if(n>0 && n<=20)
        break;
    }
Composite ob=new Composite(m,n);
ob.fill();
System.out.print("\nFinal Matrix\n");
ob.display();
}
}   
    
         
    
      

Friday, December 8, 2023

Java Program To Check Boring Number

  Write a program in java to accept a number from user and check whether the number us a boring number or not.

Boring number is a positive number having all the digits at even position in the number as even and the digits at odd position odd

 

import java.util.*;
class BoringNumber
{
int n,rev,f;
Scanner sc=new Scanner(System.in);
public void take()
{
   System.out.print("\nEnter The Number:");
   n=sc.nextInt();
   display();
}
private void display()
{
   rev=0;
   f=1;
    System.out.print("\nEntered Number is:"+n);
   while(n>0)
   {
        rev=rev*10+n%10;
        n=n/10;
   }
   while(rev>0)
   {
       int d=rev%10;
       if(f%2!=d%2)
       break;
       rev=rev/10;
       f++;
   }
   if(rev>0)
   System.out.print("\nEntered Number is not a boring number");
   else
   System.out.print("\nEntered Number is a boring number");
}
public static void main(String args[])
{
BoringNumber ob=new BoringNumber();
ob.take();
}
}         


Sample Input Output


Enter The Number:1234
Entered Number is:1234
Entered Number is a boring number

Enter The Number:1321
Entered Number is:1321
Entered Number is not a boring number

Sunday, October 15, 2023

C Language Program Finding Factorial Value Using Recursive Function

 #include<stdio.h>

long int fact(int n);

int main() {

    int n;

    printf("Enter a positive integer: ");

    scanf("%d",&n);

    printf("Factorial of %d = %ld", n, fact(n));

    return 0;

}

long int fact(int n) {

    if (n==1)

    return 1;

       int y=fact(n-1);

       return n*y;    

}

Sunday, October 1, 2023

Converting Non-Palindrome Word Into Palindrome

 Convert the non-palindrome words of the sentence into palindrome words by concatenating the word by its reverse (excluding the last character).

Example: The reverse word of HELP would be LEH (omitting the last alphabet) and by concatenating both, the new palindrome word is HELPLEH. Thus the word HELP becomes HELPLEH.

Note: The words which end with repeated alphabets, for example ABB would become ABBA and not ABBBA and XAZZZ becomes XAZZZAX.


import java.util.*;

class Ab

{

  String str1,str2;

  Scanner sc=new Scanner(System.in);

  int c=0;

  void show()

  {

      int len,i;

      c=1;

      System.out.print("\nEnter the Word:");

      str1=sc.next();

      if(notPalin(str1))

      {

         len=str1.length();

         for(i=len-1;i>0;i--)

         {

             if(str1.charAt(i)==str1.charAt(i-1))

             c++; /* 'c' marks the index where mismatch of characters happen */

            }

         }

         str2=leftRev(str1.substring(0,str1.length()-c)); 

         str1=str1+str2;

         System.out.println("\nModified="+str1);

      }

    

    private boolean notPalin(String str1)

    {

       int i,len;

       len=str1.length()-1;

       for(i=0;i<=len;i++)

       {

           if(str1.charAt(i)!=str1.charAt(len-1))

           break;

       }

       if(i<=len)

       return true;

       else

       return false;

    }

    private String leftRev(String str1)

    {

       int i,len;

       String str2="";

       len=str1.length(); 

       for(i=len-1;i>=0;i--)

       str2=str2+str1.charAt(i);

       return str2;

    }

public static void main(String args[])

{

Ab obj=new Ab();

obj.show();

}

Subscribe via email

Enter your email address:

Delivered by FeedBurner