C program to check if a given number is a power of 2 in single statement

C program to check if a given number is a power of 2 in single statement
  • Write a program in C to find whether a number is power of two or not in one line.
  • How to find whether a number is power of two or not.
  • Any number who is a power of two(let it be N) have only one bit set in it’s binary representation.
    For Example: 4 = 100, 8 = 1000, 16 = 10000, 32 = 100000 etc.
  • If we subtract 1 from any power of 2 number then, the set bit becomes unset and all the bits in right side of the originally set bit becomes 1.
    For Example: 4-1 = 011, 8-1 = 0111, 16-1 = 01111, 32-1=011111
  • Now, If bitwise and(&) of N and N-1 returns ) means N is a power of 2.
    For Example, 4 & 3 = 100 & 011 = 000

C program to check whether a number is power of two using bitwise operator

C program to check if a given number is a power of 2 in single statement

#include<stdio.h>
#include<math.h>
 
int main() {
    int num;
     
    printf("Enter an integer\n");
    scanf("%d", &num);
     
    if(num && ((num & (num-1)) == 0)){
        printf("%d is power of 2", num);
    } else {
        printf("%d is not a power of 2", num);
    }
 
    return 0;
}

Output

Enter an integer
16
16 is power of 2
Enter an integer
15
16 is not a power of 2

C++ Program to Find Power of Number using Recursion

In the previous article, we have discussed C++ Program to Find GCD or HCF of Two Numbers Using Recursion. In this article, we will see C++ Program to Find Power of Number using Recursion.

C++ Program to Find Power of Number using Recursion

C++ Program to calculate exponential of a number using recursion

C++ Program to calculate exponential of a number using recursion

#include <iostream>
 
using namespace std;
 
int getPower(int base, int exponent);
 
int main(){
    int base, exponent, counter, result = 1;
    cout << "Enter base and exponent\n";
    cin >> base >> exponent;
      
    result = getPower(base, exponent);
      
    cout << base << "^" << exponent << " = " << result;
    return 0;
}
/*
 * Function to calculate base^exponent using recursion
 */
int getPower(int base, int exponent){
    /* Recursion termination condition,
     * Anything^0 = 1
     */
    if(exponent == 0){
        return 1;
    }
    return base * getPower(base, exponent - 1);
}

Output

Enter base and exponent
3 4
3^4 = 81

Print Hello World, Multiply two Numbers, Add Two Numbers, etc. are some of the basic-level Programs in C++. Want to practice advanced C++ Programs? Go with the available links & gain more coding knowledge.

C Program to Find Twos Complement of a Binary Number

C program to find two complement of a binary number
  • Write a C program to read a binary number and find its two’s complement.
  • Wap in C to find two complement of a binary number.

Required Knowledge

Algorithm to find twos complement of a binary number
1. To find the twos complement of a number first find the ones complement by toggling the bits of the number. Change all 1's to 0's and all 0's to 1's.
2. Add binary 1 to the ones complement to get twos complement.

For Example :
Binary Number = 00101011
Ones Complement = 11010100
Twos Complement = 11010101

C program to find two complement of a binary number

C program to find two complement of a binary number

#include <stdio.h>  
#include <string.h>  
   
int main() {
    char binaryNumber[100], onesComplement[100], twosComplement[100];
    int counter, error=0, digitCount, carry = 1;   
    /*
     * Take a binary string as input from user
     */
    printf("Enter a Binary Number\n");  
    scanf("%s", binaryNumber);  
   
    /* 
     * To get one's complement, we toggle 
     * 1's to 0's and 0's to 1's
     */
    digitCount = strlen(binaryNumber);
     
 for(counter=0; counter < digitCount; counter++) {  
        if(binaryNumber[counter]=='1') {  
            onesComplement[counter] = '0';  
        } else if(binaryNumber[counter]=='0') {  
            onesComplement[counter] = '1';  
        } else {  
            printf("Error :( ");  
            return 1;
        }  
    }  
    onesComplement[digitCount] = '\0';
     
    /* 
     * Add 1 to the 1's complement. Perform binary addition
     */ 
    for(counter = digitCount-1; counter >= 0; counter--) {  
        if(onesComplement[counter]=='1' && carry==1){
            twosComplement[counter] = '0';  
        } else if(onesComplement[counter]=='0' && carry==1) {  
            twosComplement[counter] = '1';  
            carry = 0;  
        } else {  
            twosComplement[counter] = onesComplement[counter];  
        }  
    }  
    twosComplement[digitCount] = '\0'; 
       
    printf("Two's Complement : %s", twosComplement);  
   
    return 0;  
}

Output

Enter a Binary Number
11100001
Two's Complement : 00011111

C Program to Sort an Integer Array in Decreasing Order Using Bubble Sort

  • Write a C program to sort to an array in decreasing order using bubble sort.

Required Knowledge

Given an unsorted integer array, we have to sort the elements of the array in decreasing order. As the index of the array increases, the values of the elements should decrease.
For Example:
Input Array : 2 8 4 9 1 0
Output Array : 9 8 4 2 1 0

Algorithm to sort an array in decreasing order using bubble sort
Let inputArray is an integer array having N elements.

  • Bubble sort compares two adjacent element of an array and will swap them if they are out of order. If inputArray [i] < inputArray[i+1] then we will swap elements at position i and i+1 as they are not in decreasing order. As we are sorting in decreasing order, element at index i must be greater than element in i+1 in sorted array.
  • If inputArray [i] >= inputArray[i+1] then we should not swap elements at position i and i+1 as they are already in decreasing order.
  • Next, we will compare i+1 and i+2, then i+2 amd i+3 and so on… till end of the array.
  • If there are N element then we have to repeat above process N-1 times because in every traversal we will put largest element of unsorted sub-array in its sorted position.

C program to sort an array in decreasing order using bubble sort

C Program to Sort an Integer Array in Decreasing Order Using Bubble Sort

#include <stdio.h>
#include <conio.h>
 
int main() {
    int inputArray[100], elementCount, index, i, j, temp; 
 
    printf("Enter Number of Elements in Array\n");
    scanf("%d", &elementCount);
    printf("Enter %d numbers \n", elementCount);
     
    /* Read array elements */
    for(index = 0; index < elementCount; index++){
        scanf("%d", &inputArray[index]);
    }
     
    /* Sort Array in decreasing order using Bubble Sort */
    for(i = 0; i < elementCount; i++) {
        for(j = 0; j < elementCount-i-1; j++) {
            if(inputArray[j] < inputArray[j+1]) {
             /* Swap inputArray[j] and inputArray[j+1] */
                temp = inputArray[j];
                inputArray[j] = inputArray[j+1];
                inputArray[j+1] = temp;
            } 
        }
    }
      
     printf ("Sorted Array in Decreasing Order\n") ;
    /* Print Sorted Array */
    for(index = 0; index < elementCount; index++){
        printf("%d ", inputArray[index]);
    }
     
     getch();
 }

Output

Enter Number of Elements in Array
7
Enter 7 numbers
1 6 3 0 7 2 9
Sorted Array in Decreasing Order
9 7 6 3 2 1 0
Enter Number of Elements in Array
8
Enter 8 numbers
1 2 1 2 1 2 1 2 
Sorted Array in Decreasing Order
2 2 2 2 1 1 1 1

C Program to Print Fibonacci Series using Recursion

C Program to Print Fibonacci Series using Recursion 2

Fibonacci series are the numbers in the following integer sequence
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 ….
the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent term is the sum of the previous two terms. In mathematical terms, the Nth term of Fibonacci numbers is defined by the recurrence relation:

  • fibonacci(N) = Nth term in fibonacci series
  • fibonacci(N) = fibonacci(N – 1) + fibonacci(N – 2);
  • whereas, fibonacci(0) = 0 and fibonacci(1) = 1

Below program uses recursion to calculate Nth fibonacci number. To calculate Nth fibonacci number it first calculate (N-1)th and (N-2)th fibonacci number and then add both to get Nth fibonacci number.
For Example : fibonacci(4) = fibonacci(3) + fibonacci(2);

C program to print fibonacci series till Nth term using recursion

In below program, we first takes the number of terms of fibonacci series as input from user using scanf function. We are using a user defined recursive function named ‘fibonacci’ which takes an integer(N) as input and returns the Nth fibonacci number using recursion as discussed above. The recursion will terminate when number of terms are < 2 because we know the first two terms of fibonacci series are 0 and 1.In line number 17, we are calling this function inside a for loop to get the Nth term of series.

C program to print fibonacci series till Nth term using recursion

/*
* C Program to print fibonacci series using recursion
*/
#include <stdio.h>
#include <conio.h>
 
int fibonacci(int term);
int main(){
    int terms, counter;
    printf("Enter number of terms in Fibonacci series: ");
    scanf("%d", &terms);
    /*
     *  Nth term = (N-1)th therm + (N-2)th term;
     */
    printf("Fibonacci series till %d terms\n", terms); 
    for(counter = 0; counter < terms; counter++){
        printf("%d ", fibonacci(counter));
    }
    getch();
    return 0;
}
/*
 * Function to calculate Nth Fibonacci number
 * fibonacci(N) = fibonacci(N - 1) + fibonacci(N - 2);
 */
int fibonacci(int term){
    /* Exit condition of recursion*/
    if(term < 2)
       return term;
    return fibonacci(term - 1) + fibonacci(term - 2);
}

Program Output

Enter number of terms in Fibonacci series: 9
Fibonacci series till 9 terms
0 1 1 2 3 5 8 13 21

Fibonacci series till Nth term using memorization

Recursive program to print fibonacci series is not so efficient because it does lots of repeated work by recalculating lower terms again and again.

For Example:
fibonacci(6) = fibonacci(5) + fibonacci(4);
To calculate fibonacci(5) it will calculate fibonacci(4) and fibonacci(3). Now, while calculating fibonacci(4) it will again calculate fibonacci(3) which we already calculated while calculating fibonacci(5). We can solve this recalculation problem by memorizing the already calculated terms in an array.

In the below program, we are using an integer array named ‘fibonacciArray’ to store the already calculated terms of fibonacci series(Nth term of fibonacci series is stored at fibonacciArray[N-1]). To calculate the Nth term we add the last two fibinacci elements(N-1 and N-2th element) stored in array. Finally we store the Nth term also in array so that we can use it to calculate next fibonacci elements.

C Program to Print Fibonacci Series using Recursion 2

/*
* C Program to print fibonacci series using memorization
*/
#include <stdio.h>
#include <conio.h>
  
int main(){
    int terms, fibonacciArray[100] = {0}, counter;
    printf("Enter number of terms in Fibonacci series: ");
    scanf("%d", &terms);
    /*
     * fibonacciArray[N] = fibonacciArray[N-1] + fibonacciArray[N-2]
     */
    for(counter = 0; counter < terms; counter++){
        if(counter < 2){
            fibonacciArray[counter] = counter;
        } else {
            fibonacciArray[counter] = fibonacciArray[counter-1] +
                fibonacciArray[counter-2];
        }
        printf("%d ", fibonacciArray[counter]);
    }
    getch();
    return 0;
}

Program Output

Enter number of terms in Fibonacci series: 7
Fibonacci series till 7 terms
0 1 1 2 3 5 8

File Input Output in C Programming

C Program to Close a File using fclose Function

File in C programming language, can be anything from a disk file to a device. C language provide support for opening a file, closing a file, reading data from a file and writing data to a file through a set of standard library functions.

C file handling is designed to work with a number of devices and file formats like printer, screen, keyboard, disk file etc. C file system transforms different devices into a abstract logical device called stream. All streams behaves same irrespective of devices and files. It provides an abstract consistent interface for C program to interact by hiding the details of the device specific implementations. Disk file read function can be used to read data from keyboard as well.

Points to Remember about Streams

  • A Stream is a the sequence of bytes of data.
  • All Streams behaves similarly. Same code can be used to perform I/O operations of various streams.
  • A stream hides the low level device dependent complexities from C program.
  • In a C program, all input and Output is done through streams.
  • Three streams gets automatically attached when a program starts execution that is
  • Standard Input Stream(stdin)
  • Standard Output Stream(stdout)
  • Standard Error Stream(stderr)
  • Keyboard is associated with stdin stream.
  • Screen is associated with stdout stream.

FILE Pointer in C

A file pointer is a pointer to a structure of type FILE. A file pointer is associated with stream and manages all Input and Output operations of that stream. The FILE structure contains information which is required to perform any file I/O operations like name of the file, current location of position indicator in file, data transfer bugger, status flag etc.

A file pointer is a bridge between c program and file/stream. We can declare a FILE pointer as follows:

FILE *file;
Facts about FILE Structure

  • To perform any I/O operation on file, we need FILE pointer.
  • FILE Structure is defined in stdio.h header file.
  • FILE Structure contains necessary information about a stream, which is required to perform I/O operation in that stream.
  • FILE pointer is an interface for our programs to interact with files.

Opening a File in C

The stdio.h library function fopen() is used to create a new file or open an exiting file.

FILE *fopen(const char *filename, const char *mode);

Function fopen() opens the file whose name is given in the filename argument and associate it with a stream and return a FILE pointer to be used in any future I/O operations on this stream. The fopen function opens a stream in a particular mode, which defines the operations that are allowed on the stream.
Various Modes of Opening a File

Mode Description
“r” To read a file. Opens a file for reading. The file must exist.
“w” To write on a file. Creates an empty file for writing. If a file already exists with same name, its content is removed and the file is considered as a new empty file.
“a” To append data at the end of file. The file is created if it does not exist.
“r+” To read and write on an existing file. Opens a file to update both reading and writing. The file must exist.
“w+” To create a new file for reading and writing.
“a+” To read and append data on a file.

C Program to Open a File using fopen Function

The following program shows the use of fopen function to open a text file in read mode.

File Input Output in C Programming

#include <stdio.h>
 
int main(){
   FILE *file;
   int ch;
    
   /* Open a file for reading */
   file = fopen("textFile.txt","r");
   if(file == NULL){
      perror("Error: Unable to open a file");
   } else {
       while(!feof(file)){
          ch = fgetc(file);
          printf("%c", ch);
       }
       fclose(file);
   }
    
   return(0);
}

Output

fopen C Standard library function

Closing a File in C

The stdio.h library function fclose() is used to close a stream that was opened by fopen() function. Before closing a stream it flushes it’s buffer.

int fclose(FILE *stream);

Function fclose closes a stream whose file pointer is passed as argument. If a call to fclose is successful it returns zero, otherwise EOF is returned.

C Program to Close a File using fclose Function

The following program shows the use of fclose function to close a file(stream) after writing a sentence in it.

C Program to Close a File using fclose Function

#include <stdio.h>
 
int main (){
  FILE * pFile;
   
  /* Creates a new file */
  pFile = fopen ("TextFile.txt","w");
  /* Write a sentence in a file */
  fprintf (pFile, "fclose C standard library function");
  /* Close a file */
  fclose (pFile);
   
  return 0;
}

Output

fclose C standard library function

Reading from a File in C

The stdio.h standard library provides a number of functions to read data from a stream.
Functions to read a single character from a file

Function Description
fgetc() Reads a character from the given stream.
getc() Reads a single character from given stream.
getchar() Reads a single character from stdin stream.

Functions to Read a String from a File

Function Description
fgets() Reads a line from given stream and stores it into a character array.
gets() Reads a line from stdin and stores it into given character array.

Functions to Read Formatted Data from a File

Function Description
fscanf() Read formatted data from given stream.
scanf() Reads formatted data from stdin.

C Program to read formatted data from a file using fscanf function

The following program shows the use of fscanf function to read formatted data from a stream.

C Program to read formatted data from a file using fscanf function

#include <stdio.h>
 
int main()
{
   char string[50];
   int val;
   float fval;
   FILE *file;
 
   file = fopen ("textFile.txt", "w+");
   fprintf(file, "%d %f %s", 5, 5.5, "TechCrashCourse.com");
   rewind(file);
 
   fscanf(file, "%d %f %s", &val, &fval, string);
    
   printf("Integer : %d\n", val);
   printf("Floating point number : %f\n", fval);
   printf("String : %s\n", string);
 
   fclose(file);
   return(0);
}

Output

Integer : 5
Floating point number : 5.500000
String : TechCrashCourse.com

Writing to a File in C

The stdio.h standard library provides a number of functions to write data on a stream.

Functions to write a single character on a file

Function Description
fputc() Writes a character to the given stream.
putc() Writes a character to the given stream.
putchar() Writes a character to stdout stream.

Functions to write a string on a file

Function Description
fputs() Writes a string to the given stream excluding the null terminating character.
puts() Writes a string to stdout stream excluding null terminating character.

Functions to write formatted data on a file

Function Description
fprintf() Writes formatted output to a stream.
printf() Print formatted data to stdout.
vfprintf() Writes formatted data to a stream using an argument list.
vprintf() Print formatted data to stdout using an argument list.
vsprintf() Writes formatted data to a string using an argument list.

C Program to write formatted data on a file

C Program to write formatted data on a file

#include <stdio.h>
 
int main (){
   int c, counter = 0;
   FILE *file;
   char *string = "fprintf C Standard Library function";
    
   /* Create a temporary file */
   file = fopen("textFile.txt", "w");
   fprintf(file, "%s", string);
   fclose(file);
    
   /* Opening textFile.txt and printing it's content */
   file = fopen("textFile.txt", "r");
   while(!feof(file)){
       c = fgetc(file);
       printf("%c", c);
   }
    
   return(0);
}

Output

fprintf C Standard Library function

Standard Library function for File Handling in C

There are many file handling functions defined in stdio.h header file. Here is the list of frequently used file handling functions:

Function Description
clearerr() Clears error indicators associated with a given stream.
fclose() Closes the stream and flushes buffers associated with the given stream.
feof() Checks the end-of-file indicator of the given stream.
ferror() Checks the error indicator of the given stream.
fflush() Flushes the content of the given stream.
fgetc() Gets the next character from the given stream.
fgetpos() Gets current position of the given stream.
fgets() Reads a line from given stream and stores it into a character array.
fopen() Opens a file in the given mode.
fprintf() Writes formatted output to a stream.
fputc() Writes a character to the given stream.
fputs() Writes a string to the given stream excluding the null terminating character.
fread() Reads data from the given stream and stores it into an array.
freopen() Reopens a stream with different file or mode.
fscanf() Read formatted data from given stream.
fseek() Changes the position indicator of the given stream.
fsetpos() Sets the position indicator of the given stream.
ftell() Returns the current position of the given stream.
fwrite() Writes data from an array to the given stream.

C Program to Check Symmetric Matrix

  • Write a C program to check whether a matrix is symmetric matrix or not.

Required Knowledge

This program checks whether a given matrix is symmetric matrix or not. Here is the algorithm to check symmetric matrix.
Algorithm to find symmetric matrix
Let inputMatrix is an integer matrix having N rows and M columns.

  • Find transpose matrix of inputMatrix and store it in transposeMatrix. Check this C program to find transpose of a matrix.
  • Compare inputMatrix and transposeMatric. Check this C program to compare two matrix
  • If both matrices are equal then inputMatrix is symmetric matrix otherwise not a symmetric matrix

C program to check a matrix is symmetric matrix or not

C Program to Check Symmetric Matrix

#include <stdio.h>
#include <conio.h>
  
int main(){
    int rows, cols, row, col, size, isSymmetric;
    int inputMatrix[50][50], transposeMatrix[50][50];
     
    printf("Enter the size of Square Matrix\n");
    scanf("%d", &size);
    rows = cols = size;
     
    printf("Enter Matrix of size %dX%d\n", rows, cols);
      
    for(row = 0; row < rows; row++){
        for(col = 0; col < cols; col++){
            scanf("%d", &inputMatrix[row][col]);
        }
    }
      
    /* Find Transpose of inputMatrix 
 transpose[i][j] = inputMatrix[j][i] */
    for(row = 0; row < rows; row++){
        for(col = 0; col < cols; col++){
            transposeMatrix[col][row] = inputMatrix[row][col];
        }
    }
      
    /* Compare Input Matrix and its Transpose Matrix */
    isSymmetric = 1;
    for(row = 0; row < cols; row++){
        for(col = 0; col < rows; col++){
            if(inputMatrix[row][col] != transposeMatrix[row][col]){
                isSymmetric = 0;
            }
        }
    }
     
    if(isSymmetric == 1)
        printf("Input Matrix is Symmetric Matrix\n");
    else
        printf("Input Matrix is Not a Symmetric Matrix\n");
     
    getch();
    return 0;
}

Output

Enter the size of Square Matrix
3
Enter Matrix of size 3X3
4 5 6
5 9 1
6 1 2
Input Matrix is Symmetric Matrix
Enter the size of Square Matrix
3
Enter Matrix of size 3X3
1 2 3
4 5 6
7 8 9
Input Matrix is Not a Symmetric Matrix

C Program to Find Power of a Number using Recursion

The power of a number(baseexponent)is the base multiplied to itself exponent times.
For Example
ab = a x a x a …..x a(b times)
24 = 2x2x2x2 = 16

Here we will solve this problem using recursion. We will first take base and exponent as input from user and pass it to a recursive function, which will return the value of base raised to the power of exponent(baseexponent).

Algorithm to find power of a number using recursion

  • Base condition of recursion : A0 = 1; (anything to the power of 0 is 1).
  • To calculate An, we can first calculate An-1 and then multiply it with A(A^n = A X An-1).
  • Let getPower(int A, int n) is a function which returns An. Then we can write recursive expression as getPower(A, n) = A X getPower(A, n-1);

Time Complexity: O(n)

C program to find power of a number using recursion

Below program first takes base and exponent as input from user using scanf function and stores it in integer variables. It uses a user defined function getPower, that takes base and exponent as input parameters and returns the value of baseexponent. To calculate baseexponent, function getPower calls itself recursively as getPower(base, exponent-1) to calculate baseexponent-1
For Example:
23 = getPower(2,3) = 2 x getPower(2,2) = 2 x 2 x getPower(2,1) = 2 x 2 x 2 x getPower(2,0) = 2 x 2 x 2 x 1 = 8

C Program to Find Power of a Number using Recursion 1

/*
* C Program to find power of a number (a^b) using recursion
*/
#include <stdio.h>
#include <conio.h>
 
int main(){
    int base, exponent, counter, result = 1;
    printf("Enter base and exponent \n");
    scanf("%d %d", &base, &exponent);
     
    result = getPower(base, exponent);
     
    printf("%d^%d = %d", base, exponent, result);
    getch();
    return 0;
}
/*
 * Function to calculate base^exponent using recursion
 */
int getPower(int base, int exponent){
    /* Recursion termination condition,
     * Anything^0 = 1
     */
    if(exponent == 0){
        return 1;
    }
    return base * getPower(base, exponent - 1);
}

Program Output

Enter base and exponent 
4 0
4^0 = 1
Enter base and exponent 
3 4
3^4 = 81

C program to find power of a number using divide and conquer

To find the power of a number, we can use Divide and Conquer approach. A divide and conquer algorithm solves a problem in three steps.

  • It divides a problem into two or more sub problems. Such that each sub-problem is same as the original problem but for smaller data set.
  • It solve each sub-problem recursively and stores the solution of each sub-problem If required.
  • It combines the solutions of sub-problem to get the overall solution of original problem.

C program to find power of a number using divide and conquer 2

Below program contains a user defined function getPower, that takes base and exponent as input parameters and returns the value of baseexponent. Function getPower implements the divide and conquer algorithm mentioned above to calculate the power of a number.

getPower(A, n) = if n is even: getPower(A, n-1) X getPower(A, n-1); 
                 if n is odd: A X getPower(A, n-1) X getPower(A, n-1);

C Program to Find Power of a Number using Recursion 2

/*
* C Program to find power of a number (a^b) using recursion
*/
#include <stdio.h>
#include <conio.h>
 
int main(){
    int base, exponent, counter, result = 1;
    printf("Enter base and exponent \n");
    scanf("%d %d", &base, &exponent);
     
    result = getPower(base, exponent);
     
    printf("%d^%d = %d", base, exponent, result);
    getch();
    return 0;
}
/*
 * Function to calculate base^exponent using recursion
 */
int getPower(int base, int exponent){
    /* Recursion termination condition,
     * Anything^0 = 1
     */
    int result;
    if(exponent == 0){
        return 1;
    }
    result = getPower(base, exponent/2);
    if(exponent%2 == 0){
        /*Exponent is even */
        return result*result;
    } else {
        /*Exponent is odd */
        return base*result*result;
    }
}

Program Output

Enter base and exponent 
5 4
5^4 = 625

Artificial Intelligence Lecture Notes and Study Material PDF Free Download

artificial-intelligence-lecture-notes

Artificial Intelligence Lecture Notes: Graduates eyeing to get hold of the Artificial Intelligence Lecture Notes and Study Materials can avail the best notes and reference resources for their preparation process from this article.

The chief study material for better and comprehensive preparation is the Artificial Intelligence Lecture Notes as they offer comprehensive, accurate, and credible materials that help you score better grades. Students can refer and read through the Artificial Intelligence Lecture Notes as per the latest syllabus from this article.

Artificial Intelligence Lecture Notes aim to provide aspirants with detailed yet concise information on the subject matter and gives you an advantage as you additionally acquire the latest and updated Syllabus, Important list of Questions, and Reference Books on Artificial Intelligence course programme over regular notes. Students can access the latest Artificial Intelligence Lecture Notes and Study Materials PDF Free Download from this article.

Graduates can better prepare with the right strategy and approaches and achieve better grades.

Introduction to Artificial Intelligence Notes

Artificial Intelligence is a widespread advanced study concerned with the structure of smart machines. These machines are developed to perform tasks with prerequisite human intelligence. Artificial Intelligence is a multidisciplinary Science with multiple approaches. The advancements in Machine learning create a paradigm shift that alters the virtual sector in today’s world, especially in the tech industry.

The major limitation of Artificial Intelligence is that it fails to explain what artificial intelligence is all about; however, authors Norvig and Russell state four approaches that define the field of Artificial Intelligence.

For Example,- Siri is the best Example of Artificial Intelligence. Siri uses machine learning technology to decode and understand human language and answer accordingly. It is the most iconic Example of gadgets, machine learning abilities.

B.Tech/M.Sc Artificial Intelligence Lecture Notes and Study Material PDF Free Download

Graduates pursuing Bachelors in Technology (B.Tech) or Masters in Science (M.Sc) can avail from the Artificial Intelligence Lecture Notes and Study Material updated in this article. The study resources aim to help your preparation with these ultimate tools and help you score better grades.

Students can download the lecture notes and study material to refer to them during the preparation or revision process. The utilisation of the Artificial Intelligence Lecture Notes and Study Materials as a source reference will assist graduates in getting a comprehensive understanding of the concepts and changing your score chart.

Here, are a list of a few important lecture notes that provides a comprehensive preparation of the Artificial Intelligence course programme-

  • Artificial Intelligence Fourth Semester Lecture Notes for MSc. PDF
  • Artificial Intelligence Lecture Notes PDF
  • Artificial Intelligence Handwritten Lecture Notes PDFs
  • Artificial Intelligence Lecture Notes for CSE PDFs
  • Artificial Intelligence Programme Question Paper PDFs
  • Artificial Intelligence Fifth Semester Lecture Notes for B.Tech. PDF
  • Artificial Intelligence PPT Lecture Notes PDF
  • Artificial Intelligence CSC384 – Lectures Slides and Readings
  • Artificial Intelligence Lecture Notes for Bachelor of Technology in Computer Science and Engineering and Information Technology PDFs

Artificial Intelligence Reference Books

Books act as a portal to credible and well-researched information. Students should ensure to refer to the best reference books for the Artificial Intelligence course programme as per the subject experts’ recommendations. Students can refer and read through artificial Intelligence Books and other sources of reference to improve their learning, organise, and structure their preparation.

The list of best reference books for Artificial Intelligence preparation is as follows. However, students should consider a book that meets their knowledge and prepare accordingly.

  1. Book on Artificial Intelligence- from Fundamentals to Intelligent Searches by Qiangfu ZHAO and Tatsuo Higuchi
  2. Artificial Intelligence and Machine Learning book by Anand Hareendran S and Vinod Chandra S S
  3. Book on Artificial Neural Networks By B Yegnanarayana
  4. Artificial Intelligence- A New Synthesis by Nils J Nilsson and Elsevier
  5. Book on Introduction to Artificial Intelligence by Patterson
  6. CENGAGE Learning on Artificial Intelligence by Saroj Kaushik
  7. The Fifth Edition of Artificial Intelligence, Strategies, and Structures for Complex Problem Solving by George F Luger
  8. Book on Artificial Intelligence- A Modern Approach by Stuart Russell and Peter Norvig
  9. Artificial Intelligence and Innovations 2007 Book by Pnevmatikakis
  10. Book on Introduction to Artificial Intelligence by Wolf Gang, Ertel, and Springer
  11. The Fifth Edition of Artificial Intelligence by Rich, Shiv Shankar B Nair, and Kevin Knight
  12. Textbook of Artificial Intelligence by A Vikraman
  13. Book on Artificial Intelligence Engines- A Tutorial Introduction to the Mathematics of Deep Learning by James V Stone
  14. Machine Learning For Absolute Beginners Book by Oliver Theobald
  15. Book on Introduction to Artificial Intelligence by Shinji Araya

Artificial Intelligence Updated Syllabus

An effective way to improve your preparation is holding an initial idea, and an overview of the Artificial Intelligence updated Syllabus. Keeping in mind every student’s requirements and stipulations, we have provided a comprehensive outlook of the Artificial Intelligence syllabus.

Course Curriculum will instruct, manage, and structure students’ preparation and give you a clear overview of what to study and how to study. The unit-wise break up of syllabus gives students a clear idea of each unit so that students can allot time to each topic accordingly.

Students must ensure to cover all the topics and essential concepts before attempting the Artificial Intelligence course exam so that the test or exam paper is reasonably answerable at the time of the exam. Students must ensure to remain aware and updated of the Artificial Intelligence Syllabus as it stops you from squandering unwanted time on redundant topics.

The updated unit-wise breakup of the Artificial Intelligence Syllabus is as follows-

Unit- I- The Fundamental of Artificial Intelligence

Overview of Artificial Intelligence concerning approaches, methods, specific theories, and technologies.

Unit- II- Machine Learning

Principles and Critical Analysis of Machine Learning and their applications. An overview of predictive tools and systems.

Unit- III- Human Language technologies

Principles, Techniques, and Models of Natural language focusing on Statistical Machine Learning approach using modern programming libraries.

Unit- IV- Distributed systems: paradigms and models

Unit- V- Intelligent Systems for Pattern Recognition

Designs of A. I based on Solutions and Applications, Presentation of Image processing and signalling; Focus on Pattern recognition problems and models

Unit-VI- Smart applications

Novel Smart Applications, Implementation of Design, Prototype of Smartwork.

Unit- VII- Computational mathematics for learning and data analysis

Use of common Computational Mathematical tools in fields like Statistics, Data fitting, Artificial Intelligence, Approximation, Information Retrieval, and others.

Unit- VIII- Robotics

Fundamentals of Robotics, Application domain for Intelligence Systems, Computer Science, Schemes, and behaviour control

Unit- IX- Semantic web

Semantic Web Technologies, Linked Data, and Wen Languages

Unit- X- Computational modelling of complex systems

Advanced modelling approaches, Case study dynamics, economy, biochemistry, and epidemiology.

List of Artificial Intelligence Important Questions

Students pursuing Bachelors in Technology (B.Tech) or M.Sc can read through the list of all the essential questions mentioned below for the Artificial Intelligence course programme. The given review questions mentioned below aim to help the graduates to excel in the examination.

  • Write a short note on the Tower of Hanoi.
  • Explain briefly how Artificial Intelligence connects to Human-based Nature.
  • Explain how you would formulate a Constraint Satisfaction Problem.
  • Write a short note on the Turing Test.
  • Elucidate the PEAS description for a Vacuum cleaner and give your opinion about heuristic function.
  • Please write a short note on the problem-solving agents along with its algorithms.
  • Define parallel machines.
  • Write a short note on the capabilities of a computer for a Turing test.
  • Define a ridge.
  • Summarize the factors relating to rationality.
  • Formulate Constraint Satisfaction Problem on a brief note.
  • List the different categories of production systems.
  • Demonstrate the condition if a problem is decomposed.
  • Write a short note on Artificial, Alternate, Natural, and Compound keys.
  • Elucidate on the Hybrid Bayesian network.

FAQs on Artificial Intelligence Lecture Notes

Question 1.
Define Artificial Intelligence with examples.

Answer:
Artificial Intelligence is an advanced subject that deals with the stimulation of human intelligence in machines programmed to function and think like humans and impersonate human actions.

Example- Tesla is a good example of Artificial Intelligence that shows the shift of automobiles towards AI.

Question 2.
State the importance of Artificial Intelligence in today’s scenario?

Answer:
Artificial Intelligence automates the discovery and learning through data. It analyses deeper and abundant data and achieves accuracy. AI adds intelligence and adapts via advanced algorithms learning. Finally, Artificial Intelligence acquires the most out of data.

Question 3.
Name a few important questions for the Artificial Intelligence course programme.

Answer:

  • Elucidate the PEAS description for a Vacuum cleaner and give your opinion about heuristic function.
  • Please write a short note on the problem-solving agents along with its algorithms.
  • Define parallel machines.
  • Write a short note on the capabilities of a computer for a Turing test.
  • Define a ridge.

Question 4.
What are a few reference books that can elevate your exam preparation?

Answer:

  • The Fifth Edition of Artificial Intelligence, Strategies, and Structures for Complex Problem Solving by George F Luger
  • Book on Artificial Intelligence- A Modern Approach by Stuart Russell and Peter Norvig
  • Artificial Intelligence and Innovations 2007 Book by Pnevmatikakis
  • Book on Introduction to Artificial Intelligence by Wolf Gang, Ertel, and Springer
  • The Fifth Edition of Artificial Intelligence by Rich, Shiv Shankar B Nair, and Kevin Knight

Conclusion

The article on Artificial Intelligence Lecture Notes is a credible source of information. It provides accurate and reliable study materials, books and resources that aim to help and enhance a student’s knowledge and comprehension of the subject during preparations and at the time of examination. Students can refer and practice from the provided Artificial Intelligence Lecture Notes,  Books, Important Questions, and Study materials from this article.

Java Program to Count Total Number of Negative Elements in a Matrix

Java Program to Count Total Number of Negative Elements in a Matrix

In the previous article, we have discussed Java Program to Count Total Number of Positive Elements in a Matrix

In this article we are going to see how we can write a program to count the Total Number of Negative Elements in a matrix in JAVA language.

Java Program to Count Total Number of Negative Elements in a Matrix

A 3*3 Matrix is having 3 rows and 3 columns where this 3*3 represents the dimension of the matrix. Means there are 3*3 i.e. total 9 elements in a 3*3 Matrix.

Let’s understand it in more simpler way.

                   | A00   A01   A02 |
Matrix A =  | A10   A11   A12 |
                   | A20   A21   A22 | 3*3
  • Matrix A represents a 3*3 matrix.
  • A‘ represents the matrix element
  • Aij‘ represents the matrix element at it’s matrix position/index.
  • i‘ represents the row index
  • j‘ represents the column index
  • Means A00=Aij  where i=0 and j=0,  A01=aij where i=0 and j=1 and like this.
  • Here we have started row value from 0 and column value from 0.

Negative elements in a matrix are the elements which are less than 0.

Let’s see different ways to count total number of negative elements in a Matrix.

Method-1: Java Program to Count Total Number of Negative Elements in a Matrix By Static Initialization of Array Elements

Approach:

  • Declare and initialize an array of size 3×3, with elements.
  • Use two for loops to iterate the rows and columns.
  • Inside the for loops count all the negative elements using a counter.
  • Print the result.

Program:

public class matrix
{
    public static void main(String args[])
    {
        // Initializing the 3X3 matrix i.e. 2D array
        int arr[][] = {{19,25,-32},{40,-54,-62},{-70,-20,60}};
        int row, col ,count = 0;

        System.out.print("The matrix elements are : ");
        printMatrix(arr);

        // Loops to count total number of negative elements in a matrix
        for(row=0;row<3;row++)
            for(col=0;col<3;col++)
            {
                if(arr[row][col]<0)
                    count++;
            }   
        
        System.out.println("\nNumber of negative elements in the matrix are : "+count);
    }

    // Method to print the matrix
    static void printMatrix(int arr[][])
    {
        int row, col;
        // Loop to print the elements
        for(row=0;row<3;row++)
        {
            // Used for formatting
            System.out.print("\n");
            for(col=0;col<3;col++)
            {
                System.out.print(arr[row][col]+" ");
            }
        }
        System.out.print("\n");
    }
}

Output:

The matrix elements are : 
19 25 -32 
40 -54 -62 
-70 -20 60

Number of negative elements in the matrix are : 5

Method-2: Java Program to Count Total Number of Negative Elements in a Matrix By Dynamic Initialization of Array Elements

Approach:

  • Declare one array of size 3×3.
  • Ask the user for input of array elements and store them in the array using two for loops.
  • Use two for loops to iterate the rows and columns .
  • Inside the for loops count all the negative elements using a counter.
  • Print the result.

Program:

import java.util.Scanner;
public class matrix{
    public static void main(String args[])
    {
        //Scanner class to take input
        Scanner scan = new Scanner(System.in);

        // Initializing the 3X3 matrix i.e. 2D array
        int arr[][] = new int[3][3];
        int row, col ,count = 0;

        // Taking matrix1 input
        System.out.println("Enter matrix elements : ");
        for(row=0;row<3;row++)
            for(col=0;col<3;col++)
                arr[row][col] = scan.nextInt();


        System.out.print("The matrix elements are : ");
        printMatrix(arr);

        // Loops to count total number of negative elements in a matrix
        for(row=0;row<3;row++)
            for(col=0;col<3;col++)
            {
                if(arr[row][col]<0)
                    count++;
            }   
        
        System.out.println("\nNumber of negative elements in the matrix are : "+count);
    }

    // Method to print the matrix
    static void printMatrix(int arr[][])
    {
        int row, col;
        // Loop to print the elements
        for(row=0;row<3;row++)
        {
            // Used for formatting
            System.out.print("\n");
            for(col=0;col<3;col++)
            {
                System.out.print(arr[row][col]+" ");
            }
        }
        System.out.print("\n");
    }
}

Output:

Enter matrix elements : 
The matrix elements are : 
0 6 -1 
-2 3 8 
7 0 5

Number of negative elements in the matrix are : 2

Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output

Related Java Programs: