C Programming Lab Manual – 16

Write a C program to Find the Largest Number Among Three Numbers.

Function Main
    Declare Real num1
    Declare Real num2
    Declare Real num3
    Output "Enter three numbers: "
    Input num1
    Input num2
    Input num3
    If num1>=num2
        If num1>=num3
            Output "The larggest numner is: " &num1
        Else
            Output "The larggest numner is: " &num3
        End
    Else
        If num2>=num3
            Output "The larggest numner is: " &num2
        Else
            Output "The larggest numner is: " &num3
        End
    End
End

#include <stdio.h>
#include <stdlib.h>

int main()
{
    double num1;
    double num2;
    double num3;

    printf("Enter three numbers:\n");
    scanf("%lf %lf %lf", &num1, &num2, &num3);

    if (num1 >= num2) {
        if (num1 >= num3) {
            printf("The largest number is: %.2lf\n", num1);
        } else {
            printf("The largest number is: %.2lf\n", num3);
        }
    } else {
        if (num2 >= num3) {
            printf("The largest number is: %.2lf\n", num2);
        } else {
            printf("The largest number is: %.2lf\n", num3);
        }
    }

    return 0;
}
Enter three numbers:
10.0
20.0
30.0
The largest number is: 30.00

C Programming Lab Manual – 15

Write a C program to Display Fibonacci Series Up to n Terms.

Function Main
    Declare Integer i
    Declare Integer n
    Declare Integer t1
    Declare Integer t2
    Assign t1 = 0
    Assign t2 = 1
    Declare Integer nextTerm
    Assign nextTerm = t1 + t2
    Output "Enter the number of terms: "
    Input n
    Output "Fibonacci Series: " &t1
    Output ", " &t2
    For i = 3 to n
        Output ", " &nextTerm
        Assign t1 = t2
        Assign t2 = nextTerm
        Assign nextTerm = t1 + t2
    End
End

#include <stdio.h>
int main() {

  int i, n;
  int t1 = 0, t2 = 1;
  int nextTerm = t1 + t2;
  printf("Enter the number of terms: ");
  scanf("%d", &n);

  printf("Fibonacci Series: %d, %d, ", t1, t2);

  for (i = 3; i <= n; ++i) {
    printf("%d, ", nextTerm);
    t1 = t2;
    t2 = nextTerm;
    nextTerm = t1 + t2;
  }

  return 0;
}

Enter the number of terms: 10
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34

C Programming Lab Manual – 14

Write a C program to Find the Factorial of a Number.

Function Main
    Declare Integer n
    Declare Integer i
    Declare Integer fact
    Assign fact = 1
    Output "Enter an integer: "
    Input n
    If (n<0)
        Output "Error! Factorial of a negative number doesn't exist."
    Else
        For i = 1 to n
            Assign fact = fact * i
        End
        Output "Factorial is " &fact
    End
End

#include <stdio.h>
int main() {
    int n, i;
    int fact = 1;
    printf("Enter an integer: ");
    scanf("%d", &n);

    if (n < 0)
        printf("Error! Factorial of a negative number doesn't exist.");
    else {
        for (i = 1; i <= n; ++i) {
            fact *= i;
        }
        printf("Factorial is %d",fact);
    }

    return 0;
}

Enter an integer: 5
Factorial is 120 

C Programming Lab Manual – 13

Write a C program to Reverse a Number.

Function Main
    Declare Integer n
    Declare Integer reverse
    Assign reverse = 0
    Declare Integer remainder
    Output "Enter an integer:  "
    Input n
    While (n!=0)
        Assign remainder = n % 10
        Assign reverse = reverse * 10 + remainder
        Assign n = n / 10
    End
    Output " Reversed number = " &reverse
End
#include <stdio.h>

int main() {

  int n, reverse = 0, remainder;

  printf("Enter an integer: ");
  scanf("%d", &n);

  while (n != 0) {
    remainder = n % 10;
    reverse = reverse * 10 + remainder;
    n /= 10;
  }

  printf("Reversed number = %d", reverse);

  return 0;
}

Enter an integer: 1234
Reversed number = 4321

C Programming Lab Manual – 12

Write a C program to Convert Temperature Celsius to Fahrenheit.

Function Main
    Declare Real celsius
    Declare Real fahrenheit
    
    Output "Enter temperature in Celsius: "
    Input celsius
    Assign fahrenheit = (celsius * 9 / 5) + 32
    Output "Fahernheit is : " &fahrenheit
End





#include <stdio.h>
int main()
{
    float celsius, fahrenheit;
    printf("Enter temperature in Celsius: ");
    scanf("%f", &celsius);
    //celsius to fahrenheit conversion formula
    fahrenheit = (celsius * 9 / 5) + 32;
    printf("Fahrenheit is: %f", fahrenheit);
    return 0;
}

Enter temperature in Celsius: 10
Fahrenheit is : 50 

C Programming Lab Manual – 11

Write a C program to Calculate Area and Circumference of Circle.

Function Main
    Declare Real radius
    Declare Real area
    Declare Real Circumference
    
    Output "Enter the radius of Circle : "
    Input radius
    Assign area = 3.1416 * radius * radius
    Assign Circumference = 2 * 3.1416 * radius
    Output "Area of Circle : " &area
    Output "Circumference of circle: " &Circumference
End





#include <stdio.h>

int main() {
    double radius;
    double area;
    double Circumference;
    
    printf("Enter the radius of Circle : ");
    scanf("%lf", &radius);
    area = 3.1416 * radius * radius;
    Circumference = 2 * 3.1416 * radius;
    printf("Area of Circle : %lf\n",  area);
    printf("Circumference of circle: %lf", Circumference);
    return 0;
}

Enter the radius of Circle : 5
Area of Circle : 78.54
Circumference of circle: 31.416

C Programming Lab Manual – 10

Write a C program to Check Whether a is Even or Odd.

Function Main
    Declare Integer num
    Output "Enter a integer: "
    Input num
    If num % 2 == 0
        Output "number is even"
    False:
        Output "number is odd"
    End
End





#include <stdio.h>
int main() {
    int num;
    printf("Enter an integer: ");
    scanf("%d", &num);

    if(num % 2 == 0)
        printf("%d is even.", num);
    else
        printf("%d is odd.", num);
    
    return 0;
}

Enter an integer: 4
4 is even.

Enter an integer: 5
5 is odd. 

C Programming Lab Manual – 9

Write a C program to visualize a pattern using nested for loop.

Function Main
    Declare Integer i
    Declare Integer j
    Declare Integer n
    Output "Enter a number: "
    Input n
    For i = 1 to n
        For j = 1 to i
            Output "* "
        End
        Output ""
    End
End

#include <stdio.h>
int main() 
{
    int i;
    int j;
    int n;
    printf("Enter a number:");
    scanf("%d", &n);
    for (i = 1; i <= n; i++) 
    {
        for (j = 1; j <= i; j++) 
        {
            printf("* "); // output * pattern or number or symbol
        }
        printf("\n");
    }
    return 0;
}
Enter a number: 5

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

More Examples:

// This code show us left, right, inverted pattern, if we can organized it.
#include <stdio.h>
 
int main()
{
  int i, j, k, rows;
 
  printf("Enter the no. of rows: ");
  scanf("%d", &rows);
 
  for (i = 1; i <= rows; i++)
  {
    // for (j = i; j <= rows; j++) 
      // printf("  ");
 
    for (k = 1; k <= i; k++)
      printf(" %d", k);
 
    printf("\n");
  }
 
  return 0;
}

Enter a number: 5 

 1                                                                              
 1 2                                                                            
 1 2 3                                                                          
 1 2 3 4                                                                        
 1 2 3 4 5                                                                                

#include <stdio.h>  
  
int main()  
{  
      
    int i, j, rows, k = 0;  
    printf (" Enter a number of rows:");  
    scanf ("%d", &rows);   
      
    for ( i =1; i <= rows; i++)  
    {  
        for ( j = 1; j <= rows - i; j++)  
        {  
            printf ("  ");   
        }  
        // use for loop where k is less than equal to (2 * i -1)  
        for ( k = 1; k <= ( 2 * i - 1); k++)  
        {  
            printf ("%d ", k); }
        printf ("\n");  
    }  
    
} 

 Enter a number of rows:5                                                       
        1                                                                       
      1 2 3                                                                     
    1 2 3 4 5                                                                   
  1 2 3 4 5 6 7                                                                 
1 2 3 4 5 6 7 8 9                                                                                

Though it is a Java hollow pattern, it helps us very much in printing hollow patterns in C Program.

C Programming Lab Manual – 8

Write a C program to calculate a rectangle’s Area and Perimeter.

Function Main
    Declare Real base
    Declare Real height
    Declare Real Area
    Declare Real Perimeter
    Output "Enter value of base:"
    Input base
    Output "Enter height: "
    Input height
    Assign Area = (base * height)
    Output "The Area is: " &Area
    Assign Perimeter = (base + height) * 2
    Output "The Perimeter is: " &Perimeter
End




#include <stdio.h>
int main() 
{
    double base;
    double height;
    double Area;
    double Perimeter; 
    printf("Enter value of base: ");
    scanf("%lf", &base);
    printf("Enter height:");
    scanf("%lf", &height);
    Area = base * height;
    printf("The Area is: %lf\n", Area);
    Perimeter = (base + height) * 2;
    printf("The Perimeter is: %lf", Perimeter);
    return 0;
}




Enter value of base: 5.5
Enter height: 5.5
The Area is: 30.2500
The Perimeter is: 22.0000

C Programming Lab Manual – 7

Write a C program to calculate the area of triangle.

Function Main
    Declare Real base
    Declare Real height
    Declare Real Area
    Output "Enter base: "
    Input base
    Output "Enter height:"
    Input height
    Output "The area of ​​the triangle is "
    Output (base * height) / 2
End



#include <stdio.h>
int main() {
    double base;
    double height;
    double area;
    printf("Enter base: ");
    scanf("%lf", &base);
    printf("Enter height:");
    scanf("%lf", &height);
    printf("The area of ​​the triangle is ");
    printf("%lf", base * height / 2 );
    return 0;
}




Enter base: 10.50
Enter height: 10.50
The area of ​​the triangle is: 55.125000