c1

četvrtak, 13. prosinca 2012.

Program with integer, modul, average


/* 

 Make a program in which user enters a number n...If n is not integer, print a message about that and ask a user to enter a number again...User than enters n numbers, after that program  prints an  average of entered numbers....


#include <iostream>
using namespace std;
int main()
{
    bool ok=false;
    int m;
    while(ok==false){
                      cout<<"Type number n:  "<<endl;
    double n;
    cin>>n;
    n=n*10;
    
    m=(int)n;
    if(m%10==0){
                m=m/10;
    cout<<endl<<"It's OK, you can continue....  "<<endl<<endl;
    ok=true;
}
else{
     cout<<endl<<"You need to enter integer....  "<<endl<<endl;
     ok=false;
     }
}
cout<<endl<<"Now type "<<m<<" numbers  "<<endl;
int niz[m];
int sum=0;
for(int i=0;i<m;i++){
        cin>>niz[i];
        cout<<endl;
        sum=sum+niz[i];
         }
float as;
float sum1=sum;
float m1=m;
as=sum1/m1;

cout<<"Average is:   "<<as<<endl;    

    system("PAUSE");
    return EXIT_SUCCESS;
}


utorak, 11. prosinca 2012.

Matrix 50 x 50 and secondary diagonal

/*
Question:
Make a logic of program which will use a matrix [50][50]....User types the numbers, and numbers must be less or equal to 50, if that condition is not satisfied, you need to repeat the number m....Find and multiply the numbers on secondary diagonal of matrix....(secondary diagonal goes from upper right side to downer left)

*/

HERE YOU CAN FIND MORE C++ PROGRAMS ON REDGAGE







#include <iostream>
using namespace std;
int main()
{
    int x=3;
    int y=3;
    int n[x][y];
    int m;
    int sum=0;
    cout<<" Type the numbers of matrix: "<<x<<" x "<<y<<endl;
    for(int i=0;i<x;i++){
            for(int j=0;j<y;j++){
                  
            cout<<"Position "<<i+1<<" - "<<j+1<<" is: ";
            cin>>m;
            if(m<51){
            n[i][j]=m;      
            }
            else{
                 j--;
                 cout<<" Type the number which is smaller or equal to 50 "<<endl;
                 }
                    }
            }
  
    cout<<"Matrix is: "<<endl;
    for(int i=0;i<x;i++){
            cout<<endl;
          
            sum=sum+n[i][x-1-i];
            cout<<n[i][x-1-i]<<"   ";
                
                  
                  
            }
    cout<<endl<<endl<<"The sum of secondary diagonal is: "<< sum <<endl;
    cout<<endl;
  
  
    system("PAUSE");
    return EXIT_SUCCESS;
}



HERE YOU CAN FIND MORE C++ PROGRAMS ON REDGAGE


petak, 13. travnja 2012.

10. Simple C++ Program - Tutorial


#include <iostream>
using namespace std;
int main()
{
    int option=1;
    while(option!=0){
    cout<<"How many numbers you want to enter: ";
    int howMany;
    cin>>howMany;
    int array[howMany];
    cout<<"Write those numbers: "<<endl;
    for(int i=0;i<howMany;i++){
            cin>>array[i];
         
            }
            cout<<endl<<endl;;
    cout<<"Choose the option that you want:  "<<endl;
    cout<<"1)  Write from first to last..... "<<endl;
    cout<<"2)  write from last to first.....  "<<endl;  
    cout<<"0)  Exit... "<<endl;
    cin>>option;  
    cout<<endl;
    switch(option){
      case 1:{
       for(int i=0;i<howMany;i++){
               cout<<array[i]<<", ";
               }
             
       break;
                        }
      case 2:{
       for(int i=howMany-1;i>=0;i--){
               cout<<array[i]<<", ";
               }
       break;
                        }
         
}
    cout<<endl;
                }        
    cout<<endl<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}



9. Simple C++ Program - Tutorial


/*
In this program we use calculating operations like adding, multiplying, dividing and subtracting numbers...
*/


#include <iostream>
using namespace std;
int main()
{
    int option=1;
    while(option!=0){
    double number1;
    double number2;
    int option2;
    cout<<"Choose the option that you want:  "<<endl;
    cout<<"1)  Add two numbers...  "<<endl;
    cout<<"2)  Subtract two numbers...  "<<endl;
    cout<<"3)  Multiply two numbers...  "<<endl;
    cout<<"4)  Divide two numbers...  "<<endl;
    cout<<"0)  Exit... "<<endl;
    cin>>option;
    if(option!=0){
    cout<<endl<<"Enter two numbers:   "<<endl;
    cin>>number1;
    cin>>number2;
                 }
    cout<<endl;  
    switch(option){
      case 1:{
       cout<<number1<<" + "<<number2<<" = "<<number1+number2<<endl;
       break;
                        }
      case 2:{
       cout<<number1<<" - "<<number2<<" = "<<number1-number2<<endl;
       break;
                        }
      case 3:{
       cout<<number1<<" x "<<number2<<" = "<<number1*number2<<endl;
       break;
                        }
      case 4:{
       cout<<number1<<" : "<<number2<<" = "<<number1/number2<<endl;
       break;
                        }      
}
    cout<<endl;
                }          
    cout<<endl<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

code

after we run and use program

8. Simple C++ Program - Tutorial


#include <iostream>
using namespace std;
int main()
{
    int option=1;
    while(option!=0){
    int number1;
    int number2;
    cout<<"Please enter 2 numbers: "<<endl;
    cin>>number1;
    cin>>number2;
    cout<<"All odd numbers between these two numbers are: ";
    for(int i=number1+1;i<number2;i++){
            if(i%2!=0){
                 cout<<i<<", ";  
                       }}        
    cout<<endl<<endl;
    cout<<"To exit type 0 , if you want to try again type any other number:";
    cin>>option;
    cout<<endl<<endl;
                }          
    cout<<endl<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}


četvrtak, 12. travnja 2012.

7. Simple C++ Program - Tutorial


#include <iostream>
using namespace std;
int main()
{
    int option=1;
    while(option!=0){
    int number1;
    int number2;
    cout<<"Please enter 2 numbers: "<<endl;
    cin>>number1;
    cin>>number2;
    cout<<"All even numbers between these two numbers are: ";
    for(int i=number1+1;i<number2;i++){
            if(i%2==0){
                 cout<<i<<", ";    
                       }}          
    cout<<endl<<endl;
    cout<<"To exit type 0 , if you want to try again type any other number:";
    cin>>option;
    cout<<endl<<endl;
                }          
    cout<<endl<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}


srijeda, 11. travnja 2012.

6. Simple C++ Program - Tutorial


#include <iostream>
using namespace std;
int main()
{
    int option=1;
 
    while(option!=0){
    int number1;
    int number2;
    cout<<"Please enter 2 number which you want to add: "<<endl;
    cin>>number1;
    cin>>number2;
    cout<<"When we add these 2 numbers we get: ";
    cout<<number1+number2<<endl<<endl;
    cout<<"To exit type 0 , if you want to try again type any other number:";
    cin>>option;
    cout<<endl<<endl;  
                }            
            cout<<endl<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}