Thursday, August 27, 2020

C program to find first & second largest number in array

 /*

C program to find first & second largest number in array

        Array: 3, 10, 80, 20, 5

        Max1: 80

        Max2: 20


Logic:

        1. Input size and elements in array, store it in some variable say size and arr.

        2. Declare two variables max1 and max2 to store first and second largest elements.

        Store minimum integer value in both i.e. max1 = max2 = INT_MIN.

        3. Iterate though all array elements, run a loop from 0 to size - 1.

        4. Inside loop, check if arr[i] > max1

                max2 = max1

                max1 = arr[i]

        5. Else if arr[i] > max2 && arr[i] < max1

                max2 = arr[i]

*/

#include<stdio.h>

#include <limits.h>

int main()

{

        int size, arr[100], i, max1, max2;

        scanf("%d", &size);

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

                scanf("%d", &arr[i]);

        }

        max1 = max2 = INT_MIN;

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

                if(arr[i] > max1){

                        max2 = max1;

                        max1 = arr[i];

                }

                else if(arr[i] > max2 && arr[i] < max1){

                        max2 = arr[i];

                }

        }

        printf("%d %d\n", max1, max2);

        return 0;

}


Video Description: https://www.youtube.com/watch?v=6276kGaKEI4




Wednesday, August 26, 2020

C program to swap two numbers in different ways

 /*

Md. Alamgir Hossain

Lecturer, Dept. of CSE, Prime University

*/

//C program to swap two numbers

//Input:    a = 10, b = 25

//Output: b = 25, a = 10

#include<stdio.h>

int main()

{

        int a, b, temp;

        a = 10;

        b = 25;

        printf("Before swapping: a = %d, b = %d\n",a, b);

        /*

        temp = a;

        a = b;

        b = temp;

        */

        /*

        Using arithmetic operators + and -

        a = a + b;

        b = a - b;

        a = a - b;

        */

        /*

        Using arithmetic operators * and /

        a = a * b;

        b = a / b;

        a = a / b;

        */

        

        /*

        Using bitwise XOR operator ^

        a = a ^ b;

        b = a ^ b;

        a = a ^ b;

        */

        //Using bitwise XOR operator ^ in one line

       // a ^= b ^= a ^= b;

        //Using arithmetic operators + and - in one line

        //a = (a + b) - (b = a);

        //Using arithmetic operators * and / in one line

        //a = (a * b) / (b = a);

        printf("After swapping: a = %d, b = %d\n",a, b);

        return 0;

}



Monday, August 24, 2020

CPP program to check a number is prime or not using class and object

 /*
CPP program to check a number is prime or not using class and object
2, 3, 5, 7, .......
*/
#include<iostream>
using namespace std;
class prime{
private:
        int n, isPrime;//Data Members
public:
        prime(int num){//Constructor
                n = num;
        }
        void primeCk(){//Prime checking function
                isPrime = 1;
                if(n <= 1){
                        isPrime = 0;
                }
                for(int i = 2; i < n; i++){//10
                        if(n % i == 0){
                                isPrime = 0;
                                break;
                        }
                }
        }
        void display(){//Display function
                if(isPrime == 1){
                        cout<<"It's Prime"<<endl;
                }else{
                        cout<<"It's Not Prime"<<endl;
                }
        }
};
int main()
{
        prime obj(1);
        obj.primeCk();
        obj.display();
        return 0;
}



CPP program to calculate the Factorial of a number using class and object

 /*
Alamgir Hossain
Lecturer, Dept. of CSE, Prime University

CPP program to calculate the Factorial of a number using class and object
5! = 5 * 4 * 3 * 2 * 1 = 120
*/
#include<iostream>
using namespace std;
class factorial{
private:
        int num, fact;
public:
        factorial(int n){//Constructor
                num = n;
        }
        void factorialCalculate(){//Function for calculating factorial
                fact = 1;
                for(int i = num; i >= 1; i--){
                        fact = fact * i;
                }
        }
        void display()
        {
                cout<<"Factorial of: "<<num<<" is: "<<fact<<endl;
        }
};
int main()
{
        factorial obj(7);
        obj.factorialCalculate();
        obj.display();
        return 0;
}



C program to find maximum and minimum element in an array

 /*
Alamgir Hossain
Lecturer, Dept. of CSE, Prime University.

C program to find maximum and minimum element in array.
        Example: 20, 17, 10, 5, 50
        Max: 50
        Min: 5


Logic:
        1. Input size and element in array, store it in some variable say size and arr.
        2. Declare max and min to store maximum and minimum element.
        Assume first array element as maximum and minimum both,
        so, max = arr[0] and min = arr[0].
        3. Run loop from first to last array element i.e. 0 to size - 1
        4. Inside loop check each array elements,
        if (arr[i] > max), then update max = arr[i];
        if (arr[i] < min), then update min = arr[i];
*/
#include<stdio.h>
int main()
{
        int size, arr[100], max, min, i;
        printf("Enter the size of the array: ");
        scanf("%d", &size);
        printf("Enter the elements of the array: ");
        for(i = 0; i < size; i++){
                scanf("%d", &arr[i]);
        }
        max = min = arr[0];
        for(i = 0; i < size; i++){
                if(max < arr[i]){
                        max = arr[i];
                }
                if(min > arr[i]){
                        min = arr[i];
                }
        }
        printf("Maximum is : %d\n", max);
        printf("Minimum is: %d\n", min);
        return 0;
}



C program to count even and odd elements in an array

 /*

Alamgir Hossain

Lecturer, Dept. of CSE, Prime University.


C program to count even and odd elements in an array.
Array: 20, 12, 13, 50, 17
Even: 3
Odd: 2
*/
#include<stdio.h>
int main()
{
        int size, arr[100], i, even, odd;
        printf("Enter the size of the array: ");
        scanf("%d", &size);
        printf("Enter the elements of the array: ");
        for(i = 0; i < size; i++){
                scanf("%d", &arr[i]);
        }
        even = 0;
        odd = 0;
        for(i = 0; i < size; i++){
                if(arr[i] % 2 == 0){
                        even = even + 1;
                }
                if(arr[i] % 2 != 0){
                        odd = odd + 1;
                }
        }
        printf("Total even elements: %d\n", even);
        printf("Total odd elements: %d\n", odd);
        return 0;
}


Saturday, August 22, 2020

Major Parts of a C program with proper example

 
 
/*
        01. Documentation Section:
        Name: Alamgir Hossain
        Date: 23/08/2020
        For knowing about major parts.
*/
#include<stdio.h>//02. Linking Section/Part
#include<math.h>
#include<string.h>
#define abb 3.5670 //03. Definition Part
int x = 5;//04. Global Declaration Part
int y = 10;
char ch = 'c';
int main()//05. Main program section
{
        int  p =10;///Local Declaration
        prime();
        printf("%d\n", x);
        return 0;
}
void prime()//06. Sub-Program Section
{
        //printf("%d\n", p);
        int w = 200;
        printf("%d\n", x);
        printf("Welcome to Prime University\n");
}



Friday, August 21, 2020

Parameterized constructor in cpp/c++

 #include<iostream>
using namespace std;
class prime{
public:
        int id; //Data Member
        string name; //Data Member
        prime(int value, string s_name){//Parameterized Constructor
                id = value;
                name = s_name;
        }
        void display(){
                cout<<"Id is: "<<id<<endl;
                cout<<"Name is: "<<name<<endl;
        }
};
int main()
{
        int num;
        string name;
        cin>>num;
        cin>>name;
       // prime obj(120, "Bangladesh");
        prime obj = prime(num, name);
        obj.display();
        return 0;
}

Youtube video link: https://www.youtube.com/watch?v=fEnIx3lbXO4&list=UUkE_J-tXiqxzASO6nlB8D6w&index=2

Default constructor with proper example in cpp in bangla

 #include<iostream>
using namespace std;
class prime{
public:
        int id; //Data Members
        string name; //Data Members
        prime(){//Default Constructor
                id = 12345;
                name ="Bangladesh";
        }
        void display()//Output Function
        {
                cout<<id<<endl;
                cout<<name<<endl;
        }
};
int main()
{
        prime obj;
        obj.display();
        return 0;
}