Showing posts with label Function. Show all posts
Showing posts with label Function. Show all posts

C++ Program to swap two numbers using function.

How to write a C++ Program to swap two numbers using function in C++ Programming ?

c program to swap two numbers using function call by value write a program to swap two numbers using function in c++ program to swap two numbers using functions in java program to swap two numbers using pointers and functions c program to swap two numbers using pointers without using third variable program to swap two numbers using call by reference in c++ program to swap two numbers using bitwise operators program to swap two numbers without using temporary variable
C++ Program to swap two numbers using function.


Solution:
//Program to swap two numbers using function.
#include<iostream.h>
#include<conio.h>
void swap(int&,int&);
void main()
{
clrscr();
int a,b,c;
cout<<"Enter one numbers: ";
cin>>a;
cout<<"Enter second number: ";
cin>>b;
swap(a,b);
cout<<a<<" "<<b;
getch();
}
void swap(int &a,int &b)
{
int c;
c=a;
a=b;
b=c;
}

This C++ Program to swap two numbers using function.

Array-Function "oddsAndEvens" loops a parameter "nums"

Write a function below called "oddsAndEvens" that loops through a parameter "nums" (an array). For each number in the given array, if it is even, it is added to the evens array, if the number is odd, is added to the odds array.

Question has two parts:
1.Write a function below called "oddsAndEvens" that loops through a parameter "nums" (an array).
2.For each number in the given array, if it is even, it is added to the evens array, if the number is odd, is added to the odds array.

Solution:
var evens = [];
var odds = [];

//code here
var oddsAndEvens = function(nums) {
  if (nums % 2 === 0) {
    evens.push(nums);
  } else if (nums % 2 != 0) {
    odds.push(nums);
  }
}

What is Array ?

An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.