Friday, December 27, 2019

Python program to check if a string is palindrome or not

Python program to check if a string is palindrome or not

Answer:

my_str = 'Madam'
my_str = my_str.casefold()

rev_str = reversed(my_str)

if list(my_str) == list(rev_str):
   print("The string is a palindrome.")
else:
   print("The string is not a palindrome.")


Output:

The string is a palindrome.

C++ code to implement Sierpinski Triangle using Graphics

C++ code to implement Sierpinski Triangle using Graphics
 
#include  
#include  
#include  
 
#define Y 900
#define X 1600

void triangle(float x, float y,
              float h, int colorVal)
{
    setcolor(colorVal % 15 + 1);
 
    for (float delta = 0; delta > -5; delta -= 1) {
        line(x - (h + delta) / sqrt(3),
             y - (h + delta) / 3,
             x + (h + delta) / sqrt(3),
             y - (h + delta) / 3);
        line(x - (h + delta) / sqrt(3),
             y - (h + delta) / 3,
             x,
             y + 2 * (h + delta) / 3);
        line(x,
             y + 2 * (h + delta) / 3,
             x + (h + delta) / sqrt(3),
             y - (h + delta) / 3);
    }
}
 

void trianglev2(float x, float y,
                float h, int colorVal)
{
    setcolor(colorVal % 15 + 1);
 
    for (float delta = 0; delta > -1 + 5; delta -= 1) {
 
        line(x - (h + delta) / sqrt(3),
             y + (h + delta) / 3,
             x + (h + delta) / sqrt(3),
             y + (h + delta) / 3);
        line(x - (h + delta) / sqrt(3),
             y + (h + delta) / 3,
             x,
             y - 2 * (h + delta) / 3);
        line(x,
             y - 2 * (h + delta) / 3,
             x + (h + delta) / sqrt(3),
             y + (h + delta) / 3);
    }
}
 

int drawTriangles(float x = X / 2,
                  float y = 2 * Y / 3,
                  float h = Y / 2,
                  int colorVal = 0)
{
 
    if (h < 5) {
        return 0;
    }
 
    if (x > 0 && y > 0 && x < X && y < Y) {
        triangle(x, y, h, colorVal);
    }
 
    drawTriangles(x,
                  y - 2 * h / 3,
                  h / 2,
                  colorVal + 1);
    drawTriangles(x - h / sqrt(3),
                  y + h / 3,
                  h / 2,
                  colorVal + 1);
    drawTriangles(x + h / sqrt(3),
                  y + h / 3,
                  h / 2,
                  colorVal + 1);
 
    return 0;
}
 
int main()
{
    initwindow(X, Y);
    trianglev2(X / 2, 2 * Y / 3, Y, 2);
 
    drawTriangles();
    getch();
    closegraph();
 
    return 0;

Python program to sort alphabetically the words form a string provided by the user

Python program to sort alphabetically the words form a string provided by the user

Answer:

my_str = input("Enter a string: ")
words = my_str.split()
words.sort()
print("The sorted words are:")
for word in words:
print(word)

Output:
Enter a string: Hello this Is an Example
The sorted words are:
Example
Hello
Is
an
this

C++ program to implement Selection Sort

C++ program to implement Selection Sort:

#include

using namespace std;

void swap(int &a, int &b)
{     
   int temp;
   temp = a;
   a = b;
   b = temp;
}

void display(int *array, int size)
{
   for(int i = 0; i      cout << array[i] << " ";
   cout << endl;
}

void selectionSort(int *array, int size)
{
   int i, j, k;
   for(i = 0; i   {
      k = i; 
      for(j = i+1; j         if(array[j] < array[k])
            k = j;
            swap(array[i], array[k]);
   }
}

int main()
{
   int n;
   cout << "Enter the number of elements: ";
   cin >> n;
   int arr[n];         
   cout << "Enter elements:" << endl;
   for(int i = 0; i {
      cin >> arr[i];
   }
   cout << "Array before Sorting: ";
   display(arr, n);
   selectionSort(arr, n);
   cout << "Array after Sorting: ";
   display(arr, n);
}


Output

Enter the number of elements: 4
Enter elements:
5 9 7 23
Array before Sorting: 5 9 7 23
Array after Sorting: 5 7 9 23 

C++ program to implement Bubble Sort.

C++ program to implement Bubble Sort.

#include

using namespace std;

void BubbleSort (int arr[], int n)
{
int i, j;
for (i = 0; i < n; ++i)
{
for (j = 0; j < n-i-1; ++j)
{
if (arr[j] > arr[j+1])
{
arr[j] = arr[j]+arr[j+1];
arr[j+1] = arr[j]-arr[j + 1];
arr[j] = arr[j]-arr[j + 1];
}
}

}
}

int main()
{
int n, i;
cout<<"\nEnter the number of data element to be sorted: ";
cin>>n;

int arr[n];
for(i = 0; i < n; i++)
{
cout<<"Enter element "< cin>>arr[i];
}

BubbleSort(arr, n);
  cout<<"\nSorted Data ";
for (i = 0; i < n; i++)
        cout<<"->"<
return 0;
}

Program in python to demonstrate star pattern after 180 degree rotation

Program in python to demonstrate star pattern after 180 degree rotation

Answer:

def pypart2(n):
   
 
    k = 2*n - 2

 
    for i in range(0, n):
   
            for j in range(0, k):
            print(end=" ")
   
     
        k = k - 2
   
            for j in range(0, i+1):
       
       
            print("* ", end="")
   
   
        print("\r")

n = 5
pypart2(n)

Output:

            *
         * *
      * * *
   * * * *
* * * * * 

Program in python to demonstrate star pattern printing triangle

Program in python to demonstrate star pattern printing triangle
  
Answer:

def triangle(n):
k = 2*n - 2
     for i in range(0, n):
     
        for j in range(0, k):
            print(end=" ")
     
     
        k = k - 1
     
       
        for j in range(0, i+1):
         
         
            print("* ", end="")
     
     
        print("\r")

n = 5
triangle(n)



Output:

    *
   * *
  * * *
 * * * *
* * * * * 

Tuesday, December 24, 2019

Happy Christmas

Merry Christmas😄🎄🎁❄🎅

Merry Christmas greetings

“Christmas season is the time of sharing. Start sharing LOVE and HOPE to your FAMILY. Eventually, the Love you have shared will also be imparted to everyone in your society. Let LOVE fill the air. Merry Christmas to you and your family!”

Featured posts

Mongolia

 Mongolia! Mongolia is a vast and sparsely populated country in East Asia, known for its stunning natural beauty, rich history, and unique c...

Popular posts