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