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

C Programming Lab Manual – 6

Write a C program to swap two numbers from user without third variable.

Function Main
    Declare Integer a
    Declare Integer b
    Output "Enter first number: "
    Input a
    Output "Enter second number: "
    Input b
    Assign a = a + b
    Assign b = a - b
    Assign a = a - b
    Output "Now first number is: " & a
    Output "Now second number is: " & b
End



#include <stdio.h>
int main() {
    int a;
    int b;
    printf("Enter first number:");
    scanf("%d", &a);
    printf("Enter second number:");
    scanf("%d", &b);
    a = a + b;
    b = a - b;
    a = a - b;
    printf("Now first number is second number:%d\n ", a);
    printf("Now second number is first number:%d ", b);
    return 0;
}



Enter first number: 50
Enter second number: 100
Now first number is second number: 100
Now second number is first number: 50

C Programming Lab Manual – 5

Write a C program to swap two numbers from user using third variable.

Function Main
    Declare Integer a
    Declare Integer b
    Declare Integer temp
    Output "Enter first number: "
    Input a
    Output "Enter second number: "
    Input b
    Assign temp = a
    Assign a = b
    Assign b = temp
    Output "Now first number is: " & a
    Output "Now second number is: " & b
End


#include <stdio.h>
int main() {
    int a;
    int b;
    int temp;
    printf("Enter first number:");
    scanf("%d", &a);
    printf("Enter second number:");
    scanf("%d", &b);
    temp = a;
    a = b;
    b = temp;
    printf("Now first number is second number:%d\n", a);
    printf("Now second number is first number:%d ", b);
    return 0;
}



Enter first number: 50
Enter second number: 100
Now first number is second number: 100
Now second number is first number: 50

C Programming Lab Manual – 4

Write a C program to calculate sum of two numbers from user using third variable.

Function Main
    Declare Integer a
    Declare Integer b
    Declare Integer sum
    
    Output "Enter first number: "
    Input a
    Output "Enter second number: "
    Input b
    Assign sum = a + b
    Output "The sum is: " sum
End

#include <stdio.h>
int main() {
    int a;
    int b;
    int sum;
    
    printf("Enter first number: ");
    scanf("%d",&a);
    printf("Enter second number: ");
    scanf("%d",&b);
    sum = a + b;
    printf("The sum is: %d", sum);
    return 0;
}


Enter first number: 5
Enter second number: 10
The sum is: 15