Pages

Sunday, December 29, 2013

Nested if in two-way selection

Input:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include <stdio.h>
#include <conio.h>
void main (void)
   {
   clrscr();
   int a, b;
   printf("Enter two integer values: ");
    scanf("%d, %d", &a, &b);
   if (a<=b)
     if(a<b)
     printf("\n %d < %d", a, b);
     else // equal
     printf("\n %d = %d", a, b);
   else // a>b
     printf("\n %d > %d", a, b);
     getch();  }// main

Logical AND Operator

Write a program that accept three number from the user and display Largest number


Input:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#include <stdio.h>
#include <conio.h>
void main(void)
{      clrscr();
 int a, b, c;
 printf("Enter three integer values: ");
  scanf("%d, %d, %d", &a, &b, &c);
  if (a>b && a>c)
     printf("%d is largest.", a);
  else if (b>a && b>c)
     printf("%d is largest.", b);
  else
     printf("%d is largest.", c);
}// main

Nested Loop

Write a program that will print Asterisks (*) in following pattern

*******
******
*****
****
***
**
*Input:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include<stdio.h>
#include<conio.h>
void main (void)
{
  for(int outer=7; outer >=1; outer--)
 {
   int inner = 1;
    while (inner<= outer)
    {
 printf("*");
 inner++;
    }
 printf("\n");
  }
   getch();
 }// main

Result:

*******
******
*****
****
***
**
*

Saturday, December 28, 2013

Goto Statement

Write a program to calculate the square root of a Positive number and also handle negative number

Input:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main (void)
{
   float num;
positive:
   printf("Enter a positive number: ");
    scanf("%f", &num);
if (num < 0)
   goto positive;
else
  printf("Square Root of %0.2f is %0.2f", num, sqrt(num));
getch();
}// main

Result:


Enter a positive number: 81
Square Root of 81 is 9


Loop Constructs

DO-WHILE Loop

Write a Program that describe the state of Cell-Phone, its working or not


Input:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include <stdio.h>
#include <conio.h>
void main (void)
{
     char state;
 do
  {
   printf("Enter the current state of Cell-Phone\n(Enter \'W\' for working and \'D\' for dead):  ");
    scanf("%c", &state);
   }
 while (state != 'W' && state != 'D');
 getch();
 }// main
Result:
Enter the current state of Cell-Phone
(Enter \'W\' for working and \'D\' for dead):  W


DO-WHILE LOOP

Write a program to print digit from 1 to 15 

Input:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#include<stdio.h>
#include<conio.h>
void main (void)
{
  int c = 1;
  do
  {
     printf("%d \n", c);
     c++;
     }
  while (c<=15);
     getch();
 }// main

Result:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

While Statement

Write a program to print digit from 1 to 10 

Input: 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#include <stdio.h>
#include <conio.h>
void main (void)
{
 int c = 1;
 while( c <= 10)
 {
  printf("%d\n", c);
  c++;
 }
 getch(); 
}// main

Result:

1
2
3
4
5
6
7
8
9
10

Condition Operator

Write a program that will demonstrate Condition Operator 


Input:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#include <stdio.h>
#include <conio.h>
void main (void)
{
  int a, b;
  printf("Enter two integer number: ");
   scanf("%d %d", &a, &b);
   a > b ?
   printf("%d is larger", a):
   printf("%d is larger", b);
   getch();
 }// main

Result:


Enter two integer number: 87  96
96 is larger

if-else Statement

Write a program that calculate the Square Root of a given number


Input:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include <stdio.h>
#include <math.h>
void main (void)
{
 double x = 0.0, square_root = 0.0;
 printf("Enter a number: ");
 scanf("%lf", &x);
 if (x >= 0)
 { 
  square_root = sqrt(x);
   printf("Square Root of %3.3lf = %3.2lf", x, square_root);
 } 
 else
  printf(Square root cannot be calculated);
 }

Result:

Case1:

Enter a Number: 16
Square Root = 4.00

Case2:

Enter a Number: -56
Square root cannot be calculated

Comparison of Nested if and Sequence of ifs

Write a program that inputs a number from user and determines whether its Positive, Negative or Zero

Input:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include <stdio.h>
#include <conio.h>
void main (void)
{
 int num;
 printf("Enter a number: ");
 scanf("%d", &num);
 if (num>0)
 printf("The number is Positive");
 if(num<0)
 printf("The number is negative");
 if(num==0)
 printf("The number is zero");
 getch();
}

Output:

Case1:
Enter an integer Number: 9
The number is positive

Case2:
Enter an integer Number: -45
The number is negative

Case3:
Enter an integer Number: 0
 The number is zero

Decision Constructs

Programs:

01.     IF Statement
02.     Simple if Statement
03.     if-else Statement
04.     Nested if Statement
05.     Nested if in two-way Selection 
06.    Comparison of Nested if and Sequence of ifs
07.     if-else if Statement
08.     Logical AND Operator
09.     Switch Statement
10.     Condition Operator
11.     XY-coordinate

if-else if statement

Write a program that inputs a number from user and determines whether its positive, Negative or zero


Input:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#include <stdio.h>
#include <conio.h>
void main(void)
{
  int num;
  printf("Enter an integer Number: ");
   scanf("%d", &num);
  if(num>0)
 printf("The number is positive");
  else if (num<0)
 printf("The number is negative");
  else
 printf("The number is zero");
 }// main

Output:
Case1:

Enter an integer Number: 4
The number is positive

Case2:
Enter an integer Number: -6
The number is negative

Case3:
Enter an integer Number: 0
 The number is zero

Switch Statement

Write program that input a character from user and checks whether it’s a Vowel or a Consonant 


Input:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 #include <stdio.h>
 #include <conio.h>
 void main(void)
 {
  clrscr();
  char ch;
   printf("Enter an Alphabet:  ");
    ch = getche();
   switch(ch)
   {
 case 'a':
 case 'A':
    printf("\n It's a vowel");
  break;
 case 'e':
 case 'E':
   printf("\n It's a vowel");
  break;
 case 'i':
 case 'I':
   printf("\n It's a vowel");
  break;
 case 'o':
 case 'O':
   printf("\n It's a vowel");
  break;
 case 'u':
 case 'U':
   printf("\n It's a vowel");
  break;
       default:
   printf("\n It's not a vowel");
  break;
  }
 getch();
 }// main

Result:

Case 1(if it’s vowel):
Enter an Alphabet:  A
It's a vowel

Case 2(if it’s not vowel):
Enter an Alphabet:  H
It's not a vowel

Tuesday, December 24, 2013

Simple if Statement

Write a program that calculate the square root of a given number


Source Code:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#include <stdio.h>
#include <math.h>
void main (void)
{
 double x = 0.0, square_root = 0.0;
 printf("Enter a number: ");
 scanf("%lf", &x);
 if (x > 0)
 square_root = sqrt(x);
 printf("SquareRoot = %2.2lf", square_root);
 }

Result:

Enter a number: 16 
SquareRoot = 4.00

IF Statement

Write a program that tells the age is greater 60 or not 

Source Code:

1
2
3
4
5
6
7
8
9
#include <stdio.h>
 void main ()
{
 int age, status;
printf(Enter the age:  );
scanf(%3d, &age);
status = (age > 50);
printf(Status = %d, status);
}

Result:

Case 1:
Enter the age: 45
Status = 0

Case 2:
Enter the age: 85
Status = 1

Input/ Output

Programs:

01.     Write a program that print "Assalam O Alikum!!! World"
02.     Write a program of addition of two characters     
03.     Write a program to calculate and print the area of a square
04.     Write a program that subtract two numbers and shows their sum on the screen
05.     Write a program that stores the value in separate memory cells
06.     Write a program that will demonstrate the use of Escape Sequence
07.     Write a program that converting Centigrade temperatures to Fahrenheit
08.     Write a program that convert a temperature in degree Fahrenheit  to degree Celsius
09.     Write a program to convert the distance in kilometer into meter
10.     Write a program that calculate circle’s area
11.     Write a program that displays the ASCII code of character

Write a program that displays the ASCII code of character

Source Code:

1
2
3
4
5
6
7
8
9
#include <stdio.h>
#include <conio.h>
void main ()
{
 char ch;
 printf("Enter a character: ");
 ch = getche();
 printf("\n The ANSII code for \'%c\' is %d", ch, ch);
 }

Result:


Enter a character: H
The ANSII code for 'H' is 72

Write a program that stores the value in separate memory cells

Write a program that store the value “R”, “H”, 3.456E-6 and 50 in memory cells. Program should take 1st three value as input data, and use assignment value for last value

Source Code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
 #include <stdio.h>
 #include <conio.h>
 void main ( )
 {
 clrscr ();
 char ch1, ch2;
 float db;
 int n;
 printf("\n  Enter two alphabet value, one exp value and one integer value: \n ");
 scanf(" %c %c %f ", ch1, ch2, db);
 n = 50 ;
 printf(" %d ", n);
 getch();
 }

Result:


Enter two alphabet value, one exp value and one integer value:
R  H  3.045E-6   50


Write a program that calculate circle’s area

Write a program that take radius of circle as input and then computes and displays circle’s area
Formula: Area = PI * r^2

Source Code:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
       #include <stdio.h>
       #include <conio.h>
       void main (void)
# define PI 3.142857
{
 clrscr ();
float  A, r;
 printf("Enter the value of r: ");
 scanf(" %f", &r);
 A = (PI * (r * r));
 printf("\n Area of a circle = %f", A);
 getch();
 }

Result:


Enter the value of r: 3

Area of a circle = 28.285713

Write a program that convert a temperature in degree Fahrenheit to degree Celsius

Formula: C = 9/5 (F - 32)

Source Code:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
 #include <stdio.h>
 #include <conio.h>
 void main (void)
 {

 double F, C;
 printf("Enter temperature value in Fahrenheit: ");
 scanf("%lf",F);
 C = ((5/9) * (F - 32));
 printf(" Celsius = %2.2lf ", C);
 getch();
 }

Result:


Enter temperature value in Fahrenheit: 60
Celsius = 15.56

Write a program to convert the distance in kilometer into meter

Source Code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#include <stdio.h>
#include <conio.h>
void main (void)
{
   Double m, km; 
   printf("Enter distance in km > ");
   scanf(%lf, &km);
  m = km * 1000;
  printf("\n%4.2lf km = %4.2lf m", km, m);
   getch ();
}// main

Result:

Enter distance in km > 78.5
78.50 km = 7850.00 m

Write a program that will demonstrate the use of Escape Sequence

Source Code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#include <stdio.h>
#include <conio.h>
void main (void)
{
   printf("  Name\t\tRoll-No\t\tMarks” );
   printf("\n  ------------------------------------” );
   printf("\n  Amir\t\t  45\t\t425” );
  printf("\n  Hadia\t\t  87\t\t825” );
   getch ();
}// main

Result:

  Name  Roll-No  Marks
  --------------------------------------
  Amir    45  425
  Hadia   87  825

Saturday, November 30, 2013

Introduction of C





Refference by Syed Zulqurnain Jaffery & Ms. Shahina Naz

Write a program of addition of two characters in C

Source Code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#include <stdio.h>
#include <conio.h>
void main (void)
{
 char ch1, ch2, sum; 
         ch1 = '5' ;
 ch2 = '9' ;
 sum = ch1 + ch2;
 printf("Sum = %d", sum);
   getch();
}// main

Result:

Sum = 109

Write a program that converting Centigrade temperatures to Fahrenheit in C

Formula: F = 32 + (C * 180.0/100.0)

Source Code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
  #include <iostream.h>
  #include <conio.h>
  void main(void)
  {  
   clrscr ();
   float F, C;
    C    =   -10;
    F    = 32 + (C * 180.0/100.0);
  printf("F = %2.2f",F);  // use field-width specifier 
     getch();
         } //main

Result:

F = 14.00

Write a program to calculate and print the area of a square in C

Source Code:


1
2
3
4
5
6
7
8
9
#include <stdio.h>
#include <conio.h>
void main (void)
{
   int Height, Width, Area; 
   Height = 8, Width = 5;
   printf("Area of Square = %d", Area);
   getch ();
}// main

Result:


Area of Square = 40

Write a program that subtract two floating point numbers and shows their sum on the screen in C

Source Code:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#include <stdio.h>
#include <conio.h>
void main (void)
{
   Float Num1, Num2, Sub;
   Num1 = 8;
   Num2 = 5;
   Sub = Num1 - Num2;
   printf("%2.2f + %2.2f  = %2.2f ", Num1, Num2, Sub);
   getch ();
      }// main

  Result:


65.77  24.27 = 41.5

Write a program that print "Assalam O Alikum!!! World" in C

Input:

#include <stdio.h>
#include <conio.h>
void main (void)
{
     printf("Assalam O Alikum!!! World");
     getch ();
     } // main

Output:

Assalam O Alikum!!! World

Friday, November 08, 2013

Contact

              If you have any problem to understand any program , you want any help/guid, find any error in program and want to tell us or want to give any feedback, want to encourage us, just feel free to contact us. We will try to reply you soon. You also send E-main on hearty.422422@gmail.combe Happy and enjoy learning!!!!!

Sunday, September 01, 2013

Write a program of Adding a list of number in C++

Add a list of integer from keyboard

Source Code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
   #include <iostream.h>
   #include <conio.h>
   using namespace std;
   int main ()
     {
       int x;
       int sum = 0;
       
      cout << " Enter your number: <EOF> to stop \n";
      while (cin >> x)
           sum += x;
      cout << "\n The total number: " << sum << endl;
      getch ();
      return 0;
             } // main       

Result:


 Enter your number: <EOP> to stop
15
22
3^d
 The total is: 40

Repetition

Write a program of A while loop to print number in C++

Simple while loop that prints numbers 10 per line

Source Code:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
   #include <iostream.h>
   #include <conio.h>
   #include <iomanip.h>
   int main ()
     {
      cout << " Enter an integer between 1 and 100:  ";
      int num;
      cin  >> num;
      
   // Test number
   if (num > 100)
       num = 100;
   int lineCount = 0;
   while (num > 0)
    {
      if (lineCount < 10)
          lineCount++;
      else
      {
          cout << endl;
          lineCount = 1;
          }// else
   cout << setw(4) << num--;
        } //while
        getch ();
        return 0;
               } // main  

Result:


 Enter an integer between 1 and 100:  15
  15  14  13  12  11  10   9   8   7   6
   5   4   3   2   1 

Write a program of Process-control system example in C++


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
 while (1)
 {
  temp = getTemperture();
       if (temp < 68)
     turnOnHeater();
  else if (temp > 78)
     turnOnAirCond();
  else
  {
      turnOfHeater();
      turnOfAirCond();
      } // main
           } // while(1)

Write a program that convert score to grade in C++

This program reads a test score .Calculate the letter grade based on the absolute scale, and print it

Source Code:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
   #include <iostream.h>
   #include <conio.h>
   
   char scoreToGrade (int score);
   
   int main ()
      {
       cout << " Enter the test score(0-100): ";
       int score;
       cin  >> score ;
       
      char grade = scoreToGrade (score);
      cout << " The grade is: " << grade << endl;
      getch();
      return 0;
             } //main
   /* ============= scoreToGrade ===================
   This function calculates the letter grade for a score
   Pre  the parameter score
   Post Return the grade                     */
   char scoreToGrade (int score)
   {
    char grade;
   
   
        if (score >= 90)
             grade = 'A';
   else if (score >=80)
             grade = 'B';
   else if (score >=70)
             grade = 'C';
   else if (score >=60)
             grade = 'D';
   else 
             grade = 'F';
   getch ();
   return grade;    
                }// scoreToGrade 

Result:


 Enter the test score (0-100): 91
 The grade is: A

Write a Program of Student Grading in C++

This program reads a test score, calculates the letter grade for the score, print the grade

Source Code:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
   #include <iostream.h>
   #include <conio.h>
   
   char scoreToGrade (int score);
   
   int main ()
      {
      cout << " Enter the test score (0-100): ";
      int score;
      cin  >> score;
      
     char grade = scoreToGrade (score);
      cout << " The grade is: " << grade << endl;
      getch();
      return 0;
             } //main
   /* ============= scoreToGrade ===================
   This function calculates the letter grade for a score
   Pre  the parameter score
   Post Return the grade                     */
   char scoreToGrade (int score)
   {
     int temp = score / 10;
   char grade;
   switch (temp)
     {     
       case 10 :
       case  9 : grade = 'A';
                break; 
       case  8 : grade = 'B';
                break; 
       case  7 : grade = 'C';
                break; 
       case  6 : grade = 'D';
                break; 
       default : grade = 'f';
                break; 
                }// switch
    getch();
    return grade;    
                      }// scoreToGrade

Result:


 Enter the test score (0-100): 89
 The grade is: B

A switch statement in C++



1
2
3
4
5
6
7
8
9
 switch (print flag)
        {
          case 1: cout << " do case 1 \n";
                  doCase1 ();
          case 2: cout << " do case 2 \n";
                  doCase2 ();
          case 3: cout << " do default \n";
                  doDefault ();                     
               } // switch

Write a program of Multivalued case statment in C++


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
   switch (print flag)
    (
     case 1;
     case 3;
             cout << " Good Day\n";
             cout << " Odds have it!\n";
            break;
     case 2;
     case 4;
             cout << " Good Day\n";
             cout << " Even have it!\n";
            break; 
     default:
             cout << " Good Day, I'm confused!\n";
             cout << " Bye!\n";
            break;
                  )// switch

Friday, August 23, 2013

Write a program of add two digit in C++

This program extracts and adds the two least significant digits of an integer

Source Code:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
      #include <iostream.h>
      #include <conio.h>
      using namespace std;
    
    // Prototype Declarations
      int addTwoDigits (int num);
      int firstDigit   (int);
      int secondDigit (int);
  
    int main ()
     {
      cout << " Enter an integer:  ";
      int number;
      cin  >> number;
      
      int sum = addTwoDigits (number);
      cout << " \n Sum of last two digits is:   "<< sum ;
      getch();
      return 0;
               } // main
      /* ============== addTwoDigits ===================
      Adds the first two digits of an integer.
           Pre  num contains an integer 
           Post return sum of two least significant digits
      */         
        int addTwoDigits (int number)       
      {
            int result = firstDigit (number) + secondDigit (number);
            return result;
            } //add two digit
      /* ============== firstDigits ===================
      Extracts the least significant digits of an integer.
           Pre  num contains an integer 
           Post Return least significant digits
      */
      int firstDigit (int num)
      {
          return (num % 10);
          }
      /* ============== secondDigits ===================
      Extracts second least significant digits of an integer.
           Pre  num contains an integer 
           Post Return digit is 10s position                 */    
      int secondDigit (int num)
      {   
          int result = (num / 10) % 10;
      return result;
      getch ();
      return result;
               } //secondDigits  

Result:


 Enter an integer:  23

 Sum of last two digits is:   5

Wednesday, August 21, 2013

Functions

Selection ----- Making Decisions

Structure of C++ Program

Programs:

01.     Write a program of Binary Expression
02.     Write a program that calculate compound assignment
03.     Write a program of Demonstrate Postfix Increment
04.     Write a program of Demonstrate Prefix (unary) Increment 
05.     Write a program of Precedence
06.     Write a program of Evaluating Expressions
07.     Write a program of Implicit type Conversion
08.     Write a program of Explicit Casts
09.     Write a program that Calculate Quotient and Remainder 
10.     Write a program that calculate Average of Four Numbers
11.     Write a program that convert Radians to Degrees
12.     Write a program that calculate Sales Total
13.     Write a program that calculate Student Score
14.     Write a program that Print Rightmost Digit of Integer

Introduction to C++