C++ Programs

print star triangle

Program 

C++ LANGUAGE

#include<iostream>     
#include<conio.h>
void main()
{
int n;
cout<<"Enter Number Of Lines You want";
cin>>n;
cout<<endl;
for (int i=0; i<n; i++)
{
for (int j=i; j>=0; j--)
{
cout<<"*";
}
cout<<"\n";
}
 getch();
}

C LANGUAGE

#include<conio.h>
void main()
{
clrscr();
for(int i=0;i<5;i++)
{
for(int j=i;j>=0;j--)
{
printf("*");
}
printf("\n");
}
getch();
}


OUTPUT :- 


--------------------------------------------------------------------------------------

Program 

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
for(int i=0;i<5;i++)
{
for(int j=i;j>=0;j--)
{
printf("%d",i+1);
}
printf("\n");
}
getch();

}

OUTPUT :-


--------------------------------------------------------------------------------------

PROGRAM 

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int k=5;
for(int i=0;i<5;i++)
{
for(int j=i;j>=0;j--)
{
printf("%d",k);

}
k--;
printf("\n");
}
getch();
}

--------------------------------------------------------------------------------------

Program

#include<stdio.h>
#include<conio.h>
void main()
{
int k=5;
for(int i=0;i<5;i++)
{
for(int j=i;j>=0;j--)
{
printf("%d",k); 
}
k--;
printf("\n");
}
getch();
}

--------------------------------------------------------------------------------------



Program:

#include <iostream>
#include <conio.h>
int main()
{
int n;
for(n=1;n<=10;n++){
cout<<"*";
}
cout<<endl;
for(int i=1;i<=10;i++){
if(i==5||i==6){
cout<<" ";
}
else
cout<<"*";
}
cout<<endl;
for(int j=1;j<=10;j++){
if(j==4||j==5){
cout<<" ";
}
else if(j==6||j==7)
cout<<" ";
else
cout<<"*";
}
cout<<endl;
for(int k=1;k<=10;k++){
if(k==3||k==4){
cout<<" ";
}
else if(k==5||k==6)
cout<<" ";
else if(k==7||k==8)
cout<<" ";
else
cout<<"*";
}
cout<<endl;
for(int m=1;m<=10;m++){
if(m==2 || m==3){
cout<<" ";
}
else if(m==4 || m==5){
cout<<" ";
}
else if(m==6 || m==7){
cout<<" ";
}
else if(m==8 || m==9){
cout<<" ";
}
else
cout<<"*";
}
cout<<endl;
for(int q=1;q<=10;q++){
if(q==3||q==4){
cout<<" ";
}
else if(q==5||q==6)
cout<<" ";
else if(q==7||q==8)
cout<<" ";
else
cout<<"*";
}
cout<<endl;
for(int w=1;w<=10;w++){
if(w==4||w==5){
cout<<" ";
}
else if(w==6||w==7)
cout<<" ";
else
cout<<"*";
}
cout<<endl;
for(int r=1;r<=10;r++){
if(r==5||r==6){
cout<<" ";
}
else
cout<<"*";
}
cout<<endl;
int t;
for(t=1;t<=10;t++){
cout<<"*";
}
cout<<endl;
getch();
};

--------------------------------------------------------------------------------------

Basic To Advanced Task for the Beginners
Exercises:

1. C++ Program To Print ASCII value of Digits,Uppercase and Lowercase Alphabates.
2. C++ Program to calculate sum and average of three numbers.
3. C++ Program to raise any number x to a positive power n.
4. C++ Program to convert given inches into equivalent yard,feet and inches.
5. C++ Program to Find ASCII value of a character.
6. C++ Program to find Compound Interest.
7. C++ Program to calculate square root of any number.
8. C++ Program to Convert given no. of dayes into years,weeks and days.
9. C++ program to calculate sum of two numbers.
10. C++ program to swap two numbers using macros.
11. C++ program to find cube of a number using macros.

Array

1. C++ program to multiply two matrices.
2. C++ program to read a matrix of size mxn from the keyboard and display the same on the screen.
3. C++ program to find sum of elements above and below the main digonal of a matrix.
4. C++ program to insert an element in an array.
5. C++ program to print upperhalf and lowerhalf triangle of a square Matrix.
6. C++ program to find largest and second largest no from a 2D array.
7. C++ program to find sum of 1st & 2nd digonal of a square Matrix.
8. C++ Program to sort an Array by using Bubble sort.
9. C++ Program to delete an element from an Array.
10. C++ program to do linear search in Array.
11. C++ Program to explain Binary search in Array.
12. C++ Program to find highest and lowest element of a Matrix.
13. C++ Program to Add two matrices.

if/else
1. C++ program to swap two numbers without using temp variable.
2. C++ Program to convert a lowercase alphabate to uppercase or vice-versa.
3. C++ Program to print three numbers in descending order.
4. C++ Program to calculate roots of quadratic equation ax^2+bx+c=0.
5. C++ Program to find quotient and remainder of two numbers.
6. C++ Program to Check whether a year is Leap year or not.
7. C++ Program to Find LCM and HCF of two numbers.
8. C++ Programs to check whether a given character is uppercase or lowercase alphabate or a digit or a special character.
9. C++ Program to find Greatest number among three numbers.
10. C++ Program to check whether a number is odd or even.

Loops
1. C++ program to find whether a number is an Armstrong number or not.
2. C++ program to reverse a number.
3. C++ program to find largest number of a list of numbers entered through keyboard.
4. C++ Program to print table of any number.
5. C++ Program to print your name 10 times.
6. C++ Program to calculate and print the sum of even and odd integers of the first n natural numbers.
7. C++ Program to check whether a number is prime number or not.
8. C++ program to print truth table of XY+Z.
9. C++ Program check whether a number is palindrome or not.
10. C++ Program to find First three Pythagorian Triplet.
11. C++ Program to convert binary number to decimal number.
12. C++ Program to print first 10 Prime numbers.
13. C++ Program to convert a decimal number to binary number.
14. C++ Program to print a triangle or square of *'s according to user choice.
15. C++ Program to print fibonacci series.
16. C++ Program to find sum of square of n natural numbers.
17. C++ Program to find divisers of a number.
18. C++ Program to find sum of digits of a number.
19. C++ Program to find sum of first n natural number.
20. C++ Program to calculate Factorial of any number.
21. C++ Program to print series of odd numbers in decending order from a given number.
22. C++ program to find average of list of numbers entered through keyboard.

String
1. C++ Program to convert first letter of each word of a string to uppercase and other to lowercase.
2. C++ Program to Count no. of words in a string.
3. C++ Program to reverse all the strings stored in an array.
4. C++ Program to concatenate two strings.
5. C++ Program to check whether a String is Palindrome or not.
6. C++ Program to Convert first alphabet of every word in a string from lowercase to uppercase.
7. C++ Program to reverse a string.
8. C++ Program to find length of a string.

File Handling
1. C++ Program to Count no. of alphabates,digits and spaces present in a file STORY.TXT.
2. C++ Program to read from a text file and than write in another text file.

Functions
1. C++ Program to find cube of a number using function.
2. C++ Program to do Addition,subtraction and multiplication of two numbers using function.
3. C++ Program to print following series using function: x + x^3/3! + x^5/5! +.......+ x^n/n!.

Games
1. Tic-Tac-Toe Game.
2. High/Low Game.

Projects
1. C++ Hotel Management Project.
2. C++ Program to create an Analog Clock.
3. C++ program to create Facebook Login screen.
4. C++ Program to Print an Alphabats from A to L of *'s in capital letter.

Stacks/Queues
1. C++ program to perform a PUSH operation on a dynamically allocated stack.

Sum Of Series
1. C++ program to find sum of series 1 + 2 + 3 +......+ n.
2. C++ program to find the sum of the series x+x^2/2+x^3/3+.....+x^n/n.
3. C++ program to find sum of series 1/2+4/5+7/8+......
4. C++ program to find sum of series 1+x+x^2+......+x^n.
5. C++ program to find sum of series 1^2+3^2+5^2+......+n^2.
6. C++ Program to print given series:1 2 4 8 16 32 64 128.
7. C++ Program to Print following series: 1 -4 7 -10..........-40.
8. C++ program to find sum of series 1+1/2^2+1/3^3+.....+1/n^n.

Switch Case
1. C++ program to calculate area of a circle,a rectangle or a triangle depending upon user's choice.
2. C++ Program to perform all arithmetic calculation using switch case.
3. C++ Program for temperature conversion which converts fahrenheit to celcius or celcius to fahrenheit depending upon user's choice.
4. C++ Program to do arithmetic operations according to user choice using switch case.
5. C++ program to enter a number and print it into words.

Pointers

1. C++ program to swap two numbers using pointers.
Posted by Muhammad Usman Sharif at 4:33 AM 



--------------------------------------------------------------------------------------

Basic C++ Exercises

1.                   Check odd or even
2.                  convert seconds to minutes
3.                  convert seconds to hours
4.                  convert seconds to minutes and hours
5.                  convert minutes to seconds
6.                  convert minutes to hours
7.                  convert minutes to minutes and hours
8.                  convert hours to minutes
9.                  convert hours to seconds
10.               convert Fahrenheit to Celsius
11.                convert Fahrenheit to kelvin
12.               convert Celsius to Fahrenheit
13.               convert Celsius to kelvin
14.               convert kelvin to Celsius
15.               convert kelvin to Fahrenheit
16.               table of any number
17.               calculate HCF
18.               calculate LCM
19.               check perfect number
20.              reverse number
21.               display next prime number
22.              Program to add 2 numbers
23.              Find maximum of 2 numbers
24.              Find maximum of 3 numbers
25.              Find maximum of n numbers
26.              Swap 2 numbers
27.              Fibonacci series
28.              average of n numbers
29.              palindrome
30.              Calculate sum of digits
31.               Calculate reverse of a number
32.              Check a number prime or not
33.              Check a year leap year or not
34.              Check a character is vowel or not
35.              Check a character is digit or not
36.              Count number of vowels in a string
37.              Program to perform arithmetic operation
38.              Display list of 1 to 10 factorials
39.              Display list of 1 to 10 squares
40.             Program to add n integers
41.               Sum of 1 to 100 even number
42.              Sum of 1 to 100 odd numbers
43.              Sum of 1 to 100 numbers divisible by 4
44.             Sum of 1 to 25 numbers
45.              Average of 1 to 35 numbers


Array Related
Reverse a array values
Traversing
Insert an element in array-
a)       At the begin
b)       At middle or any position except start or last
c)       At the end
d)       Insert an element in an array based on its position
e)       Insert an element in an array based on its value
4.  Delete an element in array
a)      At the begin
b)      At middle or any position except start or last
c)      At the end
d)      Delete an element from an array based on its value
e)      Delete an element from an array based on its position
5.  Searching an element in array
a)      Linear Search
b)      Binary Search
6.  Sorting elements
a)      Bubble sort
b)      Selection sort
c)       Insertion sort
d)      Quick sort
e)      Merge sort
f)       Radix sort
g)      Tree sort
h)      Heap sort
7.  Program to Swap First half with Last half of an Array
8.  Program to Count numbers greater than 5
9.  Program to Swap neighbor elements in an array
10. Program to print array values through Pointers
11. Program to find sum of an array using pointers
12. Program to find maximum in an array

String
1.                   Program to Concatenate Two Strings using strcat( )
2.                  Program to Concatenate Two Strings without using strcat( )
3.                  Program to Compare Two Strings using strcmp( )
4.                  Program to Compare Two Strings without using strcmp( )
5.                  Program to Copy String using strcpy( )
6.                  Program to Copy String without using strcpy( )
7.                  Program to Find Length of a String using strlen( )
8.                  Program to Reverse a String using strrev( )
9.                  Program to Reverse a String without using strrev( )
10.               Program to Input-Output Strings using Character Functions
11.                Program to Input-Output Strings using gets( ) and puts( )
12.               Program to Input-Output Strings using printf( ) and scanf( )
13.               Program to Find Length of a String Without using strlen( )
14.               Program to Find Whether a String is Palindrome or Not
15.               java charAt() mehod or extract a particular position character from String
16.               Program to Find Whether a String is Palindrome or Not without using String Functions



1.                   File Handling
2.                  Program to Copy Contents of One File to Another
3.                  Program to Print a File and Count Number of Characters
4.                  Program to Print a File and Count Number of words
5.                  Program to Print a File and Count Number of lines


Matrix
  1. Sum of Individual Rows and Columns of a Matrix
  2. Program to find Sum of Diagonals in a Matrix
  3. Program to perform matrix Multiplication
  4. Program to perform transpose of a matrix
  5. Program to Display numbers divisible by 10 in a Matrix
  6. Program to find Sum of numbers divisible 2 or 3 in a Matrix
  7. Program to display Both Diagonal elements of a Matrix
       For Example:

Matrix:             1   2   3
                        4   5   6
                        7   8   9

          Diagonal 1:        1   5   9
          Diagonal 2:        3   5   7

  8. Program to Display Lower triangle of a Matrix
       For Example:

Input:       1   2   3            Output:      1
               4   5   6                             4   5
               7   8   9                             7   8   9
  9. Program to Display Upper triangle of a Matrix
       For Example:

Input:      1   2   3            Output:     1   2    3
              4   5   6                                  5   6
              7   8   9                                       9



Structure
1.                   Example on Structure
2.                  Program to illustrate returning structure from function
3.                  Program to pass entire Structure from an Array
4.                  Program for passing individual structure members through Function
5.                  Program to illustrate Array of Structure
6.                  Program to illustrate Nested Structure
7.                  Program to Implement Structure with Array
8.                  Program to Implement Structure with Function
9.                  Program to Implement Structure with Pointers



Class
  1. Example on Class
  2. Friend Function Example
  3. Example 1 on Class
  4. Example 2 on Class

Constructor and Destructor
  1. Constructor and Destructor Example

Inheritance
  1. Inheritance with same Function name in Base and Derived Class
  2. Inheritance: Virtual Base Class Example

File Handling
  1. File Handling: Copy entire contents of a file into another
  2. File Handling: Searching a telephone number from a file

Stack and Queue
  1. Stack: Implemented using Array
  2. Queue: Implemented using Array
  3. Circular Queue: Implemented using Array

Linked List
  1. Stack: Implemented using Linked List
  2. Queue: Implemented using Linked List
  3. Circular Queue: Implemented using Linked List
Posted by Muhammad Usman Sharif at 4:29 AM 


--------------------------------------------------------------------------------

Sunday, September 8, 2013

Write a program that prompts the user to enter three integer values, and then outputs the values in numerical sequence separated by commas. So, if the user enters the values 10 4 6, the output should be 4, 6, 10. If two values are the same, they should just be ordered together. So, the input 4, 5, 4 should give 4, 4, 5.

#include<iostream>
using namespace std;
int main()
{
 int val1 = 0;
 int val2 = 0;
 int val3 = 0;
 cout << "Please enter three integer values separated by a space: ";
 cin >> val1 >> val2 >> val3;
 cout << "values read : " << val1 << ", " << val2 << ", " << val3 <<'\n';

 // idea for solution:
 // 
 just test which value is the smallest and put it into "smallest"
 // 
 then test which ofthe remaining two values is the smaller and put it into "middle"
 // 
 then but the remaining variable int "largest"

 int smallest = 0;
 int middle = 0;
 int largest = 0;
 if (val1<=val2 && val1<=val3) {
 // && means and
 
 smallest = val1;
 
 if (val2<=val3) {
  
 middle = val2;
  
 largest = val3;
 
 }
 
 else {
  
 middle = val3;
  
 largest = val2;
 
 }
 }
 else if (val2<=val1 && val2<=val3) {
 
 smallest = val2;
 
 if (val1<=val3)  {
  
 middle = val1;
  
 largest = val3;
 
 }
 
 else {
  
 middle = val3;
  
 largest = val1;
 
 }
 }
 else {
 // since neither val1 nor val2 was smaller than val3, val3 must be the smallest
 
 smallest = val3;
 
 if (val1<=val2) {
  
 middle = val1;
  
 largest = val2;
 
 }
 
 else {
  
 middle = val2;
  
 largest = val1;
 
 }
 }

 cout << "values sorted : " << smallest << ", " << middle << ", " << largest <<'\n';
 system("pause");
}

Posted by Muhammad Usman Sharif at 6:37 AM 



------------------------------------------------------------------------------------

Complex numbers class: Design and implement a complex numbers class, Complex. A complex number has two parts: a real and an imaginary, for example, 3 + 4j and 2 – 5j. Provide addition, subtraction, multiplication, division.

#include <iostream>
using namespace std;

class C_nos
{

 int real;
 int imaginary;
public:
 C_nos():real(0), imaginary(0){}

 C_nos(int a, int b)
 {
 real      = a;
 imaginary = b;
 }

 void disp()
 {
 cout<<"\nReal Part is:\t\t"   <<real<<endl;
 cout<<"\nImaginary part is:\t"<<imaginary<<"i"<<endl;
 }

 C_nos operator +(C_nos a)
 {
 C_nos u;
 u.real = real+a.real;
 u.imaginary=imaginary+a.imaginary;
 return u;
 }

 C_nos operator -(C_nos a)
 {
 C_nos u;
 u.real= real-a.real;
 u.imaginary=imaginary-a.imaginary;
 return u;
 }

 C_nos operator *(C_nos a)
 {
 C_nos u;
 u.real= real*a.real;
 u.imaginary=imaginary*a.imaginary;
 return u;
 }

 C_nos operator /(C_nos a)
 {
 C_nos u;
 u.real= real/a.real;
 u.imaginary=imaginary/a.imaginary;
 return u;
 }

};

int main()
{
 cout<<endl;
 cout<<"Default contructor called:\n";
 C_nos c1;
 c1.disp();
 cout<<endl;
 cout<<"Overloaded contructor called:\n";
 C_nos c2(2,2);
 c2.disp();
 C_nos c3(10,6);
 c2.disp();
 cout<<endl;
 cout<<"\nSum operator is called\n";
 cout<<"sum is:\n";
 C_nos sum = (c2 + c3);
 sum.disp();
 cout<<endl;
 cout<<"\nDifference operator is called\n";
 cout<<"difference is:\n";
 C_nos dif;
 dif= c3-c2;
 dif.disp();
 cout<<endl;
 cout<<"\nProduct operator is called\n";
 cout<<"product is:\n";
 C_nos mul= c2*c3;
 mul.disp();
 C_nos div= c3/c2;
 cout<<endl;
 cout<<"\nDivide operator is called\n";
 cout<<"quotient is:\n";
 div.disp();
 system("pause");
}
Posted by Muhammad Usman Sharif at 12:36 AM


---------------------------------------------------------

Monday, July 22, 2013

Pointers to Structures:
You can define pointers to structures in very similar way as you define pointer to any other variable as follows:

struct Books *struct_pointer;

Now you can store the address of a structure variable in the above defined pointer variable. To find the address of a structure variable, place the & operator before the structure's name as follows:

struct_pointer = &Book1;

To access the members of a structure using a pointer to that structure, you must use the -> operator as follows:
struct_pointer->title;
Let us re-write above example using structure pointer, hope this will be easy for you to understand the concept:

#include <iostream>
#include <cstring>

using namespace std;
void printBook( struct Books *book );

struct Books
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
};

int main( )
{
   struct Books Book1;        // Declare Book1 of type Book
   struct Books Book2;        // Declare Book2 of type Book

   // Book 1 specification
   strcpy( Book1.title, "Learn C++ Programming");
   strcpy( Book1.author, "Chand Miyan");
   strcpy( Book1.subject, "C++ Programming");
   Book1.book_id = 6495407;

   // Book 2 specification
   strcpy( Book2.title, "Telecom Billing");
   strcpy( Book2.author, "Yakit Singha");
   strcpy( Book2.subject, "Telecom");
   Book2.book_id = 6495700;

   // Print Book1 info, passing address of structure
   printBook( &Book1 );

   // Print Book1 info, passing address of structure
   printBook( &Book2 );

   return 0;
}
// This function accept pointer to structure as parameter.
void printBook( struct Books *book )
{
   cout << "Book title : " << book->title <<endl;
   cout << "Book author : " << book->author <<endl;
   cout << "Book subject : " << book->subject <<endl;
   cout << "Book id : " << book->book_id <<endl;
}
When the above code is compiled and executed, it produces following result:
Book title : Learn C++ Programming
Book author : Chand Miyan
Book subject : C++ Programming
Book id : 6495407
Book title : Telecom Billing
Book author : Yakit Singha
Book subject : Telecom
Book id : 6495700


------------------------------------------------------------------------

Structures as Function Arguments:
You can pass a structure as a function argument in very similar way as you pass any other variable or pointer. You would access structure variables in the similar way as you have accessed in the above example:
#include <iostream>
#include <cstring>

using namespace std;
void printBook( struct Books book );

struct Books
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
};

int main( )
{
   struct Books Book1;        // Declare Book1 of type Book
   struct Books Book2;        // Declare Book2 of type Book

   // book 1 specification
   strcpy( Book1.title, "Learn C++ Programming");
   strcpy( Book1.author, "Chand Miyan");
   strcpy( Book1.subject, "C++ Programming");
   Book1.book_id = 6495407;

   // book 2 specification
   strcpy( Book2.title, "Telecom Billing");
   strcpy( Book2.author, "Yakit Singha");
   strcpy( Book2.subject, "Telecom");
   Book2.book_id = 6495700;

   // Print Book1 info
   printBook( Book1 );

   // Print Book2 info
   printBook( Book2 );

   return 0;
}
void printBook( struct Books book )
{
   cout << "Book title : " << book.title <<endl;
   cout << "Book author : " << book.author <<endl;
   cout << "Book subject : " << book.subject <<endl;
   cout << "Book id : " << book.book_id <<endl;
}
When the above code is compiled and executed, it produces following result:
Book title : Learn C++ Programming
Book author : Chand Miyan
Book subject : C++ Programming
Book id : 6495407
Book title : Telecom Billing
Book author : Yakit Singha
Book subject : Telecom
Book id : 6495700


--------------------------------------------------------------------

Accessing Structure Members:
To access any member of a structure, we use the member access operator (.). The member access operator is coded as a period between the structure variable name and the structure member that we wish to access. You would use struct keyword to define variables of structure type. Following is the example to explain usage of structure:
#include <iostream>
#include <cstring>

using namespace std;

struct Books
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
};

int main( )
{
   struct Books Book1;        // Declare Book1 of type Book
   struct Books Book2;        // Declare Book2 of type Book

   // book 1 specification
   strcpy( Book1.title, "Learn C++ Programming");
   strcpy( Book1.author, "Chand Miyan");
   strcpy( Book1.subject, "C++ Programming");
   Book1.book_id = 6495407;

   // book 2 specification
   strcpy( Book2.title, "Telecom Billing");
   strcpy( Book2.author, "Yakit Singha");
   strcpy( Book2.subject, "Telecom");
   Book2.book_id = 6495700;

   // Print Book1 info
   cout << "Book 1 title : " << Book1.title <<endl;
   cout << "Book 1 author : " << Book1.author <<endl;
   cout << "Book 1 subject : " << Book1.subject <<endl;
   cout << "Book 1 id : " << Book1.book_id <<endl;

   // Print Book2 info
   cout << "Book 2 title : " << Book2.title <<endl;
   cout << "Book 2 author : " << Book2.author <<endl;
   cout << "Book 2 subject : " << Book2.subject <<endl;
   cout << "Book 2 id : " << Book2.book_id <<endl;

   return 0;
}
When the above code is compiled and executed, it produces following result:
Book 1 title : Learn C++ Programming
Book 1 author : Chand Miyan
Book 1 subject : C++ Programming
Book 1 id : 6495407
Book 2 title : Telecom Billing
Book 2 author : Yakit Singha
Book 2 subject : Telecom
Book 2 id : 6495700
Posted by Muhammad Usman Sharif at 5:02 PM

----------------------------------------------------------------

Tuesday, July 2, 2013

Write a c++ program using recursion which displays the factorial of entered number.

#include<iostream>


using namespace std;


int fact (int);
int fact (int number)
{
 if(number==1)
 
 return 1;
 else
 
 return number*fact(number-1);
}
int main()
{
 int number;
 cout<<"Enter number: ";
 cin>>number;
 cout<<fact(number);
 system("pause");
 return 0;
}

Posted by Muhammad Usman Sharif at 10:55 AM 



--------------------------------------------------------------------------------------------------

Write a simple c++ program which displays the factorial of entered number

#include<iostream>

using namespace std;
int main()
{
 int number, fac=1;
 cout<<"Enter number: ";
 cin>>number;
 for(int i=number; i>=1; i--)
 {
 fac=(fac*i);
 }
 cout<<"factorial of "<<number<<" is= "<<fac<<endl;
 system("pause");
 return 0;
}
Posted by Muhammad Usman Sharif at 10:13 AM


---------------------------------------------------------------------------------

Using bool in function, write a c++ program to check whether number entered is prime or not?

#include<iostream>
using namespace std;
bool isprime(int );
bool isprime(int number)
{
 int check=0;
 if (number > 1)
 {
 
 for(int i=2; i<number; i++)
 
 {
  
 if(number%i==0)
  
 {
  
 check++;
  
 break;
  
 }
 
 }
 
 if(check==0)
 
 {return true;}
 
 else
 
 {return false;}
 }

}
int main()
{
 int number;
 cout<<"Enter number= ";
 cin>>number;
 cout<<isprime(number)<<endl;
 system("pause");
 return 0;
}

Posted by Muhammad Usman Sharif at 9:36 AM 



------------------------------------------------------------------------------------

Friday, June 28, 2013

Write a simple program which takes input values & store it in string a array and store it reversely in another string array.

#include<iostream>
#include<string>

using namespace std;
int main()
{
 char str1[10];
 char str2[10];

 for(int i=0;i<10; i++)
 {
  cout<<"enter str1= ";
 
 cin>>str1[i];
 
 str2[9-i]=str1[i];
 }

 for(int i=0; i<10; i++)
 {
 
 cout<<"str1\t"<<i<<"\t=\t"<<str1[i]<<"\t";
 
 cout<<"str2\t"<<i<<"\t=\t"<<str2[i]<<endl;
 }

 system("pause");
 return 0;
}
Posted by Muhammad Usman Sharif  at 10:50 PM 

---------------------------------------------------------------------------------------------

Using function write a program which inputs values in a string array and store it in two other string arrays such that it is copied as it is in third string array & reversely in second string array.

#include<iostream>
#include<string>

using namespace std;
void rev(string, string);
void rev(string s1[10], string s2[10], string s3[10])
{
 int i;
 for(i=0;i<10; i++)
 {
 
 s2[9-i]=s1[i];
 
 s3[i]= s1[i];
 }
}
int main()
{
 string s1[10];
 string s2[10];
 string s3[10];
 for(int i=0; i<10; i++)
 {
 cout<<"Enter string values\t";
 cin>>s1[i];
 }
 rev(s1, s2, s3);
 system("pause");
 return 0;
}
Posted by Muhammad Usman Sharif at 10:47 PM

---------------------------------------------------------------------------------------

Saturday, April 27, 2013

Print a triangle using * in visual c++

#include <iostream>
#include <conio.h>
int main()
{
using namespace std;
int n, i, j;

cout<<"Enter the length (no. of rows) of a triangle: ";
cin >>n;
cout<<endl;

for( i=1; i<=n; i++ )
{
for( j=1; j<=i;  j++ )
cout << "*";
cout<<endl;
}
getch();
return 0;
}
Posted by Muhammad Usman Sharif at 10:43 AM

-------------------------------------------------------------------------------------

Print a rectangle using * in Visual c++

#include <iostream>
#include <conio.h>
int main()
{
using namespace std;
int n, i, j;

cout<<"Enter the length (no. of rows) of a rectangle: ";
cin >>n;
cout<<endl;

for( i=1; i<=n; i++ )
{
for( j=1; j<=n;  j++ )
cout << "*";
cout<<endl;
}
getch();
return 0;
}


Posted by Muhammad Usman Sharif at 10:42 AM

----------------------------------------------------------------------------------------

Saturday, April 27, 2013

Multiplication of two complex numbers in visual c++

# include <iostream>
# include <conio.h>
int main()
{
 using namespace std;
 int r1, r2, i1, i2;
 int  rA, rB, rC,rD;
 cout<<"\n Enter real part of first number: ";
 cin>>r1;
 cout<<"\n Enter imaginary part of first number: ";
 cin>>i1;
 cout<<"\n Enter real part of second number: ";
 cin>>r2;
 cout<<"\n Enter imaginary part of second number: ";
 cin>>i2;

 rA= r1*r2;
 rB= r1*i2;
 rC= i1*r2;
 rD= (i1*i2);
 cout<<"\n Result is= "<<rA-rD<<" + "<<rB+rC<<"i";
 getch();
 return 0;
}

Posted by Muhammad Usman Sharif at 2:53 AM 

--------------------------------------------------------------------------------------

Random Range Function

#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <ctime>

// Declaration
int randInRange(int, int);

int main()
{
srand(time(0));

using namespace std;
cout << "Selected student from batch1 is: "<<randInRange(11300, 11340)<< endl;
cout << "Selected student from batch2 is: "<<randInRange(11350, 12350)<< endl;
cout << "Selected student from batch3 is: "<<randInRange(11400, 11480)<< endl;

getch();
return 0;
}
int randInRange(int start, int end)
{

int range = end - start + 1;
return rand()%range + start;
}

Posted by Muhammad Usman Sharif  at 2:01 AM 

------------------------------------------------------------------------------------

Find HCF in visual c++ using function

  #include<iostream>
  #include<conio.h>

   int hcf(int x,int y);
   using namespace std;
   int main()

  {

    int a,b,d;


    cout<<"Enter a number=";
    cin>>a;
    cout<<"Enter 2nd number=";
    cin>>b;
    if(a>b)
    {
      d=hcf(a,b);
    }
    else
    {
      d= hcf(b,a);
    }
    cout<<"HCF is= "<<d;
 getch();
 return 0;
  }
    int hcf(int x,int y)
    {
     int r=1;
     while(r!=0)
       {
             r=x%y;
             x=y;
             y=r;
       }
     return(x);
    }
Posted by Muhammad Usman Sharif at 1:54 AM

---------------------------------------------------------------------------------

Find LCM using function in Visual c++


#include<iostream>
#include<conio.h>
using namespace std;

int lcm(int,int);
int main()
{
    int a,b,l;

    cout<<"Enter any integer: ";
 cin>>a;
 cout<<"Enter another integer: ";
 cin>>b;

    if(a>b)
         l = lcm(a,b);
    else
         l = lcm(b,a);

    cout<<"LCM of two integers is: "<<l;
 getch();
    return 0;
}

int lcm(int a,int b)
{

    int c = a;

    while(1)
 {
         if(c % b == 0 && c % a == 0)
             break;
         c++;
    }

   return c;
}

Posted by Muhammad Usman Sharif  at 1:34 AM 


---------------------------------------------------------------------------------------


Enter age and see what company gifted you? (if & else statement).

# include <iostream>
# include <conio.h>
int main()
{
 using namespace std;
 int age;
 cout<<"Enter Your Age: ";
 cin>>age;
 
 if(age<0 || age> 150)
 
 {
  
 cout<<"YOU HAVE ENTERED WRONG VALUE";
 
 }
 
 else
 
 if(age > 0 && age<12)
 
 {
  
 cout<<"YOU HAVE WON A TOY";
 
 }
 
 else
 
 if(age>=12 && age<20)
 
 
 
 {
  
 cout<<"YOU HAVE WON A MOBILE PHONE";
 
 }
 
 else
 
 if(age>=20 && age<30)
 
 {
  
 cout<<"YOU HAVE WON A WRIST WATCH";
 
 }
 
 else
  
 cout<<"YOU HAVE WON A BOOK";
 
 getch();
 
 return 0;
}


Posted by Muhammad Usman Sharif at 1:10 AM 



-----------------------------------------------------------------------------

L.C.M & H.C.F in Visual C++

#include<iostream>
#include<conio.h>

int main()
 {
  using namespace std;
  int a, b, c;
  cout<<"Enter 1st no.= ";
  cin>>a;
  cout<<"Enter 2nd no.= ";
  cin>>b;
  c=a*b;
  while(a!=b)
  {
   if(a>b)
   {
   a=a-b;
   }
   else
   {
   b=b-a;
   }
  }
  cout<<"H.C.F=  "<<a<<endl;
  cout<<"L.C.M=  "<<c/a<<endl;
  getch();
  return 0;
}
Posted by Muhammad Usman Sharif  at 1:07 AM 


----------------------------------------------------------------------------------------


Tuesday, April 23, 2013

Check number and its circular number is prime or not?

#include<iostream>
#include<conio.h>

int main()
 {
  using namespace std;
  int num, count, check = 0, check2 = 0, N, a, b;
  cout<<"\nEnter a 2 digit number= ";
  cin>>num;

  a= num/10;
  b= num%10;
  N= (b*10)+a;
  for(count=2; count<num; count++)
  {
   if(num%count == 0)
   {
    check = 1;
    break;
   }
  }
   if(check == 0)
   {
    cout<<"\nThe number you entered is a prime number. "<<endl;
   }
   else
   {
    cout<<"\nThe number you entered is not a prime number."<<endl;
   }
  for(int i=2; i<N; i++)
  {
    if(N%i==0)
  {
    check2 = 1;
    break;
     }
  }
    if(check2 == 0)
    cout<<"\ncircular number is a prime number. "<<endl;
 if(check2 != 0)
    cout<<"\ncircular number is not a prime number."<<endl;

 if( check == 0 && check2 == 0 )
  cout<<"\nboth "<<num<<" and "<<N<<" are prime.";
    if( check != 0 && check2 != 0 )
  cout<<"\nboth "<<num<<" and "<<N<<" are not prime.";
 if( check2 != 0 && check == 0 )
  cout<<"RESULT= "<<num<<" is prime and "<<N<<" is not prime.";
    if( check!= 0 && check2 == 0 )
  cout<<"RESULT= "<<N<<" is prime and "<<num<<" is not prime.";
    getch();
    return 0;
 }
Posted by Muhammad Usman Sharif  at 11:23 AM

-------------------------------------------------------------------

Monday, April 22, 2013

Fabonacci Series in visual c++


# include <iostream>
# include <conio.h>

int main ()
{
 using namespace std;
 int a,b,c,sum;
 cout<<"enter the number till which fabonacci terms you want to display= ";
 cin>>a;
 b=0;
 c=1;
 for (int count=0;count<a;count++)
 {
 
 cout<<b<<",";
 
 sum=b+c;
 
 b=c;
 
 c=sum;
 }
 getch ();
 return 0;
}

Posted by Muhammad Usman Sharif  at 4:33 AM 

----------------------------------------------------------------------------

Saturday, April 20, 2013

program in which user enters 3 inputs and checks if first number is the common divisor of 2nd and 3rd number..

# include <iostream>
# include <conio.h>
int main ()
{
 using namespace std;

int a,b,c;
cout<<"enter a: ";
cin>>a;
cout<<"enter b: ";
cin>>b;
cout<<"enter c: ";
cin>>c;
if(a!=0&&b%a==0&&c%a==0)
cout<<"a is a common divisor of b and c"<<endl;
else
cout<<"a is not a common divisor b and c"<<endl;

getch ();
return 0;
}

Posted by Muhammad Usman Sharif  at 11:07 AM



---------------------------------------------------------------------------------

Program in which user inputs Salary and grade. If grade is equal or greater then 16, it adds 50% to the salary and displays total new salary, if grade less than and equal to 15, it adds 25% to the salary and displays the new total salary.

#include<iostream>
#include<conio.h>
int main()
{
 using namespace std;
 int salary,grade,add1,add2;
 cout<<"Enter your salary= ";
 cin>>salary;
 cout<<"Enter your grade= ";
 cin>>grade;
 if(grade>15)
 {
 
 add1=(salary*50)/100;
 
 cout<<"Your new salary is= "<<salary+add1<<endl;
 }
 else if(grade<15)
 {
 
 add2=(salary*25)/100;
 
 cout<<"Your new salary is= "<<salary+add2<<endl;
 }
 getch();
 return 0;
}
Posted by Muhammad Usman Sharif  at 11:04 AM 

-------------------------------------------------------

Program in which User enters radius of circle and get Area if press 1 and get Circumference if press 2.

#include <iostream>
#include <conio.h>
int main()
{
 using namespace std;
 double a,c;
 int r,e;
 cout<<"enter radius= ";
 cin>>r;
 cout<<"enter 1 for Area and 2 for circumference= ";
 cin>>e;
 if(e==1)
 {
    a=(3.14)*(r)*(r);
 cout<<"area is= "<<a<<endl;
 }
 else if(e==2)
 {
 c=2*(3.14)*(r);
 cout<<"circumference is= "<<c<<endl;
 }
 getch();
 return 0;
}



Posted by Muhammad Usman Sharif  at 11:00 AM



---------------------------------------------------------------------------------------

Check whether 2nd term entered by user is the square of 1st term input by the user.

#include<iostream>
#include <conio.h>
int main()
{
 using namespace std;
 int x,y,z;
 cout<<"enter first number= ";
 cin>>x;
 cout<<"enter second number= ";
 cin>>y;
 z=x*x;
 if(z==y)
 {
    cout<<y<<" is the square of "<<x<<endl;
 }
 else
 {
     cout<<y<<" is not the square of "<<x<<endl;
 }
 getch();
 return 0;
}

Posted by Muhammad Usman Sharif  at 10:58 AM



-------------------------------------------------------------------------------------

To print c Triangle using equation in Visual c++

#include <iostream>
#include <conio.h>
int main()
{
 using namespace std;
 //canvas size
int noOflines =20;
int noOfstarrics =20;
//circle perimeter
int radius=10;
for (int y=0;y<noOflines;y++)
{
 for ( int x=0;x<noOfstarrics;x++)
 
 if (y>-x+20)
  
 cout<<"*";
 
 else
  
 cout<<" ";
 cout <<endl;
     
}
 getch ();
 return 0;
}
Posted by Muhammad Usman Sharif  at 10:53 AM 



---------------------------------------------------------------------------------


Calculate ( + - * / ) of two input numbers in Visual c++

#include<iostream>
#include<conio.h>
int main()
{
 using namespace std;
 int sum,subtract,product,division,a,b,e=0;
 while(e!=5)
 {
 cout<<"\n Enter a:  ";
 cin>>a;
 cout<<"\n Enter b:  ";
 cin>>b;
 cout<<" press\n 1 for sum,\n 2 for subtraction, \n 3 for product,\n 4 for division, and \n 5 for exit: ";
 cin>>e;

 
 if(e==1)
 
 {
 
 sum= a+b;
 
 cout<<sum<<endl;
 
 }
 
 else if(e==2)
 
 {
 
 subtract= a-b;
 
 cout<<subtract<<endl;
 
 }
 
 else if (e==3)
 
 {
 
 product= a*b;
 
 cout<<product<<endl;
 
 }
 
 else
 
 {
 
 division= a/b;
 
 cout<<division<<endl;
 
 }
 }
 getch();
 return 0;
}

Posted by Muhammad Usman Sharif  at 10:51 AM



----------------------------------------------------------------------

Saturday, April 20, 2013

To print a circle using equation in visual c++


#include <iostream>
#include <conio.h>
int main()
{
 using namespace std;
int noOflines =40;
int noOfstarrics =40;
int radius=10;
for (int y=0;y<noOflines;y++)
{
 for ( int x=0;x<noOfstarrics;x++)
 
 if (((x-15)*(x-15)+(y-15)*(y-15))<=radius*radius)
  
 cout<<"*";
 
 else
  
 cout<<" ";
 cout <<endl;
     
}
 getch ();
 return 0;
}
Posted by Muhammad Usman Sharif  at 10:48 AM

-----------------------------------------------------------------------------------------

Find whether entered number is prime or not in visual C++

//check whether no is prime or not?
#include<iostream>
#include<conio.h>
 int main()
{
        using namespace std;

 
 int num, count, check = 0;
 
 cout<<"Enter a number= ";
 
 cin>>num;

 
 if(num == 1)
 
 {
  
 cout<<"The number you entered is a prime number. "<<endl;
 
 }
 
 for(count=2; count<num; count++)
 
 {
  
 if(num%count == 0)
  
 {
   
 check = 1;
   
 break;
  
 }
 
 }
  

  
 if(check == 0)
   {
    cout<<"The number you entered is a prime number. "<<endl;
  
 }
  
 else
  
 {
   
 cout<<" The number you entered is not a prime number."<<endl;
  
 }
 
 
 
 getch();
 
 return 0;
 }
Posted by Muhammad Usman Sharif  at 10:33 AM

--------------------------------------------------------------

No comments:

Post a Comment