Sunday, 26 September 2021

WAP to print of digits of a entered number.

// WAP to print of digits of a entered number

#include <iostream>

#include<cstdlib>

using namespace std;


int main() {

    string str; 

    cout<<"Enter a number: "<<endl;

    cin>>str;

    int sum = 0;

    for (int i=0; i<str.length(); i++) {

        sum += str[i] - '0';

    }

    cout<<"Sum of all digit: "<<sum<<endl;

}


Sunday, 21 April 2019

To implement binary search tree using linked list.

//To implement binary search tree using linked list.
#include<stdio.h>
int count = 0;
typedef struct Btree{
     int data;
     struct Btree *left;
     struct Btree *right;
};
struct Btree* insert(struct Btree *bt,int n){
    if(bt==NULL){
         bt= (struct Btree *) malloc(sizeof(struct Btree));
         bt->data=n;
         bt->left=NULL;
         bt->right=NULL;
    }
     else if(n<=bt->data)
     bt->left = insert(bt->left,n);
     else
     bt->right = insert(bt->right,n);
     return bt;
}

int searchElement(struct Btree *bt,int item){
    if(bt!=NULL){
         count++;
         if(item<bt->data)
         searchElement(bt->left,item);
         else if(item>bt->data)
         searchElement(bt->right,item);
         else if(item == bt->data)
         return count;
    }
    else{
        return 0;
    }
}
int main(){
     struct Btree *bt;
     int loc;
     bt=NULL;
     int item;
     while(1){
        count = 0;
        printf("which operation you want to perform....\n");
        printf("1. Insert an element.\n");
        printf("2. Search an element.\n");
        int op;
        int sEle;
        scanf("%d",&op);
        switch(op){
        case 1:
             printf("Enter a number.\n");
             scanf("%d",&item);
             bt = insert(bt,item);
             break;
        case 2:
             printf("Enter an number for search.");
             scanf("%d",&sEle);
             loc = searchElement(bt,sEle);
            if(loc==0)
                printf("Element not found.");
            else
                printf("element found at %d ",loc);
                printf("\n");
            break;
        default:
            printf("Please choose correct option...\n\n");
        }
     }
}

Binary Tree using link list in c programming.


#include<stdio.h>

typedef struct Btree{
    int data;
    struct Btree *left;
    struct Btree *right;
};

struct Btree insert(struct Btree *bt,int n){
    if(bt==NULL){
        bt= (struct Btree *) malloc(sizeof(struct Btree));
        bt->data=n;
        bt->left=NULL;
        bt->right=NULL;
    }
    else if(n<= bt->data)
        bt->left = insert(bt->left,n);
    else
        bt->right = insert(bt->right,n);
    return bt;
}

void preorderTravasal(struct Btree *bt){
    if(bt){
        printf("%d,",bt->data);
        preorderTravasal(bt->left);
        preorderTravasal(bt->right);
    }
    return;
}

void inorderTravasal(struct Btree *bt){
    if(bt){
        inorderTravasal(bt->left);
        printf("%d,",bt->data);
        inorderTravasal(bt->right);
    }
    return;
}

void postorderTravasal(struct Btree *bt){
    if(bt){
        postorderTravasal(bt->left);
        postorderTravasal(bt->right);
        printf("%d,",bt->data);
    }
    return;
}

int main(){
    struct Btree *bt;
    bt=NULL;
    int item;

    while(1){
        printf("which operation you want to perform....\n");
        printf("1. Insert an element.\n");
        printf("2. preOrder Traversal\n");
        printf("3. InOrder Traversal\n");
        printf("4. postOrder Traversal\n");

        int op;

        scanf("%d",&op);
        switch(op){
        case 1:
            printf("Enter a number.\n");
            scanf("%d",&item);
            bt = insert(bt,item);
            break;
        case 2:
            preorderTravasal(bt);
            break;
        case 3:
            inorderTravasal(bt);
            break;
        case 4:
            postorderTravasal(bt);
            break;
        default:
            printf("Please choose correct option...\n\n");
        }
    }
}

Friday, 19 April 2019

Program to print day on the date given as input.

#include<stdio.h>
#include<conio.h>

struct date{
    int day,mon,year;
}dat;

int main(){
    int y,f,o_day,od_temp,i,temp,c,o_year,l_year;

    while(1){
        y=0,o_day=0,od_temp=0;temp=0;
        printf("Enter Date (dd/mm/yyyy): ");
        scanf("%d/%d/%d",&dat.day,&dat.mon,&dat.year);

        f=dat.year%4==0 ? 29:28;
        int d[12]={31,f,31,30,31,30,31,31,30,31,30,31};

        while(temp<=dat.year)
            temp+=400;
        if(temp>dat.year)
            temp-=400;
        dat.year-=temp;

        c=(dat.year-dat.year%100)/100;
        od_temp=5*c+c/4;
        od_temp%=7;

        y=dat.year%100-1;
        l_year=y/4;
        o_year=y-l_year;
        od_temp+=(2*l_year+o_year);
        od_temp%=7;

        temp=0;
        for(i=0;i<dat.mon-1;i++)
            temp+=d[i];
        temp+=dat.day;

        od_temp+=temp%7;
        o_day=od_temp%7;

        switch(o_day){
        case 1:
            printf("Monday");
            break;
        case 2:
            printf("Tuesday");
            break;
        case 3:
            printf("Wednesday");
            break;
        case 4:
            printf("Thusday");
            break;
        case 5:
            printf("Friday");
            break;
        case 6:
            printf("Saturday");
            break;
        case 0:
            printf("Sunday");
            break;
        }
        printf("\n\n");
    }
}

Friday, 14 September 2018

Fill up the blanks in the constructor to get the output as per the test cases.

Fill up the blanks in the constructor to get the output as per the test cases. Sample Test Cases
InputOutput
Test Case 1.5 D6 C A
6 C A
Test Case 2.9 I10 H A
6 C A
Test Case 3.6 A7 @ A
6 C A
Test Case 4.5 F6 E A
6 C A

#include <iostream>
using namespace std;

class Sample {
    public:
    int data_ ;
    char  graph_, data_or_graph_;

Sample(int x =6,char y='C',char z='A'): data_(x), data_or_graph_(y),graph_(z) {
        cout << data_ << " " << data_or_graph_ << " " << graph_ << " " << endl;
    }
}; // End of the class

int main() {
    int x; char y;
    cin >> x >> y ;
    Sample s1(x+1, y-1), s3;
    return 0;
}

Wednesday, 20 June 2018

Python Program To demonstrate inheritance.

# Python Program To demonstrate inheritance.

# Parent Class
class parentCls:
    def __init__(self):
        print("Parent Class Constructor.")

    # definition   of Parent class methods
    def prntMethd_1(self):
        print("I am parent class method 1.")
    def prntMethd_2(self):
        print("I am parent class method 2.")

# Child Class
class childCls(parentCls):  # inherit to parent class
    def __init__(self):
        print("Child class constructor.")

    # definition   of child class methods
    def childMethd_1(self):
        print("I am child class method 1")
    def childMethd_2(self):
        print("I am child class method 2")

c = childCls()  # Child class instance

c.childMethd_1()    # child call it's method
c.childMethd_2()    # Again child call it's method
c.prntMethd_1()     # child call it's Parent class method
c.prntMethd_2()     # Again child call it's Parent class method

input("Press Enter to Exit")

Tuesday, 19 June 2018

Python program to demonstrate constructors

#Python program to demonstrate constructors
# declaration of class

class myClass:
    def __init__(self,fName,lName): # Constructor
    # initialization of variable
        self.FirstName = fName
        self.LastName = lName

    def fName(self):
        return self.FirstName

    def lName(self):
        return self.LastName

mcls = myClass("blk","CapHax")
print("First Name : " + mcls.fName())
print("Last Name : " + mcls.lName())

print()
input("Press Enter to exit.")

Featured post

WAP to print of digits of a entered number.

// WAP to print of digits of a entered number #include <iostream> #include<cstdlib> using namespace std; int main() { ...

Popular Posts