C printf array – C Program to Read and Print Elements of an Array

  • Write a C program to read and print elements of an array using for loop.
  • How to take input of array elements using scanf function and for loop.

Required Knowledge

Reading Array Elements
C printf array: We can use scanf function to take a number as input from user and store it in integer array at index i as follows.

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

We will use a for loop to take N inputs from user and store them in array from location 0 to N-1.

Printing Array Elements
How to print array in c: We can use printf function to print an array element at index i as follows.

printf("%d ", inputArray[i]);

We will use a for loop to traverse an array from index 0 to N-1 and print the elements at corresponding indexes.

C program to read and print array elements using scanf, printf and for loop

C Program to Read and Print Elements of an Array

/*
* C Program to read array elemnts and print 
* array elements on screen  
*/
#include <stdio.h>
#include <conio.h>
  
int main(){
    int inputArray[500], elementCount, counter;
      
    printf("Enter Number of Elements in Array\n");
    scanf("%d", &elementCount);
    printf("Enter %d numbers \n", elementCount);
     
    /* Read array elements one by one using for loop and 
 stores then in adjacent locations starting form index 0*/
    for(counter = 0; counter < elementCount; counter++){
        scanf("%d", &inputArray[counter]);
    }
        
    /* Print array */
    printf("Array Elements\n");
    for(counter = 0; counter < elementCount; counter++){
        printf("%d ", inputArray[counter]);
    }
          
    getch();
    return 0;
}

Output

Enter Number of Elements in Array
5
Enter 5 numbers
8
3
5
1
6
Array Elements
8 3 5 1 6

atol c – atol C Library Function

atol C Library Function

atol c: The function atol converts a string to a long integer(long int). The function skips all white-space characters at the beginning of the string until the first non-whitespace character is found. Then it converts the subsequent characters into a long integer number until it finds a character which is not a digit. It also takes an optional initial plus or minus sign before first base-10 digit in string.

Function prototype of atol

long int atol(const char *str);
  • str : A pointer to a string beginning with the representation of an long integer number.

Return value of atol

The function returns the converted integer number as a long int value otherwise it returns zero(0), If no valid conversion could be performed.

C program using atol function

The following program shows the use of atol function for string to long integer conversion.

atol C Library Function

#include <stdio.h>
#include <stdlib.h>
 
int main(){
    char string[100];
    long value;
    printf("Enter a string\n");
    scanf("%s", &string);
     
    /* String to Long integer conversion */
    value = atol(string);
    printf("Long value %ld\n", value);
      
    return 0;
}

Output

Enter a string
1234
Long value 1234
Enter a string
1234.555
Long value 1234
Enter a string
1234ABCD
Long value 1234
Enter a string
ABCD
Long value 0

Java program to add two numbers – Java Program to Add Two Numbers

Java program to add two numbers: Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.

Java Program to Add Two Numbers

  • Java program to find sum of two numbers.
  • Write a java program to add two numbers and print it on screen.

To add two numbers in a java program, we will first take two integers as input from user using Scanner and store them in a integer variable num1 and num2. Then we add num1 and num2 using + arithmetic operator and store it in integer variable “sum”. Finally, we print the sum of two given integers on screen.

Java Program to Find Sum of Two Numbers

Java Program to Find Sum of Two Numbers

package com.tcc.java.programs;
 
import java.util.*;
 
/**
 * Java Program to find sum of two numbers
 */
public class SumTwoNumbers {
    public static void main(String[] args) {
        int num1, num2, sum;
        Scanner scanner;
 
        scanner = new Scanner(System.in);
 
        // Take two integer input from user and
        // store it in variable num1 and num2
        System.out.println("Enter First Number");
        num1 = scanner.nextInt();
 
        System.out.println("Enter Second Number");
        num2 = scanner.nextInt();
 
        // Add two numbers using + operator
        sum = num1 + num2;
        System.out.println("Sum of " + num1 + " and " + num2 + " = " + sum);
    }
}

Output

Enter First Number
5
Enter Second Number
7
Sum of 5 and 7 = 12

Java Programs to Find Sum of Two Numbers using Function

This Java program is similar to above program except here we are using a user defined function getSum to calculate the sum of two integers. The function getSum takes two integer arguments and returns the sum of both input parameters.
Java Programs to Find Sum of Two Numbers using Function

package com.tcc.java.programs;
 
import java.util.*;
 
/**
 * Java Program to find sum of two numbers using function
 */
public class SumTwoNumbersFunction {
 
    public static void main(String[] args) {
        int num1, num2, sum;
        Scanner scanner;
 
        scanner = new Scanner(System.in);
 
        // Take two integer input from user and
        // store it in variable num1 and num2
        System.out.println("Enter First Number");
        num1 = scanner.nextInt();
 
        System.out.println("Enter Second Number");
        num2 = scanner.nextInt();
 
        /*
         * Call getSum by passing num1 and num2 as parameter
         */
        sum = getSum(num1, num2);
        System.out.println("Sum of " + num1 + " and " + num2 + " = " + sum);
 
    }
 
    /**
     * Function to add two numbers
     */
    public static int getSum(int num1, int num2) {
        int sum;
        sum = num1 + num2;
        return sum;
    }
}

Output

Enter First Number
9
Enter Second Number
12
Sum of 9 and 12 = 21

Exponent c++ – C++ Program to Find Power of a Number

Exponent c++: In the previous article, we have discussed C++ Program to Convert Decimal Number to HexaDecimal Number. In this article, we will see C++ Program to Find Power of a Number.

C++ Program to Find Power of a Number

  • Write a C++ Program to calculate power of a number using pow function.
  • How to find power of a number using using multiplication.

C++ Program to calculate power of a number using loop

C++ Program to calculate power of a number using loop

#include <iostream>
 
using namespace std;
  
int main(){
    int base, exp, i, result = 1;
     
 cout << "Enter base and exponent\n";
    cin >> base >> exp;
     
    // Calculate base^exponent by repetitively multiplying base
    for(i = 0; i < exp; i++){
        result = result * base;
    }
      
    cout << base << "^" << exp << " = " << result;
     
    return 0;
}

Output

Enter base and exponent
3 4
3^4 = 81

C++ Program to calculate power of a number using pow function

C++ Program to calculate power of a number using pow function

#include <iostream>
#include <cmath>
 
using namespace std;
 
int main() {
    int base, exp;
 
    cout << "Enter base and exponent\n";
    cin >> base >> exp;
 
    cout << base << "^" << exp << " = " << pow(base, exp);
    return 0;
}

Output

Enter base and exponent
5 3
5^3 = 125

Are you in search of Sample C++ Program to understand the logic for fundamental C++ programming language concepts? Check out the list of C++ Sample Program ranging from basic to complex ones at our end.

isspace() c – isspace C Library function

isspace C Library function

isspace function checks whether a character is white-space character or not. White-space characters are vertical tab(‘\v’), horizontal tab(‘\t’), newline(‘\n’), form feed(‘\f’), carriage return(‘\r’), and space(‘ ‘).

Function prototype of isspace

int isspace(int c);

Function isspace takes a character as input in form of an integer. When we pass a value of type char to isspace() function, corresponding ASCII value of that character is passed.

Return value of isspace

isspace() c: If passed character is a white-space character, then isspace function returns non-zero(true) integer otherwise 0(false).

C program using isspace function

Isspace c: The following program is to check whether a character is a white-space character or not.

isspace C Library function

#include <stdio.h>
#include <ctype.h>
#include <conio.h>
 
int main(){
    char string[] = "A \t";
    int index = 0;
     
    while(string[index] != '\0'){
        if(isspace(string[index])){
            printf("'%c' is a space character\n", string[index]);
        } else {
            printf("'%c' is not a space character\n", 
                string[index]);
        }
        index++;
    }
     
    getch();
    return 0;
}

Output

'A' is not a space character
' ' is a space character
'        ' is a space character

Sum of gp series – C Program to Print Geometric Progression (GP) Series and it’s Sum till N Terms

  • Write a C program to print geometric series till Nth term
  • Write a C program to find sum of geometric series till Nth term

Geometric series is a sequence of terms in which next term is obtained by multiplying common ration to previous term. The (n+1)th term of GP can be calculated as
(n+1)th = nth x R
where R is the common ratio (n+1)th/nth
The formula to calculate Nth term of GP : tn = a x rn-1
where, a is first term of GP and r is the common ratio.

C program to print geometric progression series and it’s sum till N terms

Sum of gp series: In this program, we first take number of terms, first term and common ratio as input from user using scanf function. Then we calculate the geometric series using above formula(by multiplying common ratio to previous term) inside a for loop. We keep on adding the current term’s value to sum variable.

/*
* C program to generate Geometric Series and it's sum till Nth term
*/
#include <stdio.h>
#include <stdlib.h>
 
int main() {
    int first, ratio, terms, value, sum=0, i;
 
    printf("Enter the number of terms in GP series\n");
    scanf("%d", &terms);
 
    printf("Enter first term and common ratio of GP series\n");
    scanf("%d %d", &first, &ratio);
 
    /* print the series and add all elements to sum */
    value = first;
    printf("GP SERIES\n");
    for(i = 0; i < terms; i++) {
        printf("%d ", value);
        sum += value;
        value = value * ratio;
    }
 
    printf("\nSum of the GP series till %d terms is %d\n", terms, sum);
 
    getch();
 return 0;
}

Program Output:

Enter the number of terms in GP series
6
Enter first term and common ratio of GP series
2 4
GP SERIES
2 4 8 16 32 64
Sum of the GP series till 6 terms is 126

C ceil function – ceil C Library Function

ceil C Library Function

C ceil function: The function double ceil(double x); returning the smallest integral value greater than or equal to x.

Function prototype of ceil

double ceil(double x);

  • x : A floating point value to round up.

Return value of ceil

ceil() in c: Function ceil returns the smallest integral value greater than or equal to x(as a floating-point value).

C program using ceil function

Ceil function in c: The following program shows the use of ceil function to round up value of floating point number.

ceil C Library Function

#include <stdio.h>
#include <math.h>
 
int main ()
{
  double x, result;
  printf("Enter a number\n");
  scanf("%lf", &x);
   
  result = ceil(x);
  printf("Smallest integer >= of %lf is %lf\n", x, result);
   
  return 0;
}

Output

Enter a number
2.3
Smallest integer >= of 2.300000 is 3.000000
Enter a number
-2.3
Smallest integer >= of -2.300000 is -2.000000

Static and Register variables in C

What is static variable in C

Register variables in c: Static variables retain their values between function calls. We can declare static variable by adding static keyword before data type in variable declaration statement.

static data_type variable_name;
For Example, 
    static int sum;
  • Static keyword has different effect on local and global variables.
  • For local static variables, compiler allocates a permanent storage in heap like global variable, so that they can retain their values between function calls. Unlike global variables, local static variables are visible only within their function of declaration.
  • For global static variables, compiler creates a global variable which is only visible within the file of declaration.
  • Variables declared static are initialized to zero(or for pointers, NULL) by default.

What are the properties of a register variable in C

  • The scope of register variables are same as automatic variables, visible only within their function.
  • You a only declare local variables and formal parameters of a function as register variables, global register variables are not allowed.
  • Declaring a variable as register is a request to the compiler to store this variable in CPU register, compiler may or may not store this variable in CPU register(there is no guarantee).
  • Frequently accessed variables like loop counters are good candidates for register variable.

What is the difference between variable declaration and variable definition in C

Declaration of a variable declares the name and type of the variable whereas definition of a variable causes storage to be allocated for the variable. There can be more than one declaration of the same variable but there can be only one definition for the variable.

In most cases, variable declaration and definition are same. However you can declare a variable without defining it by preceding a variable name with extern specifier.

How to multiply in c++ – C++ Program to Multiply Two Numbers Without Using ‘*’ Operator

How to multiply in c++: In the previous article, we have discussed C++ Program to Copy String Without Using strcpy. In this article, we will see C++ Program to Multiply Two Numbers Without Using ‘*’ Operator.

C++ Program to Multiply Two Numbers Without Using ‘*’ Operator

  • How to multiply two numbers using addition “+” operator only in C++.

C++ Program to multiply two numbers using addition

C++ Program to multiply two numbers using addition

#include <iostream>
 
using namespace std;
 
int multiply(int a, int b) {
  int result = 0;
  // Add integer a to result b times
  while(b != 0) {
      result = result + a;
      b--;
  }
  return result;
}
 
int main () {
  int a, b;
  cout << "Enter two integers" << endl;
  cin >> a >> b;
  cout << a << " X " << b << " = " << multiply(a, b);
  return 0;
}
</iostream>

Output

Enter two integers
4 5
4 X 5 = 20

Write simple C++ programs to print a message, swap numbers, and many more to get an idea on how the program works. Here given C++ Programs for Practice are useful to master your coding skills and learn various topics.

Which driver is called as thin-driver in jdbc – Java JDBC Driver | Types of Java Database Connectivity Drivers and Comparson

Which driver is called as thin-driver in jdbc: JDBC Driver is a software component that is used to interact with the java application with the database. The purpose of JDBC driver is to convert java calls into database-specific calls and database-specific calls into java calls.

Types of JDBC Drivers:

  1. JDBC-ODBC bridge driver.
  2. Native API driver
  3. Network protocol driver.
  4. Pure java driver.

1) JDBC-ODBC bridge Driver :

  • This driver is also known as Type-1 Driver. Internally this Driver will take the support of ODBC Driver to communicate with the database.Type-1 Driver convert JDBC calls into ODBC calls and ODBC Driver convert ODBC calls into database-specific calls.
  • Using Type-1 Driver for prototyping only and not for production purposes.
  • This driver can be provided by Sun Microsystem as a part of JDK.

JDBC-ODBC bridge Driver

Advantages :

  • Type-1 Driver is database-independent driver.
  • it is very easy to use.
  • Not require to install this driver separately(By default in windows)

Disadvantages :

  • It is the slowest driver.
  • Type-1 driver internally depends upon ODBC driver so ODBC driver concept application only on window machine i.e. platform-dependent driver.

2) Native API Driver :

  • Native API driver converts JDBC calls into database-specific native libraries calls and these calls are directly understood by the database engine.
  • Large database vendors, such as Oracle and IBM, use the Type-2 driver for their enterprise databases.
  • Type-2 Drivers aren’t architecturally compatible.
  • Type-2 Drivers force developers to write platform-specific code.

Native API Driver

Advantages :

  • Good performance as compared to the type-1 driver.
  • No ODBC Driver require.
  • Type-2 Drivers are operating system-specific and compiled.

Disadvantages :

  • It is a database-dependent driver.
  • It is a platform-dependent driver.
  • Only Oracle provides type-2 Driver

3) Network protocol driver:

  • For database middle-ware, Type-3 JDBC drivers are pure Java drivers.
  • java application communicates with Network Protocol driver. Network protocol driver converts JDBC calls into middle-wear specific calls, the middle-wear server communicates with database, middle-wear server convert middle-wear specific calls into database-specific calls.

Network protocol driver

Advantages :

  • This driver does not directly communicate with the database. So it is database-independent driver. For any database, this driver is the same.
  • It is fully written in Java. So it is platform-independent driver.
    No client-side libraries are required.

Disadvantages:

  • Network support is required on the client machine.
  • When we change the database, we need to change the middle-wear code.
  • Maintenance of Network Protocol driver becomes costly.

4) Pure Java Driver:

  • It is also known as the Thin driver. The thin driver converts JDBC calls into database-specific calls directly.
  • Thin driver directly communicates with the database by using database-specific native protocol provided by the database vendor.
  • It is a platform, independent Driver.

Pure Java Driver

Advantage:

  • Better performance than all other drivers.
  • Platform independent Driver.
  • No software is required at the client-side or server side.

Disadvantages:

  • Database dependent driver, because it is directly communicating with the database direct.

Which JDBC Drivers should be used :

Which driver is called as thin driver in jdbc: In our application, if you are using only one type of database may be an oracle,
MySQL then we should go for the type-1 driver.

Example: Standalone application,Small scale web application.

In our application, if you are using multiple databases highly recommended, you should go for the type-3 driver. Because the type-3 driver is database independent driver.

Example: large scale web application,Enterprise application.

To connect to multiple databases, developers can use type-2 Driver.

if no driver is available then we should go for type-4 driver

Differentiate between Thick and Thin Driver:

Differentiate between Thick and Thin Driver
Differentiate between Thick and Thin Driver

Thick Driver:

if the database driver requires some extra component to communicate with database such type of driver is called the Thick driver.

Example: Type-1,Type-2,Type-3 Driver.

Thin Driver:

if the database driver does not require some extra component to communicate with database such type of driver is called the Thin driver.

Example: Type-4 Driver.

Comparison of all types of JDBC Drivers:

Comparison of all types of JDBC Drivers