C Programs

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 43

1

INDEX
SNO TOPIC PAGE NO REMARKS
1 PROGRAM TO DESCRIBE DIFFERENT DATA TYPES 3
2 PROGRAM TO FIND REVERSE OF A NUMBER 4
3 PROGRAM TO FIND AREA AND PERIMETER OF 5
CIRCLE
4 BIGGEST OF TWO NUMBERS 6
(USING CONDITIONAL OPERATOR)

5 PROGRAM FOR CONVERSION OF DATA TYPES 7


6 PROGRAM TO PRINT A VALUE IN DIFFERENT 8
FORMATS
7 PROGRAM TO EXCHANGE VALUE OF TWO 9
VARIABLES
8 PROGRAM TO FIND FACTORIAL OF A NUMBER 10
9 PROGRAM TO COMPARE TWO NUMBERS 11
(USING IF ELSE STATEMENT)
10 PROGRAM TO FIND BASIC SALARY USING GIVEN 12
DATA
11 PROGRAM TO CHECK IF A NUMBER IS EVEN OR 14
ODD
12 PROGRAM TO FIND BIGGEST OF 3 NUMBERS 15
(USING NESTED -IF STATEMENT)
13 PROGRAM TO FIND BIGGEST OF 3 NUMBERS 17
(USING LOGICAL OPERATOR)
14 MENU DRIVEN PROGRAM 18
15 PROGRAM TO WRITE DAYS OF WEEK 20
(USING IF ELSE IF LADDER)
16 PROGRAM TO WRITE DAYS OF WEEK 22
(USING SWITCH STATEMENT)
17 PROGRAM TO PRINT EMPLOYEE SALARY BASED ON 24
GENDER AND QUALIFICATION
18 TO DETERMINE THE QUADRANT OF A GIVEN ANGLE 26
19 PROGRAM TO CHECK NUMBER ENTERED IS EVEN 28
OR ODD
(USING CONDITIONAL OPERATOR)
20 PROGRAM TO DEMONSTRATE BREAK STATEMENT 29
21 PROGRAM TO DEMONSTRATE CONTINUE 30
STATEMENT
22 PROGRAM TO DEMONSTRATE 1-D ARRAY 31
2

23 PROGRAM TO DEMONSTRATE 2-D ARRAY 32


24 PROGRAM TO READ MXN MATRIX AND PRINT 34
MATRIX ELEMENTS
25 PROGRAM TO READ THE LIST OF 10 NUMBERS AND 36
PRINT THE LIST IN REVERSE ORDER
26 PROGRAM TO PRINT SUM OF TWO NUMBERS 37
(USING CALL BY VALUE)
27 PROGRAM TO SWAP TWO NUMBERS USING 38
FUNCTION (CALL BY REFERENCE)
28 PROGRAM TO PRINT THE ADDRESS OF A VARIABLE 39
29 PROGRAM TO PRINT THE VALUE OF I USING 40
POINTER VARIABLE
30 PROGRAM TO PASS INDIVIDUAL ELEMENTS OF AN 41
ARRAY TO A FUNCTION ONE BY ONE
(USING CALL BY VALUE)

31 PROGRAM TO PASS INDIVIDUAL ELEMENTS OF AN 42


ARRAY TO A FUNCTION ONE BY ONE
(USING CALL BY REFERENCE)

32 PROGRAM TO PRINT ADDITION OF TWO NUMBERS 43


USING CALL BY REFERENCE
3

1.PROGRAM TO DESCRIBE DIFFERENT DATA TYPES


Code:

#include<stdio.h>

int main()

int a,b;

long x,y;

float f=64.64567890;

double g=11.0123456789;

char ch;

ch= '+';

printf("enter the values\n");

scanf("\n%d%d%ld%ld",&a,&b,&x,&y);

printf("Integer and long values are\n%d\n%d\n%ld\n%ld\n",a,b,x,y);

printf("float and double value are\n%f\n%lf\n",f,g);

printf("character value is\nch=%c and its ASCII value =%d",ch,ch);

return 0;

Output:
4

2. PROGRAM TO FIND REVERSE OF A NUMBER

CODE:

#include<stdio.h>

#include<conio.h>

int main()

int rev,rem,num,n;

printf("enter the number\n");

scanf("%d",&n);

num=n;

while(num!=0)

rem=num%10;

num=num/10;

rev=rev*10+rem;

printf("\nThe number=%d",n);

printf("\nThe reverse number=%d",rev);

getch();

OUTPUT:
5

3.PROGRAM TO FIND AREA AND PERIMETER OF CIRCLE

Code:

#define PI 3.14159

#include<stdio.h>

#include<conio.h>

int main()

int R;

float C;

float area,perimeter;

C=PI;

printf("enter the value of radius of a circle\n");

scanf("%d",&R);

perimeter=2.0*R*C;

area=C*R*R;

printf("the perimeter is %f\n",perimeter);

printf("the area is %f",area);

getch();

output:
6

4. BIGGEST OF TWO NUMBERS (USING CONDITIONAL OPERATOR)

Code:

#include<stdio.h>

int main()

int a,b,max;

printf("Enter values for a and b\n");

scanf("%d%d",&a, &b);

max = a>b ? a : b;

printf("Larger of %d and %d is %d \n",a,b,max);

return 0;

Output:
7

5.PROGRAM FOR CONVERSION OF DATA TYPES

Code:

#include<stdio.h>

#include<conio.h>

int main()

int x;

x= 59.6;

printf("Conversion of float to integer value=%d\n",x);

float y;

y= 21;

printf("Conversion of integer to float=%f\n", y);

char z;

z= 15;

printf("conversion of character to integer value=%d\n",z);

getch();

Output:
8

6.PROGRAM TO PRINT A VALUE IN DIFFERENT FORMATS

Code:

#include <stdio.h>

int main()

int i,j,rows;

printf("Input number of rows=");

scanf("%d",&rows);

for(i=1;i<=rows;i++)

for(j=1;j<=i;j++)

printf("%d",j);

printf("\n");

return 0;

Output:
9

7.PROGRAM TO EXCHANGE VALUE OF TWO VARIABLES

CODE:
#include<stdio.h>

#include<conio.h>

int main()

int a,b,c;

printf("Enter two values\n");

scanf("%d %d",&a,&b);

c=a;

a=b;

b=c;

printf("\nValues after swapping\n a=%d b=%d",a,b);

getch();

OUTPUT:
10

8.PROGRAM TO FIND FACTORIAL OF A NUMBER

CODE:
#include<stdio.h>

int main()

int i,fact=1,number;

printf("enter a number\n");

scanf("%d",&number);

for(i=1;i<=number;i++)

fact=fact*i;

printf("factorial of %d is: %d",number,fact);

return 0;

OUTPUT:
11

9.PROGRAM TO COMPARE TWO NUMBERS(USING IF ELSE STATEMENT)

CODE:
#include<stdio.h>

#include<conio.h>

int main()

int a,b;

printf("enter two numbers\n");

scanf("%d%d",&a,&b);

if (a>b)

printf("\n number %d is bigger",a);

else

printf("\n number %d is big",b);

getch();

OUTPUT:
12

10 .In a company if employee’s basic salary is less than or equal to 10,000. Then
his DA is 31% of basic salary ,HRA is 12% of basic, MA is Rs.250. If his salary is
greater than 10,000 then DA is 35% of basic, HRA is 15% of basic & MA is 350.
Calculate his gross salary.
CODE:
#include<stdio.h>

#include<conio.h>

int main()

int basic,da,hra,ma,gross;

printf("enter basic salary of an employee\n");

scanf("%d",&basic);

if(basic<=10000)

da=basic*31/100;

hra=basic*12/100;

ma=250;

else

da=basic*35/100;

hra=basic*15/100;

ma=350;

gross=basic+da+hra+ma;

printf("salary of an employee=%d",gross);

getch();

}
13

OUTPUT:
14

11.PROGRAM TO CHECK IF A NUMBER IS EVEN OR ODD

CODE:
#include<stdio.h>

#include<conio.h>

int main()

int num;

printf("enter the number\n");

scanf("%d",&num);

if(num%2==0)

printf("\n The number %d entered is Even",num);

else

printf("\n The number %d entered is Odd",num);

getch();

OUTPUT:
15

12.PROGRAM TO FIND BIGGEST OF 3 NUMBERS (USING NESTED -IF STATEMENT)


CODE:
#include<stdio.h>

#include<conio.h>

int main()

int a,b,c;

printf("enter three numbers\n");

scanf("%d%d%d",&a,&b,&c);

if(a>b)

if(a>c)

printf("biggest no is %d",a);

else

printf("biggest no is %d",c);

else

if(b>c)

printf("biggest no is %d",b);

else

printf("biggest no is %d",c);

getch();

}
16

OUTPUT:
17

13.PROGRAM TO FIND BIGGEST OF 3 NUMBERS(USING LOGICAL OPERATOR)

CODE:
#include<stdio.h>

#include<conio.h>

int main()

int a,b,c;

printf("enter the three numbers\n");

scanf("%d%d%d",&a,&b,&c);

if((a>b)&&(a>c))

printf("\n Biggest number=%d",a);

if((b>a)&&(b>c))

printf("\n Biggest number=%d",b);

if((c>a)&&(c>b))

printf("\n Biggest number=%d",c);

getch();

OUTPUT:
18

14.MENU DRIVEN PROGRAM

Code:
#include <stdio.h>

int main()

int choice =0;

printf("MENU:\n1. Coffee\n2. Tea\n3. Exit");

while(1){

printf("\nEnter your choice:");

scanf("%d", &choice);

if(choice == 1)

printf("> Enjoy your Coffee!\n");

else if (choice == 2)

printf("> Enjoy your Tea!\n");

else if (choice == 3)

printf("BYE!!!\n");

break;

else

printf("> Invalid Input\n");

}
19

return 0;

Output:
20

15.PROGRAM TO WRITE DAYS OF WEEK (USING IF ELSE IF LADDER)

CODE:
#include<stdio.h>

#include<conio.h>

int main()

int day;

printf("enter the day of week as numbers\n");

scanf("%d",&day);

if(day==1)

printf("Day is Monday");

else if(day==2)

printf("Day is Tuesday");

else if(day==3)

printf("Day is Wednesday");

else if(day==4)

printf("Day is Thursday");

else if(day==5)

printf("Day is Friday");

else if(day==6)

printf("Day is Saturday");

else if(day==7)

printf("Day is Sunday");

else

printf("Wrong input");

getch();

}
21

OUTPUT:
22

16.PROGRAM TO WRITE DAYS OF WEEK (USING SWITCH STATEMENT)


CODE:
#include<stdio.h>

#include<conio.h>

int main()

int day;

printf("enter the day of week as numbers\n");

scanf("%d",&day);

switch(day)

case 1:

printf("Day is monday");

break;

case 2:

printf("Day is tuesday");

break;

case 3:

printf("Day is wednesday");

break;

case 4:

printf("Day is thursday");

break;

case 5:

printf("Day is friday");

break;

case 6:

printf("Day is saturday");
23

break;

case 7:

printf("Day is sunday");

break;

default:

printf("wrong input");

getch();

OUTPUT:
24

#include<stdio.h>

int main()

char gender;

int yos,qual,salary=0;

printf("enter gender,years of service and qualification(0 = G, 1 = PG):\n");

scanf("%c%d%d",&gender,&yos,&qual);

if (gender=='m'&&yos>=10&&qual==1)

salary = 15000;

else if ((gender=='m'&&yos>=10&&qual==0)||(gender=='m'&&yos<10&&qual==1))

salary = 10000;

else if (gender=='m'&&yos<10&&qual==0)

salary = 7000;

else if (gender=='f'&&yos>=10&&qual==1)

salary = 12000;

else if (gender=='f'&&yos>=10&&qual==0)

salary = 9000;

else if (gender=='f'&&yos<10&&qual==1)

salary = 10000;

else if (gender=='f'&&yos<10&&qual==0)

salary = 6000;

printf("\nSalary of employee=%d\n",salary);

return 0;

}
25

OUTPUT:
26

18. TO DETERMINE THE QUADRANT OF A GIVEN ANGLE

Code:
#include<stdio.h>

int main(void)

int angle;

printf("enter an angle in degrees:");

scanf("%d",&angle);

angle=angle%360;

if(angle%90==0) printf("angle %d lies on an axis\n",angle);

else

if( angle >0 && angle <90)

printf("\nThe angle is in quadrant I");

else if( angle >90 && angle <180)

printf("\nThe angle is in quadrant II");

else if(angle >180 && angle <270)

printf("\nThe angle is in quadrant III");

else if (angle >270 && angle <360 )

printf("\nThe angle is in quadrant IV");

}
27

if(angle%90==0)

switch(angle/90)

case 0:

printf("\nThe angle is on the positive X axis");

break;

case 1:

printf("\nThe angle is on the positive Y axis");

break;

case 2:

printf("\nThe angle is on the negative X axis");

break;

case 3:

printf("\nThe angle is on the negative Y axis");

break;

default:

printf("error");

return 0;

OUTPUT:
28

19. PROGRAM TO CHECK NUMBER ENTERED IS EVEN OR ODD


(USING CONDITIONAL OPERATOR)

CODE:
#include <stdio.h>

int main()

int num;

printf("Enter a number: ");

scanf("%d", &num);

(num % 2 == 0) ? printf("%d is even\n", num) : printf("%d is odd\n", num);

return 0;

OUTPUT:
29

20. PROGRAM TO DEMONSTRATE BREAK STATEMENT

CODE:
#include<stdio.h>

int main()

int i;

for(i=0;i<10;i++)

if (i==7)

break;

printf("%d\n",i);

return 0;

Output:
30

21 .PROGRAM TO DEMONSTRATE CONTINUE STATEMENT

CODE:
#include<stdio.h>

int main()

int i;

for(i=0;i<15;i++)

if (i==9)

continue;

printf("%d\n",i);

return 0;

Output:
31

22 .PROGRAM TO DEMONSTRATE 1-D ARRAY


CODE:
#include<stdio.h>

#include<conio.h>

int main ()

int a[5], i;

printf("Enter Array Elements=\n");

for(i=0; i<5; i++)

scanf("%d", &a[i]);

printf("Entered Array Elements are : ");

for(i=0; i<5; i++)

printf("%d",a[i]);

getch();

OUTPUT:
32

23. PROGRAM TO DEMONSTRATE 2-D ARRAY


CODE:
#include <stdio.h>

int main()

int rows,cols;

printf("Enter the number of rows: ");

scanf("%d", &rows);

printf("Enter the number of columns: ");

scanf("%d", &cols);

int arr[rows][cols];

printf("Enter the elements of the 2D array:\n");

for(int i = 0; i < rows; i++)

for(int j = 0; j < cols; j++)

printf("Enter element at row %d, column %d: ", i + 1, j + 1);

scanf("%d", &arr[i][j]);

printf("The 2D array is:\n");

for (int i = 0; i < rows; i++)

for (int j=0;j<cols;j++)

printf("%d",arr[i][j]);

printf("\n");
33

return 0;

OUTPUT:
34

24.PROGRAM TO READ MXN MATRIX AND PRINT MATRIX ELEMENTS

Code:
#include<stdio.h>

int main()

int array1[10][10],i,j,m,n,sum = 0;

printf("Enter no. of rows:");

scanf("%d", &m);

printf("Enter no. of cols:");

scanf("%d",&n);

printf("Enter values to the matrix ::\n");

for(i=0;i<m;i++)

for(j=0;j<n;j++)

printf("Enter a[%d][%d] value::",i,j);

scanf("%d",&array1[i][j]);

printf("\nThe given matrix is \n");

for(i=0;i<m;++i)

for(j=0;j<n;++j)

printf("\t%d",array1[i][j]);

printf("\n");
35

return 0;

Output:
36

25.PROGRAM TO READ THE LIST OF 10 NUMBERS AND PRINT THE LIST IN REVERSE
ORDER
Code:

#include<stdio.h>

int main()

int S{10};

printf("Enter the list of 10 numbers:\n");

for (i=0;i<number;i++)

scanf("%d", &S[i]);

printf("\nThe reverse order of given list is:");

for (int i=9;i>=0;i--)

printf("\n%d",S[i]);

return 0;

Output:
37

26.PROGRAM TO PRINT SUM OF TWO NUMBERS (USING CALL BY VALUE)


CODE:
#include<stdio.h>

int main()

int calsum(int,int);

int num1, num2, sum;

printf("Enter the two numbers : ");

scanf("%d %d", &num1, &num2);

sum = calsum(num1, num2);

printf("\nsum of %d and %d=%d",num1,num2,sum);

return 0;

int calsum(int num1, int num2)

int num3;

num3 = num1 + num2;

return (num3);

Output:
38

27.PROGRAM TO SWAP TWO NUMBERS USING FUNCTION (CALL BY REFERENCE)

Code:
#include <stdio.h>

int main()

int swap(int *x , int *y );

int x, y;

printf("\nEnter value of x & y:\n");

scanf("%d %d", &x, &y);

swap(&x, &y);

printf("\nthe swapped numbers are %d %d",x,y);

return 0;

swap (int *a, int *b)

int temp;

temp = *a;

*a = *b;

*b = temp;

Output:
39

28.PROGRAM TO PRINT THE ADDRESS OF A VARIABLE

CODE:
#include<stdio.h>

int main()

int i=7;

printf("value of i=%d",i);

printf("\naddress of i=%u",&i);

return 0;

Output:
40

29.PROGRAM TO PRINT THE VALUE OF I USING POINTER VARIABLE

CODE:
#include<stdio.h>

int main()

int i=5,*j;

j=&i;

printf("\nThe value of i=%d",i);

printf("\nThe address of i=%u",&i);

printf("\nThe address of i=%u",j);

printf("\nThe value of i=%d",*j);

return 0;

Output:
41

30.PROGRAM TO PASS INDIVIDUAL ELEMENTS OF AN ARRAY TO A FUNCTION


ONE BY ONE
(USING CALL BY VALUE)

CODE:
#include<stdio.h>

#include<conio.h>

int main()

{ int show(int);

int m[]={10,40,30,20,50,60},i;

for(i=0;i<6;i++)

show(m[i]);

return 0;

show(int marks)

printf("\t%d",marks);

Output:
42

31.PROGRAM TO PASS INDIVIDUAL ELEMENTS OF AN ARRAY TO A FUNCTION


ONE BY ONE
(USING CALL BY REFERENCE)

CODE:
#include<stdio.h>

#include<conio.h>

int show(int*);

int main()

int m[]={30,40,50,60,70,80},i;

for(i=0;i<6;i++)

show(m+i);

show(int *marks)

printf("\t%d",marks);

return marks;

OUTPUT:
43

32.PROGRAM TO PRINT SUM OF TWO NUMBERS USING CALL BY REFERENCE

CODE:
#include <stdio.h>

int add(int *,int *);

int main()

int num1, num2, result;

printf("\n Enter the two number: ");

scanf("%d %d", &num1, &num2);

result =add(&num1, &num2);

printf("\n Addition of %d and %d is %d", num1, num2, result);

int add(int *num1, int *num2)

int res;

res = *num1 + *num2;

return res;

OUTPUT:

You might also like