Pages

UNIT 2 : FUNDAMENTAL OF C PROGRAMS

Program 1: Write a program to print/display “Welcome to computer programming”.
#include<stdio.h>
int main()
{
printf(“Welcome to computer programming”);
return 0;
}
Output:
Welcome to computer programming


Program 2: Write a program to print your name, enrollment number, branch and semester.
#include<stdio.h>
int main()
{
printf("Name: Narendra Modi");
        printf("Enrollment Number: 210360116042");
        printf("Branch: Information Technology");
        printf("Semester: 1");

        return 0;
}
Output:
Name: Narendra Modi
Enrollment Number: 210360116042
Branch: Information Technology
Semester: 1


Program 3: Write a program to sum of two numbers.
#include<stdio.h>
int main()
{
int a, b=10, sum;

printf("Enter value of a = ");
scanf("%d", &a);

sum = a + b;
printf("Sum = %d", sum);

return 0;
}
Output:
Enter value of a = 15
Sum = 25


Program 4: Write a program that reads two nos. from key board and gives their addition, subtraction, multiplication, division and modulo. 
#include<stdio.h>
int main()
{
int a, b;
int sum, sub, mul, mod;
float div;

printf("enter value of a:");
    scanf("%d", &a);

printf("enter value of b:");
    scanf("%d", &b);

sum = a + b;
    printf("\nsum = %d", sum);

sub = a - b;
    printf("\nsub = %d", sub);

mul = a * b;
    printf("\nmul = %d", mul);

div = a / b;
    printf("\ndiv = %f", div);

mod = a % 2;
    printf("\nmod = %d", mod);

return 0;
}
Output:
enter value of a:20
enter value of b:10
sum = 30
sub = 10
mul = 200
div = 2
mod = 0


Program 5: Write a program that calculates the area and circumference of circle for a given radius.
#include<stdio.h>
#define PI 3.14 
int main()
{
int r;
float a, c;
    printf("Enter radius of circle: ");
    scanf("%d", &r);

    a = PI * r * r;
    printf("\nArea of circle : %f ",a); 

     c = 2 * PI * r;
    printf("\nCircumference : %f ",c);

    return 0;
}
Output:
Enter radius of circle: 10 
Area of circle : 314.000000
Circumference : 62.800000


Program 6: Write a program that swap (interchange) two numbers.
(With Extra Variable)
#include<stdio.h>
void main()
{
  int x, y, temp;
   
printf("Enter the value of x : ");
    scanf("%d", &x);

printf("Enter the value of y : ");
     scanf("%d", &y);
   
printf("Before Swapping\nx = %d \ny = %d\n", x, y);
   
temp = x;
    x = y;
    y = temp;
   
printf("After Swapping\nx = %d\ny = %d\n", x, y);
    
        return 0;
}
Output:
Enter the value of x and y 10
20
Before Swapping
x = 10
y = 20
After Swapping
x = 20
y = 10


Program 7: Write a program that swap (interchange) two numbers. (Without Extra Variable)
#include<stdio.h>
void main()
{
  int x, y, temp;
   
printf("Enter the value of x : ");
     scanf("%d", &x);

printf("Enter the value of y : ");
     scanf("%d", &y);
    
printf("Before Swapping\nx = %d \ny = %d\n", x, y);
   
x = x + y;
    y = x - y;
    x = x - y;
   
printf("After Swapping\nx = %d\ny = %d\n", x, y);

        return 0;
}
Output:
Enter the value of x and y 10
20
Before Swapping
x = 10
y = 20
After Swapping
x = 20
y = 10


Program 8: Write a program to compute Fahrenheit from Celsius and Celsius from Fahrenheit. (f=1.8*c +32) 
#include <stdio.h>
int main()
{
    float c, f;

    //Fahrenheit from Celsius
     printf("Enter the temperature in Celsius : ");
    scanf("%f", &c);
   
f = 1.8 * c + 32;
   
printf("%f Celsius = %f Fahrenheit" ,c, f);

        //Celsius from Fahrenheit
     printf("Enter the temperature in Fahrenheit : ");
     scanf("%f", &F);
    
        c = (f - 32) / 1.8;    

printf("%f Fahrenheit = %f Celsius" ,c, f);
        
        return 0;
}
Output:
Enter the temperature in Celsius : 10
10 Celsius = 50 Fahrenheit
Enter the temperature in Fahrenheit : 50 
50 Fahrenheit = 10 Celsius


Program 9: Write a c program to prepare pay slip using following data. 
DA = 10% of basic, 
HRA = 7.50% of basic, 
MA = 300, (Medical Allowance)
PF = 12.50% of basic (Provident Fund)
Gross = basic + DA + HRA + MA, 
Net = Gross – Pf. 
#include<stdio.h>
#include<math.h>
int main()
{
    float basic, da, hra, ma, pf, gross, net;
   
printf("Enter basic salary of employee: ");
    scanf("%f", &basic);
   
printf("\nBasic Salary= %f", basic);

da = basic*10.0/100;
printf("\nDA = %f", da);

    hra = basic * 7.5/100;
printf("\nHRA = %f", hra);

    ma = 300;
printf("\nMA = %d", ma);

pf = basic * 12.5 / 100;
printf("\nPF = %f", pf);

    gross = basic + da + hra + ma;
printf("\nGross Salary = %f", gross);

    net = gross - pf;
printf("\nNet Salary = %f", net);
   
    return 0;
}
Output:
Enter basic salary of employee: 10000
Basic Salary=10000.000000
DA=1000.000000
HRA=750.000000
MA=300.000000
Gross Salary = 12050.000000
Net Salary = 10800.000000


Program 10: Write a program to find area of triangle (a=h*b*0.5).  
a = area, h = height, b = base
#include<stdio.h>
void main()
{
float a, h, b;
   
printf("Enter height of triangle: ");
    scanf("%f", &h);
   
printf("Enter base of triangle: ");
    scanf("%f", &b);
   
a = 0.5 * b * h;
    printf("\nArea of triangle : %f ",a);
}
Output:
Enter height of triangle: 10
Enter base of triangle: 10
Area of triangle : 50.000000


Program 11: Write a program to calculate simple interest. 
(i = (p*r*n)/100)  i = Simple interest, p = Principal amount, r = interest rate, n = Number of years.
#include <stdio.h>
#define R 6.8
int main()
{
    int p;
    float i, n;
   
printf("Enter Principal Amount: ");
    scanf("%d", &p);
   
printf("Enter Number of Years: ");
    scanf("%f", &n);
    
i = p*n*R/100;
   
printf("Interest Amount: %f", i);

    return 0;
}
Output:
Enter Principal Amount:: 10000
Enter Number of Years: 1
Interest Amount: 680.000000


Program 12: Write a C program to find out distance travelled by the equation 
d = ut + ½ at^2.
#include<stdio.h>
#include<math.h>
int main()
{
    float d, u, a, t;
    printf("Enter the time in seconds : ");
    scanf("%f", &t);
   
printf("\nEnter the velocity : ");
    scanf("%f", &u);

    printf("\nEnter the acceleration : ");
    scanf("%f", &a);
 
d= u * t + (a * (pow(t, 2)) / 2);
    printf("Total distance travelled is %f", d);

    return 0;
}
Output:
Enter the time in seconds: 10
Enter the velocity: 10
Enter the acceleration: 10
Total distance travelled is 600.000000


Program 13: Write a program that convert days into days and months.
#include<stdio.h>
int main ()
{
    int m, d, rd ;
   
printf("Enter number of days: ") ;
    scanf("%d", &d) ;
   
m = d / 30 ;
    rd = d % 30 ;
   
printf("Months = %d and Remaining Days = %d", m, rd) ;
   
return 0;
}
Output:
Enter number of days: 100
Months = 3 and Remaining Days = 10


Program 14: W.A.P. to demonstrate arithmetic operations.
#include<stdio.h>
int main()
{
int a, b, sum, sub, mul, div, mod;

printf("Enter value of variable a = ");
scanf("%d", &a);

printf("\nEnter value of variable b = ");
scanf("%d", &b);

sum = a+b;
printf("\na+b = %d", sum);

sub = a-b;
printf("\na-b = %d", sub);

mul = a*b;
printf("\na*b = %d", mul);

div = a/b;
printf("\na/b = %d", div);

mod = a%b;
printf("\nRemainder when a divided by b = %d", mod);

return 0;
}
Output:
Enter value of variable a = 10
Enter value of variable b = 5
a+b = 15
a-b = 5
a*b = 50
a/b = 2
Remainder when a divided by b = 0


Program 15: W.A.P to perform different arithmetic operations.
#include<stdio.h>
int main()
{
int a, b;

printf("Enter value of variable a and b");
scanf("%d %d", &a, &b);

printf("\na+b = %d", a+b);
printf("\na-b = %d", a-b);
printf("\na*b = %d", a*b);
printf("\na/b = %d", a/b);

printf("\nRemainder when a divided by b = %d", a%b);

return 0;
}
Output:
Enter value of variable a and b10
5
a+b = 15
a-b = 5
a*b = 50
a/b = 2
Remainder when a divided by b = 0


Program 16: W.A.P to perform different relational operations.
#include<stdio.h>
int main()
{
int a, b;

printf("Enter value of variable a = ");
scanf("%d", &a);

printf("Enter value of variable b = ");
scanf("%d", &b);

printf("\n%d", a = = b);
   
    printf("\n%d", a > b);
   
printf("\n%d", a < b);
   
printf("\n%d", a != b);
   
printf("\n%d", a >= b);
   
    printf(“\n%d", a <= b);

    return 0;
}
Output:
Enter value of variable a = 10
Enter value of variable b = 5
a==b : 0
a>b : 1
a<b : 0
a!=b : 1
a>=b : 1
a<=b : 0


Program 17:  W.A.P to perform different logical operations.
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, c, result;

printf("Enter value of variable a = ");
scanf("%d", &a);

printf("Enter value of variable b = ");
scanf("%d", &b);

printf("Enter value of variable c = ");
scanf("%d", &c);

result = (a == b) && (c > b);
    printf("(a == b) && (c > b) equals to %d \n", result);

    result = (a == b) || (c < b);
    printf("(a == b) || (c < b) equals to %d \n", result);

    result = !(a != b);
    printf("!(a != b) equals to %d \n", result);

    getch();
}
Output:
Enter value of variable a = 10
Enter value of variable b = 5
Enter value of variable c = 2
(a == b) && (c > b) equals to 0 
(a == b) || (c < b) equals to 1 
!(a != b) equals to 0 


Program 18:  W.A.P to perform different assignment operations.
#include<stdio.h>
#include<conio.h>
void main()
{
int a, c;

clrscr();

printf("Enter value of vaiable a = ");
scanf("%d",&a);

c = a;
printf("c=a => %d \n", c);

c += a;
printf("c+=a => %d \n", c);

c -= a;
printf("c- =a => %d \n", c);

c *= a;
printf("c* =a => %d \n", c);

c /= a;
printf("c/=a => %d \n", c);

c%=a;
printf("c modulo a=> %d", c);

getch();
}
Output:
Enter value of vaiable a = 10
c=a => 10 
c+=a => 20 
c- =a => 10 
c* =a => 100 
c/=a => 10 
c modulo a=> 0


Program 19: W.A.P to perform increment and decrement operations.
#include<stdio.h>
#include<conio.h>
void main()
{
int x, y;

clrscr();

printf("Enter value of variable x = ");
scanf("%d", &x);

y = --x;
printf("y=--x => x=%d y=%d\n", x, y);

y = x++;
printf("y=x++ => x=%d y=%d\n", x, y);

y = ++x -y;
printf("y=++x-y => x=%d y=%d\n", x, y);

y = x++-y;
printf("y=x++-y => x=%d y=%d\n", x, y);

getch();
}
Output:
Enter value of variable x = 10
y=--x => x=9 y=9
y=x++ => x=10 y=9
y=++x-y => x=11 y=2
y=x++-y => x=12 y=9


Program 20: W.A.P to perform conditional operator operations.
#include<stdio.h>
#include<conio.h>
void main()
{
int a =10, b, c = 15, max;

clrscr();

b = ( a == 10 ) ? 20 : 30;
printf("Value of b is %d \n", b);

max = (a > b) ? ( (a > c) ? a : c ) : ((b > c)? b : c) ;
printf("Maximum from a = %d, b = %d, c = %d => %d", a, b, c, max);

getch();
}
Output:
Value of b is 20 
Maximum from a = 10, b = 20, c = 15 => 20


Program 21: W.A.P to perform demonstrates use of different types operator.
#include<stdio.h>
#include<conio.h>
void main()
{
        int a, b, c, d;
        
        printf("enter value of a");
        scanf("%d", &a);

        printf("enter value of b");
        scanf("%d", &b);

        c=++a -b;
        printf("\nc = ++a -b => a=%d b=%d c=%d", a, b, c);

        d=b++ +a;
        printf("\nd = b++ +a => a=%d b=%d c=%d d=%d", a, b, c, d);
        
        printf("\na + b => %d", a+b);
        
        printf("\na*=b => %d", a*=b);
        
        printf("\na>10 => %d", a>10);
        
        printf("\na>10 && b<10 => %d", (a>10) && (b<10));
        
        printf("\n(c > d) ? 1 : 0 => %d", (c>d)?1:0);
        
        printf("\na & b => %d", a&b);
        
        getch();
}
Output:
enter value of a : 10
enter value of b : 5
c = ++a -b => a=11 b=5 c=6
d = b++ +a => a=11 b=6 c=6 d=16
a + b => 17
a*=b => 66
a>10 => 1
a>10 && b<10 => 1
(c > d) ? 1 : 0 => 0
a & b => 2


Program 22: W.A.P to perform demonstrates implicit and explicit type casting.
#include<stdio.h>
int main()
{
int a=10, b=3;
float c=3.5, cdiv, sum;

printf("implicit conversations ");
printf("\na = %d, c = %f", a, c);

sum = a + c;
printf("\nsum = a + c => %f", sum);
 
printf(“\nexplicit conversations ");
printf("\na = %d, b = %d", a, b);

cdiv = (float) a/b;
printf("\ncdiv = (float) b / c => %f", cdiv);

return 0;
}

PPS PROGRAMS FOR PRACTICAL EXAM

1. Write a program to find maximum from 3 numbers using else if ladder structure. 2. Write a program to find minimum from 3 numbers using ne...