program – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Sun, 10 Mar 2024 17:05:13 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg program – Programmerbay https://programmerbay.com 32 32 C Program to Draw a Hut https://programmerbay.com/c-program-to-draw-a-hut/ https://programmerbay.com/c-program-to-draw-a-hut/#respond Thu, 29 Feb 2024 17:23:26 +0000 https://www.programmerbay.com/?p=1886 C supports a header file named “graphics.h” which enables us to draw various figures. In this, we’ll be using line and rectangle function of that particular header file to draw a hut.

C Program to draw a hut in computer graphics

Program:

#include<graphics.h>
#include<stdio.h>
#include<conio.h>
void main(){
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("\t\t ********* HUT ********");
line(150,100,50,200);
line(150,100,350,100); 
line(150,100,300,200);
line(300,200,500,200);
line(350,100,500,200);
line(50,200,300,200);
rectangle(50,400,300,200);
rectangle(300,200,500,400);
rectangle(130,250,230,400);

getch();
closegraph();

}

Output:

Hut

]]>
https://programmerbay.com/c-program-to-draw-a-hut/feed/ 0
C Program to draw Bezier Curve using 4 control points https://programmerbay.com/c-program-to-draw-bezier-curve-using-4-control-points/ https://programmerbay.com/c-program-to-draw-bezier-curve-using-4-control-points/#respond Sat, 24 Feb 2024 16:41:31 +0000 https://www.programmerbay.com/?p=2143 Bezier Curve is one of the Curve representation which uses control points to draw a curve. It is always determined on the number of control points that require to draw it. It follows Bernstein polynomial as the basis function.

Must Read [  What is Bezier Curve ?  ]

C Program to draw a Bezier curve

Program:

#include<graphics.h>
#include<math.h>
#include<conio.h>
#include<stdio.h>
void main()
{
int x[4],y[4],i;
double put_x,put_y,t;
int gr=DETECT,gm;
initgraph(&gr,&gm,"C:\\TURBOC3\\BGI");
printf("\n****** Bezier Curve ***********");
printf("\n Please enter x and y coordinates ");
for(i=0;i<4;i++)                 
{
scanf("%d%d",&x[i],&y[i]);
putpixel(x[i],y[i],3);                // Control Points
}

for(t=0.0;t<=1.0;t=t+0.001)             // t always lies between 0 and 1
{
put_x = pow(1-t,3)*x[0] + 3*t*pow(1-t,2)*x[1] + 3*t*t*(1-t)*x[2] + pow(t,3)*x[3]; // Formula to draw curve
put_y =  pow(1-t,3)*y[0] + 3*t*pow(1-t,2)*y[1] + 3*t*t*(1-t)*y[2] + pow(t,3)*y[3];
putpixel(put_x,put_y, WHITE);            // putting pixel 
}
getch();
closegraph();
}

 

Output:

bezier curve

 

]]>
https://programmerbay.com/c-program-to-draw-bezier-curve-using-4-control-points/feed/ 0
C Program to Wish Happy New Year https://programmerbay.com/c-program-to-wish-happy-new-year/ https://programmerbay.com/c-program-to-wish-happy-new-year/#respond Thu, 15 Feb 2024 08:35:33 +0000 https://www.programmerbay.com/?p=2284 The program uses C graphics header file capabilities to create the illusion of fireworks and show a Happy New Year message. We hope you achieve all your goals this year.

C Program to wish Happy New Year 2025

Program:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void shoot();
void shootagain();
void area();
void explode(int,int,int);
void main()
{
int gm,gd=DETECT;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
shoot();
getch();
closegraph();
}
void shoot()
{
int i=0;
int x=0,y=480,x1=15,y1=460;
while(i<350)
{
area();
line(x+i,y-i,x1+i,y1-i);
delay(50);
i=i+10;
cleardevice();
}
explode(x+350,y-350,5);
shootagain();
}


void shootagain()
{
int i=0;
int x=600,y=480,x1=585,y1=460;
while(i<250)
{
setcolor(15);
area();
line(x-i,y-i,x1-i,y1-i);
delay(30);
i=i+10;
cleardevice();
}
explode(x-300,y-300,5);
}

void explode(int x,int y,int r)
{
int k,j,interval=0;
for(k=0;k<2;k++)
{
interval=interval+50;
for(j=0;j<interval;j++)
{
area();
setcolor(BLACK);
setcolor(rand()/15);
// horizontal left and right
circle(x+j,y,r+k);
circle(x-j,y,r+k);
//vertical up and down
circle(x,y+j,r+k);
circle(x,y-j,r+k);
//slighten down
circle(x+j,y+j,r+k);
circle(x-j,y-j,r+k);
//slighten up
circle(x-j,y+j,r+k);
circle(x+j,y-j,r+k);
delay(30);
cleardevice();
}
}
}
void area()
{
//Area
setcolor(15);
line(0,350,600,350);
rectangle(0,350,100,150);
rectangle(40,350,60,300);
rectangle(10,170,30,200);
rectangle(70,170,90,200);
rectangle(10,230,30,260);
rectangle(70,230,90,260);

rectangle(100,350,180,250);
line(100,250,140,180);
line(180,250,140,180);
rectangle(110,280,130,300);
rectangle(150,280,170,300);
rectangle(130,350,160,330);

rectangle(180,350,350,300);
rectangle(190,310,220,330);
rectangle(340,310,310,330);

rectangle(370,350,440,150);
rectangle(385,350,405,300);
rectangle(380,170,400,200);
rectangle(410,170,430,200);
rectangle(380,230,400,260);
rectangle(410,230,430,260);
settextstyle(BOLD_FONT,HORIZ_DIR,1);
outtextxy(110,50,"HAPPY NEW YEAR");
}

 

Output:

happynewyear

]]>
https://programmerbay.com/c-program-to-wish-happy-new-year/feed/ 0
How to Reverse a String in Java https://programmerbay.com/java-program-to-reverse-a-string/ https://programmerbay.com/java-program-to-reverse-a-string/#respond Mon, 12 Dec 2022 07:38:21 +0000 https://www.programmerbay.com/?p=4156 The Java program reverses a string using different techniques and inbuilt methods which we’ll be discussing in this article.

 

In Java, objects of a String class are immutable, as a results, one can’t alter them. Further, If there is a need to alter a string after its creation, then StringBuilder and StringBuffer are two classes come into picture. As they produce mutable strings and provide reverse() method to reverse a string.

They can be used to finish the program within 2 or 3 lines. Alternatively, a string can also be reversed with the help of an array.

These are the following approaches to reverse a string in Java :

  1. Using StringBuilder
  2. Using StringBuffer
  3. Using toCharArray method
  4. Using swapping technique
  5. Using List
  6. Using recursion technique

Example 1. Program to reverse a string in Java using StringBuilder

Program:

import java.util.Scanner;
/**
 * Reverse a string using StringBuilder
 *
 * */
public class StringReverse {

    public static void main(String[] args) {
        String word;
        System.out.print("Please enter a String = ");
        Scanner ed = new Scanner(System.in);
        word = ed.nextLine();
        StringBuilder str = new StringBuilder(word);
        System.out.println(word + " reversed to " + str.reverse() + " using in StringBuilder");
    }
}

Output:

Please enter a String = Programmerbay
Programmerbay reversed to yabremmargorP using in StringBuilder

Example 2. Program to reverse a string in Java using StringBuffer

Program:

import java.util.Scanner;
/**
 * Reverse a string using StringBuffer
 *
 * */
public class StringReverse {

    public static void main(String[] args) {
        String word;
        System.out.print("Please enter a String = ");
        Scanner ed = new Scanner(System.in);
        word = ed.nextLine();
        StringBuffer str = new StringBuffer(word);
        System.out.println(word + " reversed to " + str.reverse() + " using in StringBuffer");
    }
}

Output:

Please enter a String = angry
angry reversed to yrgna using in StringBuffer

 

Example 3. Program to reverse a string in Java using char Array

In this approach, we have converted the input string to character array and traversing it backward.

Program:

import java.util.Scanner;

/**
 * Reverse a string by converting a string to character array
 */
public class StringReverse {

    public static void main(String[] args) {
        String word, reversed = "";
        char ch[];
        int i, size;
        System.out.print("Please enter a String = ");
        Scanner ed = new Scanner(System.in);
        word = ed.nextLine();
        ch = word.toCharArray();
        size = word.length() - 1;
        for (i = size; i >= 0; i--) {
            reversed = reversed + ch[i];
        }
        System.out.println(word + " reversed to " + reversed);
    }
}

Output:

Please enter a String = programmerbay
programmerbay reversed to yabremmargorp

Example 4. Program to reverse a string in Java by Swapping

Program:

/**
 * Reverse a string by swapping technique
 */
public class StringReverse {

    public static void main(String[] args) {
        String str = "programmerbay";
        char[] chrArr = str.toCharArray();
        for (int i = 0 ; i<=str.length() - 1 ;i++){
            int backrdPos =  str.length() - (i+1);
            if(i < (backrdPos) ) {
                char temp = chrArr[i];
                chrArr[i] =  chrArr[backrdPos];
                chrArr[backrdPos] = temp;
            }
        }
        System.out.println("Reversed String :: ");
        for (char ch:
             chrArr) {
            System.out.print(ch);
        }
    }
}

Output:

Reversed String :: 
yabremmargorp

Example 5. Program to reverse a string in Java using list

Program:

/**
 * Reverse a string by converting the string to list
 */
public class StringReverse {

    public static void main(String[] args) {
        String str = "programmerbay";
        List<Character> characterList = new ArrayList<>();
        for (char ch:
             str.toCharArray()) {
            characterList.add(ch);
        }
        Collections.reverse(characterList);
        System.out.print(str+" reversed to :: ");
        for (Character ch:
             characterList) {
            System.out.print(ch);
        }
    }
}

Output:

programmerbay reversed to :: yabremmargorp

Example 6. Program to reverse a string in Java using recursion

/**
 * Reverse a string by recursion
 */
public class StringReverse {

    public static void main(String[] args) {
        String str = "programmerbay";
        System.out.println(str + " reversed to :: "+ getReverseString(str.toCharArray(), str.length() - 1,""));
    }

    private static String getReverseString(char[] chrArr, int len,String rvrStr) {
        if(len < 0){
            return rvrStr;
        }
        rvrStr = rvrStr + chrArr[len];
        return getReverseString(chrArr, --len, rvrStr);
    }
}

Output:

programmerbay reversed to :: yabremmargorp

 

]]>
https://programmerbay.com/java-program-to-reverse-a-string/feed/ 0
C Program to Find ASCII Value of a Character https://programmerbay.com/c-program-to-print-the-integer-equivalent-of-uppercase-letters-lowercase-letters/ https://programmerbay.com/c-program-to-print-the-integer-equivalent-of-uppercase-letters-lowercase-letters/#respond Thu, 25 Aug 2022 06:56:53 +0000 https://programmerbay.com/?p=5354 The program accepts a character as an input and prints its corresponding ASCII value on output screen.

The ASCII values are ranging from 0 to 128 that cover numbers from 0 to 9, uppercase letters and lowercase letters from a to z and some special characters are also the part of this system.

For example, ASCII value of 0 is 48.

C program to find ASCII value of character

or

Write a program which prints the integer equivalent of uppercase letters, lowercase letters, digits and some special symbols.

Program:

#include <stdio.h>
int main()
{
char value;
printf("Enter a Value = ");
scanf("%c", &value);
printf("integer value of %c = %d\n", value, value);
return 0;
}

Output:

In case of number

Enter a Value = 0
ASCII integer value of 0 = 48

In case of uppercase letter

Enter a Value = A
ASCII integer value of A = 65

In case of lowercase letter

Enter a Value = a
ASCII integer value of a = 97

In case of special character

Enter a Value = @
ASCII integer value of @ = 64

Explanation:

  1. Getting letter as an input from the user
  2. printing its corresponding ASCII and character values on output screen
  3. %d format specifier represents integer for printing ASCII value and %c represents character for printing the input character.

]]>
https://programmerbay.com/c-program-to-print-the-integer-equivalent-of-uppercase-letters-lowercase-letters/feed/ 0
C program to draw arc from 135 to 270 degree using Arc() function https://programmerbay.com/c-program-to-draw-an-arc-from-135-to-270-degree/ https://programmerbay.com/c-program-to-draw-an-arc-from-135-to-270-degree/#respond Thu, 25 Aug 2022 04:44:05 +0000 https://programmerbay.com/?p=5335 Here, we will be using arc() function that is supported by graphics.h header file.

It takes 5 arguments. First two arguments are the coordinates of a circle, its successive two arguments are starting angle and end angle and last one represents the radius of the circle.

The angle value of arc() function can be ranged from 0 to 360 degree.

Syntax:

void arc(coordinateX,coordinateY,startingAngle,closingAngle,radius)

Program to draw an arc from 135 to 270 degree in C

Program:

#include<conio.h>
#include<graphics.h>
#include<stdio.h>
#include<dos.h>
int main()
{
        int gd=DETECT,gm=0;
        initgraph(&gd,&gm,"c:\\tc\\bgi");
       arc(100, 100, 135, 270, 100);
       getch();
       closegraph();
}

Output:

arc function

]]>
https://programmerbay.com/c-program-to-draw-an-arc-from-135-to-270-degree/feed/ 0
C Program To Check whether Triangle is Equilateral, Isosceles or Scalene https://programmerbay.com/c-program-to-classify-a-triangle/ https://programmerbay.com/c-program-to-classify-a-triangle/#respond Sun, 21 Aug 2022 16:33:46 +0000 https://programmerbay.com/?p=5308 The program determines the type of triangle based on the input provided by a user.

A triangle can be broadly classified according to the length of its sides and the angle between the sides.
Based on the sides, there are three types of triangle
1. Equilateral Triangle
2. Isosceles Triangle
3. Scalene Triangle

Based on the angles, there are also 3 types:
1. Acute triangle
2. Right triangle
3. Obtuse triangle

In this article, we’ll be discussing triangles that are classified based on sides, and implementing the same in the program.

type of triangle

  1. Equilateral Triangle: A triangle in which all sides are of the same length
  2. Isosceles Triangle: A triangle in which exactly two sides are equal
  3. Scalene Triangle: A triangle in which all sides are having different length

Approach to determine the type of a triangle

  1. Read the length of a triangle for the user
  2. If side1, side2 and side3 are equal to each other, then
    • print a message the triangle is an equilateral triangle
  3.  If any two from side1, side2 or side3 are having the same length, then
    • print message the triangle is isosceles triangle
  4.  If all three sides are different in length, then
    • print message the triangle is a scalene triangle

C Program To Check whether Triangle is Equilateral, Isosceles or Scalene

The program also implies Triangle Inequality Theorem to validate whether the given sides can be a triangle or not.

Program:

#include<conio.h>
#include<stdio.h>
#include<math.h>
void
main ()
{
  int a, b, c, flag = -1;
  printf (" Enter the values of a, b and c : = ");
  scanf ("%d %d %d", &a, &b, &c);
  if ((a >= 0 && a <= 10) && (b >= 0 && b <= 10) && (c >= 0 && c <= 10)){
 
 // Triangle Inequality Theorem : every side's length should be shorter than sum of other two side
      if (((a + b) > c) && ((b + c) > a) && ((c + a) > b)){
    flag = 1;
    if ((a == b) && (b == c))
      printf ("\n It is an Equilatral Triangle");
    else if ((a == b) || (b == c) || (c == a))
      printf ("\n It is an isosceles Triangle");
    else
      printf ("\n It is a Scalene Triangle");
      }
    }
    
  if (flag == -1)
    printf ("Given side inputs, can't be a triangle ");
    
  getch ();
}

 

Testing Result of the Program

Not a Triangle

 Enter the values of a, b and c : = 11 5 5
Given side inputs, can't be a triangle 

Equilateral triangle

Enter the values of a, b and c : = 4 4 4

It is an Equilateral Triangle

Isosceles Triangle

Enter the values of a, b and c : = 2 3 2

It is an isosceles Triangle

 

]]>
https://programmerbay.com/c-program-to-classify-a-triangle/feed/ 0
Program to Print Histogram in C https://programmerbay.com/c-program-to-create-histogram/ https://programmerbay.com/c-program-to-create-histogram/#respond Sat, 20 Aug 2022 09:55:04 +0000 https://programmerbay.com/?p=5379 The program accepts an array as input and prints a histogram. It uses srand() method for random data creation ranging from 0 to 20 elements.

We need to generate a frequency array for 100 numbers between 0-19. Further, we need to create, add data and print a corresponding histogram for same.

Program to Print Histogram in C

Program:

/******************************************************************************

    Program to print histogram from given array in C                

*******************************************************************************/
#include<stdio.h>
#include <stdlib.h>
#include<time.h>
int main ()
{

  int arr[100], i, barSize, p, count;

  // Variables that are being used for printing histogram 

  int firstBar = 0, secBar = 0, thirdBar = 0, fourthBar = 0, lowerRange =
    0, upperRange = 5;

  // srand for creating random values between 0 to 20 

  srand (time (0));
  for (i = 0; i < 100; i++){
      arr[i] = rand () % 20;
    }


// printing autogenerated array elements on output screen    
      printf ("Array values for the histogram :: \n");

  for (i = 0; i < 100; i++){
      printf ("%d  ", arr[i]);
    }


// printing histogram in C  by spliting bar in intervals of 5 i.e 0-5, 5-10
  for (i = 0; i < 100; i++){
      if (arr[i] < 5){
    firstBar = firstBar + 1;
  }
      else if ((arr[i] < 10) && (arr[i] > 5)){
    secBar = secBar + 1;
  }
      else if ((arr[i] < 15) && (arr[i] > 10)){
    thirdBar = thirdBar + 1;
  }
      else{
    fourthBar = fourthBar + 1;
  }
    }
    
    // Printing histogram in C
    
  printf ("\n\n*************** HISTOGRAM ***************\n\n");
  i = 1;
  barSize = firstBar;
  
  while (i <= 4){
      printf ("\n     |\n%2d-%2d|", lowerRange, upperRange);
      p = 1;
      while (p <= barSize){
    printf ("*");
    p++;
  }
  
      printf ("\n     |\n");
      if (i == 2)
  barSize = secBar;
      if (i == 3)
       barSize = thirdBar;
      if (i == 4)
       barSize = fourthBar;
         lowerRange = lowerRange + 5;
         upperRange = upperRange + 5;
         i++;
    }
  return 0;
}

 

Output:

Random data inputs are generated first:-

Array values for the histogram :: 
6  18  0  9  16  16  17  6  3  10  6  18  13  6  5  15  7  5  17  9  15  6  16  7  15  14  0  0  12  8  12  18  18  12  7  14  0  17  0  15  7  19  13  12  5  10  0  4  15  17  14  2  15  10  9  10  17  2  2  1  2  14  0  0  18  19  14  19  16  6  14  15  5  8  0  2  10  0  19  6  17  13  0  13  15  2  15  4  4  18  6  6  4  18  6  3  9  12  14  18

Plotted to Histogram:

*************** HISTOGRAM ***************


     |
 0- 5|************************
     |

     |
 5-10|************************
     |

     |
10-15|********************
     |

     |
15-20|****************
     |

Explanation:

  1. We have generated frequency data, with values ranging from 0 to 19
  2. To create a histogram, we have used intervals 0-5,5- 10, 10-15, 15-20 with 5 class size
  3. In order to create 4 bars, we have used 4 variables named, firstBar, secBar, thirdBar, and fourthBar
  4. While iterating frequency data, if the value lies between 0 to 5, count it as 1 and add it to the first bar
  5. Similarly, if it ranges from 5 to 10, then add 1 to secBar variable
  6. Lastly, we have printed the histogram on the output screen

]]>
https://programmerbay.com/c-program-to-create-histogram/feed/ 0
C Program to Convert Minutes into Hours and Minutes https://programmerbay.com/c-program-to-convert-minutes-into-hours/ https://programmerbay.com/c-program-to-convert-minutes-into-hours/#respond Sat, 20 Aug 2022 08:15:45 +0000 https://programmerbay.com/?p=5362 The program converts minute to hours & minute format and prints the result on the output screen. It uses basic formula for converting the given minute input to the respective hours.

hours = minute / 60

Write a program that takes minutes as input and converts it into hours and minutes.

Program:

#include<stdio.h>
int main()
{
int minute;
printf("\n\n\tEnter minutes = ");
scanf("%d",&minute);
printf("\n\t Entered minutes = %d minutes \n\t Which is equivalent to = %d hours and %d minutes",minute,minute/60,minute%60);
return 0;
}

Output:

minute to hour

 

Explanation:

  1. Getting input from a user
  2. Then, simply dividing it by 60, that would give quotient as hours and remainder(using modulo operator) as minutes

]]>
https://programmerbay.com/c-program-to-convert-minutes-into-hours/feed/ 0
Factorial Program in Java https://programmerbay.com/java-program-to-calculate-factorial-of-a-number-using-recursion/ https://programmerbay.com/java-program-to-calculate-factorial-of-a-number-using-recursion/#respond Fri, 12 Aug 2022 17:19:45 +0000 https://programmerbay.com/?p=5103 The program simply prints factorial of a given number.  A factorial is calculated by multiplying  all the numbers starting from 1 to all the way nth number. For example, factorial of 5 would be, 1 X 2 X 3 X 4 X 5, which is 120.

Screenshot from 2022 08 14 15 58 07

The same logic will be used in this article to calculate the same. However, there are various approaches to achieve this functionality.

Below are some approaches to calculate factorial of a number :

  1. Using Loop
  2. Using recursion
  3. Using ternary operator
  4. Using Java 8 reduce method

Example 1. Java program to calculate factorial of a number using Loop

In this example, we have simply used “for loop” and on each iteration, we’re incrementing the counter by 1 until it reaches to nth number.  Within the looping body, we’re multiplying the counter with the factorial variable which is having an initial value of 1, and storing the result in factorial variable itself. 

Program :

import java.util.Scanner;
public class FactorialProgram {
    public static void main(String[] args) {
        int number;
        int factorial = 1;
        Scanner input = new Scanner(System.in);
        System.out.print("Please enter a number :: ");
        number = input.nextInt();
        for (int i = 1 ; i <= number; i++){
            factorial = factorial * i;
        }
        System.out.println("Factorial of "+number+" is :: "+ factorial);
    }
}

Output:

Please enter a number :: 5
Factorial of 5 is :: 120

Example 2. Java program to calculate factorial of a number using Recursion

Recursion function refers to a method which calls itself and terminates when some condition is triggered. Below is the example to calculate the factorial with the help of recursion function

Program:

public class FactorialProgram {
    static long getFactorial(int number) {
        if (number == 1) {
            return 1;
        } else {
            return number * getFactorial(number - 1);
        }
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int number;
        long factorial;
        System.out.print("Please enter a number :- ");
        number = input.nextInt();
        factorial = getFactorial(number);
        System.out.println("Factorial of " + number + " = " + factorial);
    }
}

Output:

]]>
https://programmerbay.com/java-program-to-calculate-factorial-of-a-number-using-recursion/feed/ 0