ASHA

You might also like

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

Sector-36 A, Chandigarh

Website: mcmdavcwchd.edu.in

Practical File on Computational Techniques-II


MATH-692S

(C Programming)

Submitted to: Submitted by:


Dr. Navjot Kaur Asha( Msc.-II)
Assistant Professor 8533
(Department of Mathematics)

1
SR.NO PROGRAM NAME PAGE NO. TEACHER’S
SIGNATURE

1 Describe data types 4

2 Reverse of a number 5

3 Biggest of two numbers 6

4 Perimeter and area of circle 7

5 Conversion of data types 8

6 Swap Values of two Variables 9

7 Convert Fahrenheit to Degree Celsius 10

8 Factorial of a number 11

9 Compare using if else statement 12

10 Gross salary of employee using if else statement 13

11 A number is even or odd 14

12 Larger of three nos. using nested if else statement 15

13 Larger of three nos. using logical operator 16

14 Days of week using if else if ladder 17

15 Menu driven Program 18

16 Quadrant of a given angle 19

2
17 Salary of employee using if else if ladder 21

18 Demonstration of break statement 22

19 Demonstration of Continue statement 23

20 Conditional operator 24

21 Demonstration of 1-D Array 25

22 Demonstration of 2-D Array 26

23 Read m*n matrix and print its elements 27

24 Read a list and print in reverse order 29

25 Sum of two numbers(call by value) 30

26 Sum of two numbers(call by reference) 31

27 Swap of two numbers(call by value) 32

28 Swap of two numbers(call by reference) 33

29 Print address of a variable 34

30 Print value of variable using pointer variable 35

31 Print individual elements of an array to a function 36


one by one using call by value

/*Program to describe data types*/

3
CODE:
#include<conio.h>

#include<stdio.h>

int main()

{ int a,b;

long x,y;

float f=64.64567890;

double g=11.0123456789;

char ch;

unsigned char u;

ch= '+';

u= 'A';

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

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

printf("\n%f\n%lf",f,g);

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

printf("\n u=%c and its ASCII value=%d",u,u);

return 0;

OUTPUT:

/*Program to print reverse of a number*/

4
CODE:
#include<conio.h>

#include<stdio.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:

/*Program to print biggest of two numbers using conditional


operator*/
5
CODE:
#include<conio.h>

#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:

/*Write a program to print perimeter and area of circle*/

6
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 and area is %f %f",perimeter,area);

getch();

OUTPUT:

/*Program for conversion of data types*/

7
CODE:
#include<stdio.h>

#include<conio.h>

int main()

int x;

x= 27.67;

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

float y;

y= 55;

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

char z;

z= 9;

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

getch();

Output:

/* Program to swap values of two variables*/

8
CODE:
#include<stdio.h>

#include<conio.h>

int main()

inta,b,c;

printf(" \nEnter 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:

/*Program to convert degree Fahrenheit to degree celcius */

9
CODE:
#include<stdio.h>

#include<conio.h>

int main()

int Fahrenheit, Celsius;

printf("enter temprature in degree fahrenheit:");

scanf("\n %d",&Fahrenheit);

Celsius = ((Fahrenheit-32)*5)/9;

printf("\n Temperature in Celsius is : %d",Celsius);

return 0 ;

OUTPUT:

/*Program to find factorial of a number*/


10
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:

/*Program to compare two numbers(using if else statement)*/


11
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:

/*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

12
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();}

OUTPUT:

/*Program to check if a number is even or odd*/


CODE:

13
#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:

/*Program to find biggest of 3 numbers (using nested -if statement)*/


CODE:

14
#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();
}

OUTPUT:

/*Program to find biggest of 3 numbers(using logical operator)*/


CODE:

15
#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:

/*Program to print days of week using if else if ladder*/


CODE:

16
#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();
}

OUTPUT:

/*Menu driven program*/


CODE:

17
#include <stdio.h>
int main()
{
int choice =0;
printf("MENU:\n1. BURGER\n2. PIZZA\n3. Exit");
while(1){
printf("\nEnter your choice:");
scanf("%d", &choice);
if(choice == 1)
{
printf("> Enjoy your BURGER!\n");
}
else if (choice == 2)
{
printf("> Enjoy your PIZZA!\n");
}
else if (choice == 3)
{
printf("BYE!!!\n");
break;
}
Else
{
printf("> Invalid Input\n");
}
}
return 0;
}
OUTPUT:

/*Program to determine the quadrant of a given angle*/

18
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");
}

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;

19
}

OUTPUT:

/*Program to print salary of employees based on gender and


qualification*/
20
CODE:
#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;
}

OUTPUT:

/*Program to demonstrate break statement*/

21
CODE:
#include<stdio.h>

int main()

int i;

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

if (i==3)

break;

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

return 0;

OUTPUT:

/*Program to demonstrate continue statement*/

CODE:

22
#include<stdio.h>

int main()

int i;

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

if (i==3)

continue;

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

return 0;

OUTPUT:

/* Program to check number entered is even or odd


(using conditional operator)*/

23
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:

/*Program to demonstrate 1-d array*/

CODE:

24
#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:

/*Program to demonstrate 2-d array*/

25
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");
}
return 0;
}

OUTPUT:

/*Program to read mxn matrix and print matrix elements*/


CODE:

26
#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");

27
}

return 0;

OUTPUT:

/*Program to read A list of 10 numbers and print the list in reverse order*/

CODE:

28
#include<stdio.h>
int main()
{
int S[10];
int i;
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 ( i=9;i>=0;i--)
{
printf("\n%d",S[i]);
}
return 0;
}
OUTPUT:

/*Program to print sum of two numbers using call by value*/

CODE:

29
#include<stdio.h>

int main()

int calsum(int,int);

int num1, num2, sum;

printf("\nEnter 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:

/*Program to print sum of two numbers using call by reference*/

CODE:

30
#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:

/*Program to swap two numbers using function (call by value)*/

CODE:

31
#include <stdio.h>

#include<conio.h>

int main()

{ int swap (int , int );

int x, y;

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

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

swap(x, y);

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

return 0;

swap (int a, int b)

int temp;

temp = a;

a = b;

b = temp;

OUTPUT:

/*Program to swap two numbers using function (call by reference)*/

CODE:

32
#include <stdio.h>

Int main()

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

int x, y;

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

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

swap(&x, &y);

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

return 0;

swap (int *a, int *b)

int temp;

temp = *a;

*a = *b;

*b = temp;

OUTPUT:

/*Program to print the address of a variable*/

CODE:

33
#include<stdio.h>

int main()

int i=5;

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

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

return 0;

OUTPUT:

/*Program to print the value of i using pointer variable*/

CODE:

34
#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:

/*Program to pass individual elements of an array to a function one


by one using call by value*/

CODE:

35
#include<stdio.h>

#include<conio.h>

int main()

{ int show(int);

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

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

show(m[i]);

return 0;

show(int marks)

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

OUTPUT:

36
37

You might also like