Showing posts with label LOOPING. Show all posts
Showing posts with label LOOPING. Show all posts

C++ Program to shuffle elements in pairs [DEVCPP/GCC]

OBJECTIVE

We have to shuffle the elements of an array such that each element, starting from the beginning of array is interchanged with its successor element.


PROGRAM

// C++ Program to shuffle elements in pairs

#include <iostream>
#define N 10

using namespace std;

int main()
{
        int i,temp,list[N];

        cout<<"ENTER ELEMENTS \n";

        for( i=0 ; i<N ; i++ )
        {
                   cin>>list[i];
        }

        for( i=0 ; i<N ; i=i+2 )
        {
                  temp = list[i];
                  list[i] = list[i+1];
                  list[i+1]=temp;
        }

        cout<<"\nSHUFFLED ELEMENTS \n";

        for( i=0 ; i<N ; i++ )
        {
                  cout<<list[i]<<" ";
        }

        return 0;
}

OUTPUT


C++ Program to design a Magic Square [DEVCPP/GCC]

PROGRAM

//Program to design a Magic Square.

#include<iostream>
using namespace std;

int main()
{
int k[4][4],i,j,a,b,c,d,p,h;
int sum1,sum2,sum3,sum4;
char ch='n';

     cout<<"ENTER THE VALUES OF a,b,c,d"<<endl;
cout<<"a:";
cin>>a;
cout<<"\nb:";
cin>>b;
cout<<"\nc:";
cin>>c;
cout<<"\nd:";
cin>>d;

     cout<<"\nSUM OF ELEMENTS ENTERED(a,b,c,d):"<<(a+b+c+d)<<endl;
cout<<"DESIGNED A MAGICAL SQUARE USING THE ENTERED NUMBERS(a,b,c,d)..." <<endl<<endl;

    k[0][0]=a;    k[0][1]=b;    k[0][2]=c;    k[0][3]=d;
k[1][0]=d+1;  k[1][1]=c-1;  k[1][2]=b-3;  k[1][3]=a+3;
k[2][0]=b-2;  k[2][1]=a+2;  k[2][2]=d+2;  k[2][3]=c-2;
k[3][0]=c+1;  k[3][1]=d-1;  k[3][2]=a+1;  k[3][3]=b-1;    //Formula for magic square
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
cout<<k[i][j]<<"\t";
}
cout<<endl;
    }
   
 
     cout<<"\nTHIS IS THE MAGICAL SQUARE\n"<<endl;
     cout<<"IN THIS MAGICAL SQUARE THE SUM OF ELEMENTS OF EACH ROW\n"<<endl;
     cout<<"THE SUM OF ELEMENTS OF EACH COLUMN\n"<<endl;
     cout<<"THE SUM OF ELEMENTS OF EACH DIAGONAL\n"<<endl;
     cout<<"THE SUM OF ELEMENTS OF MIDDLE SQUARE\n"<<endl;
     cout<<"WILL BE SAME. AND IT IS EQUAL TO THE SUM OF THE ELEMENTS ENTERED."<<endl<<endl;
     cout<<"LET US SEE..."<<endl;
     cout<<"FROM WHERE U WANNA START:\n\n";
 
     do
     {
   
             cout<<"1.ROWS\n";
             cout<<"2.COLUMNS\n";
             cout<<"3.DIAGONALS\n";
             cout<<"4.MIDDLE SQUARE\n";
             cout<<"ENTER YOUR CHOICE:\n";
             cin>>h;
 
             switch(h)
             {
   
                   case 1:
   
do
{
                          cout<<"ROWS\n";
                          cout<<"WE HAVE 4 ROWS 1,2,3,4. ENTER THE NO OF ROW WHOSE SUM OF ELEMENTS U WANT TO SEE\n";
                          cin>>p;
   
                           switch(p)
    {
   
                                 case 1:

                                 i=0; sum1=0;
                               for(j=0;j<4;j++)
  {
  sum1=sum1+k[i][j];
}
cout<<"SUM OF ELEMENTS OF 1ST ROW:"<<sum1<<endl;
break;
case 2:

                                 i=1; sum2=0;
                               for(j=0;j<4;j++)
  {
  sum2=sum2+k[i][j];
}
cout<<"SUM OF ELEMENTS OF 2ND ROW:"<<sum2<<endl;
break;
case 3:

                                  i=2; sum3=0;
                                for(j=0;j<4;j++)
  {
  sum3=sum3+k[i][j];
}
cout<<"SUM OF ELEMENTS OF 3RD ROW:"<<sum3<<endl;
break;
                                 case 4:
   
                                   i=3; sum4=0;
                                 for(j=0;j<4;j++)
  {
  sum4=sum4+k[i][j];
}
cout<<"SUM OF ELEMENTS OF 4Th ROW:"<<sum4<<endl;
break;
default :
cout<<"default";
break;
   }

cout<<"DO U WISH TO CONTINUE WITH OTHER ROWS:"<<endl;
cin>>ch;
   
    }while(ch=='Y'||ch=='y');
break;
case 2:
do
{
cout<<"COLUMNS"<<endl;
cout<<"WE HAVE 4 COLUMNS 1,2,3,4. ENTER THE NO OF COLUMN WHOSE SUM OF ELEMENTS YOU WANT TO SEE\n";
cin>>p;

              switch(p)
{
case 1:

                    j=0; sum1=0;
for(i=0;i<4;i++)
{
sum1=k[i][j]+sum1;
}
cout<<"SUM OF ELEMENT OF 1ST COLUMN:"<<sum1<<endl;
break;
case 2:

                     j=1; sum2=0;
for(i=0;i<4;i++)
{
sum2=k[i][j]+sum2;
}
cout<<"SUM OF ELEMENT OF 2ND COLUMN:"<<sum2<<endl;
break;
case 3:

                     j=2; sum3=0;
for(i=0;i<4;i++)
{
sum3=k[i][j]+sum3;
}
cout<<"SUM OF ELEMENT OF 3RD COLUMN:"<<sum3<<endl;
break;
case 4:

                      j=3; sum4=0;
for(i=0;i<4;i++)
{
sum4=k[i][j]+sum4;
}
cout<<"SUM OF ELEMENT OF 4TH COLUMN:"<<sum4<<endl;
break;
default:
cout<<"default";
break;
}
cout<<"DO YOU WISH TO CONTINUE WITH COLUMNS... "<<endl;
cin>>ch;

  }while(ch=='Y'||ch=='y');
break;
case 3:
do
{

                cout<<"DIAGONALS"<<endl;
cout<<"WE HAVE TWO DIAGONALS 1,2.ENTER THE DIADONAL WHOSE SUM OF ELEMENTS YOU WANT TO SEE\n";
cin>>p;

                switch(p)
{

                       case 1:

                       sum1=0;
for(i=0,j=0;i<4;i++,j++)
{
sum1=sum1+k[i][j];
}
cout<<"SUM OF ELEMENTS OF 1ST DIAGONAL:"<<sum1<<endl;
break;
case 2:

                        sum2=0;
for(i=0,j=3;i<4;i++,j--)
{
sum2=sum2+k[i][j];
}
cout<<"SUM OF ELEMENTS OF 1ST DIAGONAL:"<<sum2<<endl;
break;
default :
cout<<"DEFAULT";
break;
}
cout<<"DO WISH TO CONTINUE WITH ANOTHER DIAGONAL..."<<endl;
cin>>ch;

  }while(ch=='Y'||ch=='y');
break;
case 4:

          sum1=0;
cout<<"MIDDLE INNER SQUARE";

          for(i=1;i<3;i++)
{
for(j=1;j<3;j++)
{
sum1=sum1+k[i][j];
}
}
cout<<"SUM OF ELEMENTS OF MIDDLE SQUARE IS:"<<sum1<<endl;
break;
      }
      cout<<"DO YOU HAVE SOME OTHER CHOICE:"<<endl;
      cin>>ch;
}
while(ch=='Y'||ch=='y');

}

OUTPUT




   

C++ Program to calculate factorial of a given number [DEVCPP/GCC]

STEPS

1. Initialize variable fact with 1.
                  fact = 1;

2. Multiply fact with the given number.
                 fact = fact * num;

3. Decrement number by 1.
                    num-- ;

4. Repeat steps 2-3 till num>0.

PROGRAM

//Program to calculate factorial of a given number

#include <iostream>

using namespace std;

int main()
{
        int i,n;
        long fact=1;

        cout<<"ENTER THE NUMBER: ";
        cin>>n;

        while(n!=0)
        {
                fact=fact*n;
                n--;
        }

        cout<<"FACTORIAL IS: "<<fact;
        return 0;
}

OUTPUT

C++ Program to calculate factorial of a given number with output

C++ Program to count the number of vowels in a string [DEVCPP/GCC]


STEPS 

1. Initialize the count with 0.

2. Input the string, in this case a[10].

3. Initialize i with 0, check the value at a[i] against the cases mentioned in switch loop.

4. If the value tends to be equal to any of the case labels , increment the count.

5. Increment i by 1 and repeat steps 3 and 4 till a[i] is not equal to '\0' (null).

6. Display count.


PROGRAM

//Program to count the number of vowels in a string

# include<iostream>

using namespace std;

int main()
{
char a[10];
int i,j,count=0;

cin.getline(a,20); //Input the string

for(i=0;a[i]!='\0';i++)    // Loop to count the number of vowels
{
switch(a[i])
{
 
                case  'a' :  case 'A':
                case  'i'  :  case 'I':
                case  'e' :  case 'E':
                case  'o' :  case 'O':
                case  'u' :  case 'U':
             
                 count++ ;
}
}

cout<<count;   // Displaying count
return 0; 
}

OUTPUT

C++ Program to count the number of vowels in a string with output

C++ Program to check whether a number is palindrome or not [DEVCPP/GCC]


PALINDROME NUMBER

A number is called palindrome if it remains same after reversing its digits.
For example,
Reverse(2002) = 2002 i.e. Palindrome Number
Reverse(2111) = 1112 i.e. Not a Palindrome Number

STEPS

1. Input a number(num).

2. Initialize another variable(original) with num. It would be used to compare the reversed number.

3. Obtain the last digit of num.

4.  Assign rev(Initially 0) with (rev*10) + lastnum.

5.  Decrement num as num/10.

6.  Repeat steps 3-5 until num>0.

7.  Compare Reversed Number(rev) with Original Number(original). If they are same, the given number is palindrome else not.

PROGRAM

//Program to check whether a number is palindrome or not

#include<iostream>

using namespace std;

int main()
{
int num, rev=0, original, lastnum;

cout<<"ENTER A NUMBER: ";
cin>>num;

original=num;

while(num>0)
{
                lastnum=num%10;
rev=(rev*10) + lastnum;
num=num/10;
}

if(original==rev)
{
cout<<"GIVEN NUMBER "<<original<<" IS A PALINDROME";
}
else
{
cout<<"GIVEN NUMBER "<<original<<" IS NOT A PALINDROME";
}

return 0;
}

OUTPUT

C++ Program to check whether a number is palindrome or not with output

C++ Program to check whether a number is perfect number or not [DEVCPP/GCC]


PERFECT NUMBER

A positive number is said to be perfect number if it is equal to the sum of its divisors.

For example,
The divisors of 6 are 1,2 and 3 and 1+2+3 = 6. Thus 6 is a perfect number.
The divisors of 28 are 1,2,4,7 and 14 and 1+2+4+7+14 = 28. Thus 28 is a perfect number.

STEPS

1. Initialize a variable sum with 0.

2. Input the number(num) from the user.

3. Initialize a counter (i) with 1.

4. Check the divisibility of num with i.

5. If it is divisible then add i to sum.

6. Increment counter(i).

7. Repeat steps 4-6 until i<=(num/2).

8. If sum is equal to num, then it is a perfect number else not.

PROGRAM

//  Program to check whether given number is perfect number or not.

#include <iostream>

using namespace std;

int main()
{
        int num ,sum=0 ,i;

        cout<<"ENTER A NUMBER: ";
        cin>>num;

        for(i=1;i<=(num/2);i++)
       {
                if(num % i==0)
               {
                        sum = sum + i;
               }
       }

       cout<<endl;

        if(sum==num)
       {
                cout<<num<<" IS A PERFECT NUMBER";
        }
        else
        {
               cout<<num<<" IS NOT A PERFECT NUMBER";
        }
        return 0;
}

OUTPUT

C++ Program to check whether a number is perfect number or not with output


C++ Program to check whether given number is prime or not [DEVCPP/GCC]


PRIME NUMBER

A natural number (>1) is said to be a prime number if it is completely divisible by 1 and itself only. A number which is not prime is called Composite number.

eg. 2,3,5,7,11,13 etc are Prime Numbers.

STEPS

In order to find given number is prime or not,

1. Initialize a counter(i) with 2 and a boolean variable (flag) as true.
                                          i = 2;
                                       flag = true;

2. Check the divisibility of the number with i.
                                       num % i==0

3. If the number is divisible by i, then it cannot be a prime number. So, set flag as false and break from loop.
                                        flag = false;
                                          break;

4. If the number is not divisible by i, then increment i and goto step 2 until i <=(num/2).
                                           i++;

5. If flag remains true throughout the above steps, it signifies that it is not divisible by any number between 2 and num/2 i.e. It is a prime number.
    If flag is false then the number is divisible by some number and is not a prime number.

PROGRAM

//Program to check whether given number is Prime or Not

#include <iostream>

using namespace std;

int main()
{
        int num , i;
        bool flag=true;

        cout<<"ENTER A NUMBER: ";
        cin>>num ;

        for( i=2 ; i<=(num/2) ; i++ )
       {
                if(num % i==0)
              {
              flag=false;          
                     break;                   //Exit from the Loop
              }
       }

        if(flag==true)
       {
                cout<<num<<" IS A PRIME NUMBER ";
       }
       else
       {
                cout<<num<<" IS A COMPOSITE NUMBER ";
       }

       return 0;
}

OUTPUT

C++ Program to check whether given number is Prime or Not with output

C++ Program to obtain the sum of digits of given number [DEVCPP/GCC]

STEPS

In order to calculate the sum of digits of a given number,

1. Obtain the last digit of given number using modulus (% ) operator.
                                    123 % 10 = 3

2. Add the obtained digit in a variable (sum) which is initialized by 0.
                                       0 + 3 = 3

3. Divide the number by 10 in order to remove the last digit from given number.
                                      123/10 = 12

4. Repeat the above steps 1-3 until number is not zero.                              

PROGRAM

//Program to obtain the sum of digits of given number

#include <iostream>

using namespace std;

int main()
{
        int num,sum=0,last;

        cout<<"ENTER NUMBER: ";
        cin>>num;

        while(num>0)
        {
                last = num % 10;          // Obtain Last Digit Of Number
                sum = sum + last;        // Add Obtained Digit to Number
                num = num /10;          // Remove Last Digit From Number
        }

        cout<<"\nSUM OF DIGITS IS "<<sum;
        return 0;
}

OUTPUT

C++ Program to obtain the sum of digits of given number

C++ Program to convert a String into Title Case [DEVCPP/GCC]


TITLE CASE

A sentence is said to be in Title Case if all of its constituent words begins with an Uppercase Letter.

                eg:   Input:    vijay dinanath chouhan
                       Output:  Vijay Dinanath Chouhan

PROGRAM

//Program to convert a string into Title case

# include <iostream>

using namespace std;

int main()
{
int i;
char a[30];
cin.getline(a,30);   //To Input a String
a[0]=a[0]-32;        //For First Letter

for(i=0;a[i]!='\0';i++)
{
if(a[i]==' ')
{
a[i+1] = a[i+1]-32;
}
}

cout<<a;
return 0;
}

OUTPUT

C++ Program to convert a String into Title Case with output

C++ Program to find the largest even number and largest odd number [DEVCPP/GCC]

PROGRAM

// Program to find the largest even number and largest odd number, from the list of numbers entered    by user, list terminates as the user enters zero

# include <iostream>

using namespace std;

int main()
{
int num, meven=0, modd=1;

cout<<"ENTER NUMBER (0 TO TERMINATE):\n";

do
{
cin>>num;
 
               if(num%2==0)
{
if(num>meven)
meven=num;
}
else
{
if(num>modd)
modd=num;
}

}while(num);

cout<<"LARGEST EVEN NUMBER IS:"<<meven<<endl;
cout<<"LARGEST ODD NUMBER IS:"<<modd<<endl;

return 0;
}

OUTPUT

C++ Program to find the largest even number and largest odd number

FIND US ON FACEBOOK!