Showing posts with label Coding Ninja Solutions. Show all posts
Showing posts with label Coding Ninja Solutions. Show all posts

Program to find the last index of an element of array using recursive function.

 // coded by Vaibhav Yadav


#include <iostream>

#define ll long long

ll index(ll a[],ll size,ll x);

using namespace std;

int main() {

    ll n,i,x;

    cout<<"Enter Your Number of Elements in array: ";

    cin>>n;

    ll a[n];

    for(i=0;i<n;i++){

        cout<<"Enter Your Element no "<<i+1<<": ";

        cin>>a[i];

    }

    cout<<"Enter the element you want to search for: ";

    cin>>x;

    if(index(a,n,x)>=0)

    cout<<"Element Is found at position "<<index(a,n,x)<<endl;

    else

    cout<<"Element is not found"<<endl;

    return 0;

}

ll index(ll a[], ll size, ll x){

    if(a[size-1]==x)

    return size-1;

    else

    index(a,size-1,x);

}

Program to print the smallest word in an sentence (Coding Ninja Solutions)

#include <stdio.h>

#include <string.h> // Here I've to add this header file for strcat() function

int main() {

    char name[]="Vaibhav Yadav is good boy";

    char ccname[]=" "; // This is to add space at the end of sentence so that condition can read it.

    strcat(name,ccname); // this functions append the space to last character

    int i,j,k,min=50,count=0;

    for(i=0;name[i]!='\0';i++){

        if(name[i]!=' '){

        count++;

        }

        else{

            if(count<min)

            {

                min=count;

                k  = i;

                k-=count;

            }

                count=0;

        }

    }

    

    // printf("%d ",min);

    for(i=k;name[i]!=' ';i++)

    printf("%c",name[i]);

    k = 0;

    return 0;

}

Operator Overloading in C++

#include<iostream> using namespace std; class Complex { private: int real, imag; public: Complex(int r = 0, int i = 0){       re...