IF Statement Programming Exercise Part-1
What is IF Statement in C and C++ Programming
Program 1:
Write a program that inputs values into the individual elements of an array during program execution and displays the values of the array. No loop statement should be used in the program.
#include<iostream> using namespace std; main() { int temp[5]; cout<<"Enter value in 1st element of temp. ? "; cin>>temp[0]; cout<<"Enter value in 2nd element of temp. ? "; cin>>temp[1]; cout<<"Enter value in 3rd element of temp. ? "; cin>>temp[2]; cout<<"Enter value in 4th element of temp. ? "; cin>>temp[3]; cout<<"Enter value in 5th element of temp. ? "; cin>>temp[4]; cout<<"Values in array temp are:"<<endl; cout<<temp[0]<<endl; cout<<temp[1]<<endl; cout<<temp[2]<<endl; cout<<temp[3]<<endl; cout<<temp[4]<<endl; }
Program 2:
Write a program that assigns values into the individual elements of an array and displays the values of the array.
#include<iostream> using namespace std; main() { int temp[5], i; temp[0] = 2; temp[1] = 20; temp[2] = 15; temp[3] = 254; temp[4] = 5; for(i = 0; i<=4; i++) cout<<"Value in temp["<<i<<"] = "<<temp[i]<<endl; }
Program 3:
Write a program that assigns value 50 to all the elements of an array “abc” and then displays these values on the screen.
#include<iostream> using namespace std; main() { int abc[15], i; // Assign values to array “abc” for(i = 0; i <= 14; i++) abc[i] = 50; // Display contents of array “abc” for(i = 0; i<=14; i++) cout << abc[i] << endl; }
Program 4:
Write a program that gets 5 integer values into an array and then displays the values of the elements of array in reverse order.
#include<iostream> using namespace std; main() { int arr[5], i; for(i=0; i<=4; i++) { cout<<"Enter value in element "<<i<<" ? "; cin>>arr[i]; } cout<<"Output in reverse order"<<endl; for(i=4; i>=0; i--) { cout<<arr[i]<<endl; } }
Program 5:
Write a program that inputs temperatures for seven days of the week into a one-dimensional array “temp”. It computes the average temperature and displays the values of array and calculated average temperature on the screen.
#include<iostream> using namespace std; main() { float temp[7], sum = 0; int i=0; while(i<=6) { cout<<"Enter temp. of day "<<i+1<<" ? "; cin>>temp[i]; sum = sum + temp[i]; i++; } cout<<"\nAverage temperature : "<<sum/7.0; }
Program 6:
Write a program that inputs values into one-dimensional array and finds out the total number of odd and even values entered into one-dimensional array.
#include<iostream> using namespace std; main() { int arr[10], i, odd, even; i = odd = even = 0; while(i<=9) { cout<<"Enter value into element "<<i<<"?"; cin>>arr[i]; (arr[i]%2==0)? even++ : odd++; i++; } cout<<"\nEven Values : "<<even; cout<<"\nOdd Values : "<<odd; }
Program 7:
Develop a program that inputs ‘n’ values in a list and prints the product of the value and the position at which it resides in the list, i.e. product L(1)*1, product L(2)*2, and so on. For example:
// Position 1 2 3 4 5
// Value 5 3 7 1 4
// Output 5 6 21 4 20
// Note: The list does not change.
#include<iostream> using namespace std; main() { int arr[5], i; i = 0; while(i<=4) { cout<<"Enter value into element "<<i<<"?"; cin>>arr[i]; i++; } cout<<"\n products of the values and the positions: "<<endl; for(i = 0; i<=4; i++) cout<<arr[i]<<"*"<<i+1<<" = "<< arr[i]*(i+1)<<endl; }