voidprime(); // Function prototype intmain() { // No argument is passed to prime() prime(); return0; }
// Return type of function is void because value is not returned. voidprime() { int num, i, flag = 0; cout << "Enter a positive integer enter to check:"; cin >> num; for(i = 2; i <= num/2; ++i) { if(num % i == 0) { flag = 1; break; } } if (flag == 1) { cout << num << "is not a prime number."; } else { cout << num << "is a prime number."; } }
intmain() { int num, i, flag = 0; // No argument is passed to prime() num = prime(); for (i = 2; i <= num/2; ++i) { if (num%i == 0) { flag = 1; break; } } if (flag ==1) { cout << num << " is not a prime number."; } else { cout << num << " is a prime number"; } return0; }
// Return type of function is int intprime() { int n; printf("Enter a positive integer to check: "); cin >> n; return n; }
intmain() { int num; cout << "Enter a positive integer to check: "; cin >> num; // Argument num is passed to the function prime() prime(num); return0; }
// There is no return value to calling function. Hence, return type of function is void voidprime(int n) { int i, flag = 0; for(i = 2; i <= n/2; ++i) { if (n%i == 0) { flag = 1; break; } } if (flag ==1) { cout << n << " is not a prime number."; } else { cout << n << " is a prime number."; } }
intmain() { int num, flag = 0; cout << "Enter positive integer to check: "; cin >> num; // Argument num is passed to check() function flag = prime(num); if(flag == 1) cout << num << " is not a prime number."; else cout << num << " is a prime number."; return0; }
// This function return integer value. intprime(int n) { int i; for(i = 2; i <= n/2; ++i) { if(n%i == 0) return1; } return0; }
// Program to compute absolute value // Works for both int and float
#include<iostream> usingnamespacestd;
// function with float type parameter floatabsolute(float var) { if (var < 0.0) var = -var; return var; }
// function with int type parameter intabsolute(int var) { if (var < 0) var = -var; return var; }
intmain() { // call function with int type parameter cout << "Absolute value of -5 = " << absolute(-5) << endl; // call function with float type parameter cout << "Absolute value of 5.5 = " << absolute(5.5f) << endl; return0; }
/* OUTPUT Absolute value of -5 = 5; Absolute value of 5.5 = 5.5; */
// function with 2 parameters voiddisplay(int var1, double var2) { cout << "Integer number: " << var1; cout << " and double number: " << var2 << endl; }
// functon with double type single parameter voiddisplay(double var) { cout << "Double number: "<< var << endl; }
// function with int type single parameter var display(int var) { cout << "Integer number: " << var << endl; }
intmain() { int a = 5; double b = 5.5; // call function with int type parameter display(a); // call function with double type parameter display(b); // call function with 2 parameters display(a, b); return0; }
intmain() { int numbers[5] = {7, 5, 6, 12, 35}; cout << "The numbers are: "; // Printing array elements // using rage based for loop for (coust int &n : numbers) { cout << n << " "; } cout << "\nThe numbers are: "; // Printing array elements // using traditional for loop for (int i = 0; i < 5; ++i) { cout << numbers[i] << " "; } return0; }
/* OUTPUT The numbers are: 7 5 6 12 35 The numbers are: 7 5 6 12 35 */
intmain() { int numbers[5]; cout << "Enter 5 numbers: " << endl; // store input from user to array for (int i = 0; i < 5; ++i) { cin >> numbers[i]; } cout << "The numbers are: "; // print array elements for (int n = 0; n < 5; ++n) { cout << numbers[n] << " "; } return0; }
intmain() { // initialize an array without specifying size double numbers[] = {7, 5, 6, 12, 35, 27}; double sum = 0; double count = 0; double average; cout << "The numbers are: "; // print array elements // use of range-based for loop for(constdouble &n : numbers) { cout << n << " "; //caculate the sum sum += n; // cout the no. of array elements ++count; } // print the sum cout << "\nTheir Sum = " << sum << endl; // find the average average = sum / count; cout << "Their Average = " << average << endl; return0; }
/* OUTPUT The numbers are: 7 5 6 12 35 27 Their Sum = 92 Their Average = 15.3333 */
// use of nested for loop // access rows of the array for (int i = 0; i < 3; ++i) { //access columns of the array for (int j = 0; j < 2; ++j) { cout << "test[" << i << "][" << j << "] = " << test[i][j] << endl; } } return0; }
// declare function to display marks // take a 1d array as parameter voiddisplay(int m[5]) { cout << "Displaying marks: " << endl; // dislay array elements for (int i = 0; i < 5; ++i) { cout << "Student" << i + 1 << ": " << m[i] << endl; } }
intmain() { // declare and initialize an array int marks[5] = {88, 76, 90, 61, 69}; // call display function // pass array as argument dislay(marks); return0; }