In: Computer Science
Code Without Snippet Using One For Loop:
#include <iostream>
using namespace std;
int main()
{
// Declare the array of size 49
int my_array[49];
// Initialize the array. Initialising with consecutive
numbers
for(int i=0;i<49;i++)
{
my_array[i]=i;
}
// Printing the array before changing
cout<<"Before Changing: "<<endl;
for(int i=0;i<49;i++)
{
cout<<my_array[i]<<" ";
}
// Using One For Loop
// Now Replacing every even number with 3 and Odd with 7
for(int i=0;i<49;i++)
{
// Checking if the element is Even
if(my_array[i]%2==0)
{
my_array[i]=3;
}
// Checking if the element is Odd
else
{
my_array[i]=7;
}
}
cout<<endl;
// Printing the result after changing
cout<<"After Changing using For Loop: "<<endl;
for(int i=0;i<49;i++)
{
cout<<my_array[i]<<" ";
}
}
Code With Snippet Using One For Loop:
Output Snippet Using One For Loop:
Code Without Snippet Using While Loop:
#include <iostream>
using namespace std;
int main()
{
// Declare the array of size 49
int my_array[49];
// Initialize the array. Initialising with consecutive
numbers
for(int i=0;i<49;i++)
{
my_array[i]=i;
}
// Printing the array before changing
cout<<"Before Changing: "<<endl;
for(int i=0;i<49;i++)
{
cout<<my_array[i]<<" ";
}
// Using One While Loop
// Now Replacing every even number with 3 and Odd with 7
int counter=0;
while(counter<49)
{
// Checking if the element is Even
if(my_array[counter]%2==0)
{
my_array[counter]=3;
}
// Checking if the element is Odd
else
{
my_array[counter]=7;
}
counter++;
}
cout<<endl;
// Printing the result after changing
cout<<"After Changing using While Loop: "<<endl;
for(int i=0;i<49;i++)
{
cout<<my_array[i]<<" ";
}
}
Code With Snippet Using While Loop:
Output Snippet Using One While Loop:
Thank you!!!