Pgm and OUTPUT
-
Upload
pushpavalli-kathirvelu -
Category
Documents
-
view
238 -
download
1
Transcript of Pgm and OUTPUT
-
7/30/2019 Pgm and OUTPUT
1/52
-
7/30/2019 Pgm and OUTPUT
2/52
C PROGRAM TO PERFORM D=A*A+B*B-.2*C
PROGRAM:
#include< stdio.h>
#includevoid main()
{
int a,b,c,d;
clrscr();
printf(\nEnter a,b,c:);
scanf(%d%d%d,&a,&b,&c);
d=a*a+b*b-2*c;
printf(\nThe value of a*a+b*b-2*c is %d,d);
getch();
OUTPUT:
Enter a,b,c:1 2 3
The value of a*a+b*b-2*c is -1
-
7/30/2019 Pgm and OUTPUT
3/52
C PROGRAM TO PERFORM C=A*A-B*B
PROGRAM:
#include
#include
void main()
{
int a,b,c;
clrscr();
printf(\nEnter two values:);
scanf(%d%d ,&a,&b);
c=a*a-b*b;
printf(\n The value of a*a-b*b is %d,c);getch();
}
OUTPUT:
Enter two values: 5 2
The value of a*a-b*b is 21
-
7/30/2019 Pgm and OUTPUT
4/52
AREA AND CIRCUMFERENCE OF A CIRCLE
PROGRAM
#include
#includevoid main()
{
float r,a,c;
clrscr();
printf(\nEnter radius\n);
scanf(%f,&r);
a=3.14*r*r;
c=2*3.14*r;
printf(\n The area of circle is %f,a );
printf(\n The circumference of circle is %f,c );getch();
}
OUTPUT
Enter radius
2
The area of circle is 12.56
The circumference of circle is 12.56
-
7/30/2019 Pgm and OUTPUT
5/52
TOTAL AND AVERAGE
PROGRAM
#include
#include
void main(){int m1,m2,m3,m4,m5,m6,Total;
float Average;
printf(\n Enter 6 marks\n);scanf(%d%d%d%d%d%d,&m1,&m2,&m3,&m4,&m5,&m6);
Total=m1+m2+m3+m4=m5+m6;
Average=Total/6;printf(\nTotal of six marks is %d\t,Total);
printf(\nAverage of marks is %f,Average);
getch();
}
OUTPUT
Enter 6 marks
85 90 88 90 97 95
Total of six marks is %d
Average of marks is %f
-
7/30/2019 Pgm and OUTPUT
6/52
SWAPPING OF TWO VALUES
PROGRAM:
#include
#include
void main()
{
int a,b,t;
clrscr();
printf(\nEnter the values of a&b\t);
scanf(%d%d,&a,&b);
printf(Values of a and b before swapping a=%d \t b=%d,a,b);
t=a;a=b;
b=t;
printf(\nThe swapped values are a=%d \tb=%d,a,b);
getch();
}
OUTPUT:
Enter the values of a&b 10 35
Values of a and b before swapping a=10 b=35
The swapped values are a=35 b=10
-
7/30/2019 Pgm and OUTPUT
7/52
SWAPPING OF TWO VALUES WITHOUT THIRD VARIBLE
PROGRAM:
#include
#includevoid main()
{
int a,b;
clrscr();
printf(\nEnter the values of a&b\t);
scanf(%d%d,&a,&b);
printf(Values of a and b before swapping a=%d \t b=%d,a,b);
a=a+b;
b=a-b;
b=a-b;printf(\nThe swapped values are a=%d \tb=%d,a,b);
getch();
}
OUTPUT:
Enter the values of a&b 10 35
Values of a and b before swapping a=10 b=35
The swapped values are a=35 b=10
-
7/30/2019 Pgm and OUTPUT
8/52
C PROGRAM TO CONVERT THE NUMBER OF DAYS TO MONTHS
PROGRAM:
#include
#includevoid main()
{
int m,d,nd;
clrscr();
printf(\nEnter the number of days\t);
scanf(%d,&nd);
m=nd/30;
d=nd%30;
printf(\nNumber of months.\t %d,m);
printf(\nNumber of days.\t %d ,d);getch();
}
OUTPUT
Enter the number of days 62
Number of months. 2
Number of days. 2
-
7/30/2019 Pgm and OUTPUT
9/52
C PROGRAM TO CALCULATE SALESMAN SALARY IN AN ORGANIZATION
PROGRAM
#include
#include #define BASIC 2500
#define BONUSRATE 200
#define COM 0.02
void main()
{
int qty;
float GS,PRICE,BONUS,COMM;
clrscr();
printf(\nEnter number of items sold and price\t);
scanf(s%d%f,&qty,&PRICE);BONUS=BONUSRATE*qty;
COMM=COM*qty*PRICE;
GS=BASIC+BONUS+COMM;
printf(\nBONUS.%6.2f\n,BONUS);
printf(\nCOMMISSION.%6.2f\n,COMM);
printf(\nGROSSSAL%6.2f\n,GS);
getch();
}
OUTPUT:
Enter number of items sold and price 80 90
BONUS.16000
COMMISSION.144
GROSSSAL18644
-
7/30/2019 Pgm and OUTPUT
10/52
CENTIGRADE AND FAHRENHEIT CALCULATION
PROGRAM:
#include#include
void main()
{
float c,f,cn,fn;clrscr();
printf(Enter temperature in centigrade:);
scanf(%d,&c);f=1.8*c+32;
printf(\nFahrenheit equivalent is: %.1f\n,f);
printf(Enter temperature in Fahrenheit:);scanf(%d,&fn);
cn=(fn-32)/1.8;
printf(\n Centigrade equivalent is:%.1f\n,cn);getch();
}
OUTPUT:
Enter temperature in centigrade: 20
Fahrenheit equivalent is:68.0
Enter temperature in Fahrenheit: 68
Centigrade equivalent is:20.0
-
7/30/2019 Pgm and OUTPUT
11/52
DECISION MAKING
Decision Making statements in a programming language help the
programmer to transfer the control from one-part to other part of theprogram.
Four Types of Conditional Statements are:
1.IF STATEMENT
2.IF.ELSE STATEMENT
3. NESTED IFELSE STATEMENT.
4.IFELSE LADDER
-
7/30/2019 Pgm and OUTPUT
12/52
BIGGEST OF TWO NUMBERS
PROGRAM
#include
#include
void main()
{
int a,b;
clrscr();
printf(Enter the values of a and b\n);
scanf(%d%d,&a,&b);
if(a>b)
printf(\na is bigger\n);else
printf(\nb is bigger);
getch();
}
OUTPUT
Enter the values of a and b
10 8
a is bigger
-
7/30/2019 Pgm and OUTPUT
13/52
BIGGEST OF THREE NUMBERS
PROGRAM
#include
#include
void main()
{
int a,b,c;
clrscr();
printf(enter the values of a, b and c);
scanf(%d%d%d,&a,&b,&c);
if((a>b)&&(a>c))printf(a is bigger);
else if(b>c)
printf(b is bigger);
else
printf(c is bigger);
getch();
}
OUTPUT
Enter the values of a,b,c
6 34 11
b is bigger
-
7/30/2019 Pgm and OUTPUT
14/52
EVEN OR ODD
PROGRAM
#include
#include
void main()
{
int n;clrscr();
printf(Enter a value\n);
scanf(%d,&n);if((n%2)==0)
printf(\nThe entered number %d is even,n);
elseprintf(\nThe entered number %d is odd,n);
getch();
}
OUTPUT:
Enter a value
8
The entered number 8 is even
-
7/30/2019 Pgm and OUTPUT
15/52
LEAP YEAR
PROGRAM :
#include#include void main()
{
int year;
clrscr();printf(Enter a year);
scanf(%d,&year);
if((year%4)==0)printf(The year %d is a leap year,year);
else
printf(The year %d is not a leap year,year);getch();
}
OUTPUT
Enter a year
2019
The year 2019 is not a leap year
-
7/30/2019 Pgm and OUTPUT
16/52
TO CHECK WHETHER AN ENTERED NUMBER IS IN THE RANGE OR NOT
PROGRAM :
#include#include void main()
{
int n;
clrscr();printf(Enter a value);
scanf(%d,&n);
if((n>10)&&(n
-
7/30/2019 Pgm and OUTPUT
17/52
PROGRAM TO DEMONSTRATE NESTED IF ELSE AND IF ELSE LADDER
/* C PROGRAM TO DEMONSTRATE NESTED IF ELSE*/
#include#include
void main(){
int n;
clrscr();printf(\nEnter a number.);
scanf(%d,&n);
if(n==15)printf(\n Play football);
else
{if(n==10)
printf(\nPlay Cricket);
else
printf(\nDont Play);}
}
OUTPUT:
Enter a number.10
Play Cricket
/* C PROGRAM TO DEMONSTRATE IF ELSE LADDER*/
-
7/30/2019 Pgm and OUTPUT
18/52
#include
#includevoid main()
{
int n;clrscr();
printf(\nEnter a number.);
scanf(%d,&n);if(n==15)
printf(\n Play football);
else if(n==10)
printf(\nPlay Cricket);else
printf(\nDont Play);
}
OUTPUT:
Enter a number.12
Dont Play
Enter a number...15
Play Football
Enter a number10
Play Cricket
-
7/30/2019 Pgm and OUTPUT
19/52
ARMSTRONG NUMBER
PROGRAM:
#include
#include
void main(){
int a,n,r,sum=0;
clrscr();printf(Enter the Number:);
scanf(%d,&n);
a=n;
while(n>0){
r=n%10;
sum=sum+r*r*r;
n=n/10;}
if(a==sum)printf(%d is an Armstrong number.,sum);
else
printf(%d is not an armstrong number.,sum);getch();
}
OUTPUT:
Enter the Number: 153153 is an Armstrong number.
Enter the Number: 123
123 is not an armstrong number.
-
7/30/2019 Pgm and OUTPUT
20/52
BRANCHING STATEMENTS:
1.A Switch Statement is well known branching statement. The Switch Statement is a
Multi-Way branch Statement.
2.If there is a possibility to make a choice from a number of options , this structuredselection is very useful.
3. The Switch Statement requires only one argument of any data type, which is checked
with number of case options.
4. Break statement is used to exit from the current case structure. The Switch Statement is
useful for Writing a Menu Driven Program.
-
7/30/2019 Pgm and OUTPUT
21/52
ARITHMETIC CALCULATION-SWITCH CASE
PROGRAM:
#include#includemain()
{
int a,b,c,ch;
clrscr();printf("\t======");
printf("\n\tMENU");
printf("\n\t======");printf("\n\t 1.add");
printf("\n\t 2.sub");
printf("\n\t 3.mul");printf("\n\t 4.div");
printf("\n\t 5.mod");
printf("\n\t 6.exit");printf("\n\t======");
printf("\n\n\t enter your choice");
scanf("%d",&ch);
if(ch0){
printf("enter two no");
scanf("%d%d",&a,&b);
}switch(ch)
{case 1:
c=a+b;
printf("\n add:%d",c);
break;case 2:
c=a-b;
printf("\n sub:%d",c);break;
case 3:c=a*b;printf("\n mul:%d",c);
break;
case 4:c=a/b;
printf("\n div:%d",c);
break;
-
7/30/2019 Pgm and OUTPUT
22/52
case 5:
c=a%b;
printf("\n mod:%d",c);break;
case 6:
printf("\n terminated by choice");exit();
break;
default:printf("\n invalid choice");
}
getch();
}
OUTPUT:
Menu
1.add
2.sub3.mul
4.div
5.mod
6.exitEnter your choice: 1
Enter two number 4 5
Add: 9
Menu
1.add
2.sub
3.mul4.div
5.mod
6.exitEnter your choice: 3
Enter two number 4 5
Sub: 20
-
7/30/2019 Pgm and OUTPUT
23/52
Menu
1.add
2.sub3.mul
4.div
5.mod6.exit
Enter your choice: 5
Enter two number 4 5Mod: 4
-
7/30/2019 Pgm and OUTPUT
24/52
CONVERTING YEARS INTO MIN,HR,DAYS,MONTH,SEC
PROGRAM:
#include
#include
void main(){
long ch,min,hrs,ds,mon,yrs,sec;
clrscr();printf("\n[1] MINUTES");
printf("\n[2] HOURS");
printf("\n[3] DAYS");
printf("\n[4] MONTHS");printf("\n[5] SECONDS");
printf("\n[6] EXIT");
printf("\nEnter your choice");scanf("%ld",&ch);
if((ch>0) && (ch
-
7/30/2019 Pgm and OUTPUT
25/52
printf("\n Seconds:ld",sec);
break;
case 6:printf("\n Terminated by choice");
exit();
break;default:
printf("\n Invalid choice");
}getch();
}
OUTPUT:
[1] MINUTES
[2] HOURS
[3] DAYS
[4] MONTHS
[5] SECONDS
[6] EXIT
Enter your choice4
Enter years:4
Months:48
Enter your choice1
Enter years:1
Minutes:525600
Enter your choice2
Enter years:1
Hours:8760
-
7/30/2019 Pgm and OUTPUT
26/52
LOOPING STATEMENTS
Different Loop Structures are available in C:
1.WHILE
2.DO..WHILE
3.FOR AND NESTED FOR LOOP.
LOOP:
A Loop is defined as a block of statements which are repeatedly executed for certain number of
times.
LOOP VARIABLE:
It is the variable used in the loop.
INITIALIZATION:
1.It is the first step in which starting and final value assigned to the loop variable.
2.Each time the update value is checked by the loop variable.
INCREMENT/DECREMENT:
It is the Numerical Value added to or subtracted from the variable in each round of the loop.
-
7/30/2019 Pgm and OUTPUT
27/52
FACTORIAL OF A GIVEN NUMBER USING WHILE LOOP.
PROGRAM:
#include#includevoid main()
{
int i=1,fact=1,n;
clrscr();printf(\n Enter a number\n);
scanf(%d,&n);
while(i
-
7/30/2019 Pgm and OUTPUT
28/52
TO CALCULATE FACTORIAL OF A GIVEN NUMBER USING DO-WHILE LOOP.
PROGRAM:
#include
#include
void main(){
int i=1,fact=1,n;
clrscr();printf(\n Enter a number\n);
scanf(%d,&n);
do{
fact=fact*i;
i++;
} while(i
-
7/30/2019 Pgm and OUTPUT
29/52
FACTORIAL OF A GIVEN NUMBER USING FOR LOOP.
PROGRAM:
#include
#include
void main(){
int i,fact=1,n;
clrscr();printf(\nEnter a number\n);
scanf(%d,&n);
for(i=1;i
-
7/30/2019 Pgm and OUTPUT
30/52
SUM OF N NUMBERS USING WHILE
PROGRAM:
#include
#include
void main(){
int i=1,sum=0,n;
printf(\nEnter n value);scanf(%d,&n);
while(i
-
7/30/2019 Pgm and OUTPUT
31/52
SUM OF N NUMBERS USING DO-WHILE
PROGRAM:
#include
#include
void main(){
int i=1,sum=0,n;
printf(\nEnter n value);scanf(%d,&n);
do
{
sum=sum+i;i++;
} while(i
-
7/30/2019 Pgm and OUTPUT
32/52
SUM OF N NUMBERS USING FOR LOOP
PROGRAM:
#include
#include
void main(){
int i,sum=0,n;
printf(\nEnter n value);scanf(%d,&n);
for(i=1;i
-
7/30/2019 Pgm and OUTPUT
33/52
ARRAYS:
An array in C language is a collection of similardata-type, means an array can
hold value of a particular data type for which it has been declared. Arrays can becreated from any of the C data-types int, float, and char. So an integer array can only
hold integer values and cannot hold values other than integer. When we declare array,
it allocates contiguous memory location for storing values whereas 2 or 3 variables of
same data-type can have random locations. So this is the most important difference
between a variable and an array.
Types of Arrays:
One dimension array (Also known as 1-D array).
Two dimension array (Also known as 2-D array).
Multi-dimension array.
Declaration of One Dimensional Arrays:
Syntax: data_type array_name[width];
Example: int roll[8];
http://rajkishor09.hubpages.com/_gsm/hub/Data-Types-in-C-Languagehttp://rajkishor09.hubpages.com/_gsm/hub/Data-Types-in-C-Languagehttp://hubpages.com/_eknow/hub/How-to-work-with-Two-Dimensional-Arrays-in-Chttp://hubpages.com/_eknow/hub/How-to-work-with-Multidimensional-Array-in-C-Programminghttp://rajkishor09.hubpages.com/_gsm/hub/Data-Types-in-C-Languagehttp://hubpages.com/_eknow/hub/How-to-work-with-Two-Dimensional-Arrays-in-Chttp://hubpages.com/_eknow/hub/How-to-work-with-Multidimensional-Array-in-C-Programming -
7/30/2019 Pgm and OUTPUT
34/52
MATRIX MULTIPLICATION
PROGRAM:
#include
#include
void main(){
int a[5][5],b[5][5],c[5][5],r1,r2,c1,c2,i,j,k;
clrscr();step1:
printf(\nEnter the sizeof the matrix A);
scanf(%d%d,&r1,&c1);
printf(\nEnter the size of the matrix B);scanf(%d%d,&r2,&c2);
if(c1==r2)
goto step2;else
printf(\nMultiplication is not possible);
goto step1;step2 :
printf(\nEnter matrix A elements.\n);
for(i=0;i
-
7/30/2019 Pgm and OUTPUT
35/52
for(i=0;i
-
7/30/2019 Pgm and OUTPUT
36/52
TRANSPOSE OF A MATRIX
PROGRAM:
#include
#include
void main(){
int a[10][10],b[10][10],i,j,m,n;
clrscr();printf(Input Row& Column of matrix:);
scanf(%d%d,&n,&m);
printf(Enter the elements of matrix.\n);
for(i=0;i
-
7/30/2019 Pgm and OUTPUT
37/52
SEARCHING AN ARRAY ELEMENT
PROGRAM:
#include
#include
void main()
{int j=0,n;
int x[10];
clrscr();printf("Enter 10 elements of array");
for(j=0;j
-
7/30/2019 Pgm and OUTPUT
38/52
SORTING
PROGRAM:
#include#include
void main()
{int num[5],j,k,s=0;
clrscr();
printf(\n Enter 5 numbers);for(j=0;j
-
7/30/2019 Pgm and OUTPUT
39/52
ADDITION OF EVEN NUMBERS AND MULTIPLICATION OF ODD NUMBERS IN AN
ARRAY.
PROGRAM:
#include
#include
void main()
{int a=0,m=1,num[5];
clrscr();
for(i=0;i
-
7/30/2019 Pgm and OUTPUT
40/52
OUTPUT:
Enter No [1]:1
Enter No [2]:2Enter No [3]:3
Enter No [4]:4
Enter No [5]:5
Odd Number :1Even Number :2
Odd Number :3
Even Number :4Odd Number :5
Addition of even numbers:6Product of odd numbers : 15
-
7/30/2019 Pgm and OUTPUT
41/52
FUNCTIONS:
Function groups a number of program statements into a unit and gives it a name. This
unit can be invoked from other parts of a program. A computer program cannot handle
all the tasks by it self. Instead its requests other program like entities called functions
in C to get its tasks done.
We can divide a long C program into small blocks which can perform a certain task. A
function is a self contained block of statements that perform a coherent task of same
kind.
It has three main parts:
Function Declaration.
Function Definition.
Function Call.
-
7/30/2019 Pgm and OUTPUT
42/52
FACTORIAL USING RECURSION
PROGRAM:
#include#includeint fact();
void main()
{
int n;clrscr();
printf(Enter the number whose factorial is to be found:);
scanf(%d,&n);printf(The factorial of %d is :%d\n,n,fact(n));
getch();
}int fact(n)
int n;
{if(n==0)
return(1);
else
return(n*fact(n-1));}
OUTPUT:
Enter the number whose factorial is to be found: 6
The factorial of 6 is :720
-
7/30/2019 Pgm and OUTPUT
43/52
SWAPPING USING POINTERS
PROGRAM:
#include#include
void interchange(int *x,int *y);
void main()
{int a,b;
clrscr();
printf(Enter the values A and B:);scanf(%d%d,&a,&b);
interchange(&a,&b);
printf(After interchange:\n);printf(a=%d b=%d\n,a,b);
getch();
}void interchange(int *x,int *y)
{
int temp;
temp=*x;*x=*y;
*y=temp;
}
OUTPUT:
Enter the values A and B: 23 56
a=23 b=56
After interchange:
a=56 b=23
-
7/30/2019 Pgm and OUTPUT
44/52
STRUCTURES AND UNIONS
A Structure contains one or more data items of different type in which the individual
elements can differ in type. A Simple Structure may contain integer elements, float
elements and character elements etc., The Individual strucuture elements are called
members.
Syntax:
Struct structure_name
{
Structure member 1;
Structure member 2;
Structure member 3;
}; struct structure_name v1,v2..vn or v[10];
UNIONS:
Union is a derived data type and it is declared like structure. The difference betweenunion and structure is in terms of storage. In Structure each member has its own
memory location. Members of Union Share Common memory location depending
upon largest data type in the union.
union structure_name
{
union member 1;
union member 2;union member 3;
}; struct union_name v1,v2..vn or v[10];
-
7/30/2019 Pgm and OUTPUT
45/52
GRADE CALCULATION-STRUCTURE
PROGRAM:
#include#include
struct stud
{
int rollno;
char name[30];
int m1,m2,m3,total;float avg;
char grade;
}a[25];
void main(){
int i,n;
printf(Enter the number of students:);scanf(%d,&n);
for(i=0;i
-
7/30/2019 Pgm and OUTPUT
46/52
printf(\t\tStudent mark details:\n);
printf(\nRollno\t Name \t\tmark1\tmark2\tmark3\t Total\taverage\t Grade);
for(i=0;i
-
7/30/2019 Pgm and OUTPUT
47/52
EMPLOYEE DETAILS-UNION
PROGRAM:
#include
#include
union{
char name[25];
int idno;float salary;
}desc;
void main()
{strcpy(desc.name,vinod);
clrscr();
printf(\nEmployee Details);printf(\nThe name is %s \n,desc.name);
printf(\nThe idno is %d\n,desc.idno);
printf(\nThe salary is %6.2f\n,desc.salary);desc.idno=10;
printf(\nEmployee Details);
printf(\nThe name is %s \n,desc.name);printf(\nThe idno is %d\n,desc.idno);
printf(\nThe salary is %6.2f\n,desc.salary);
desc.salary=6500.00printf(\nEmployee Details);printf(\nThe name is %s \n,desc.name);
printf(\nThe idno is %d\n,desc.idno);
printf(\nThe salary is %6.2f\n,desc.salary);getch();
}
OUTPUT:
Employee DetailsThe name is vinod
The idno is 26998
The salary is 73784926787784641000000000000.00Employee Details
The name is vinod
The idno is 8192The salary is 6500.00
-
7/30/2019 Pgm and OUTPUT
48/52
-
7/30/2019 Pgm and OUTPUT
49/52
APART FROM THE SYALLBUS:
POINTERS:
PROGRAM TO PRINT THE VALUE AND ADDRESS OF THE ELEMENT USING ARRAY OF
POINTERS
#include#include
void main(){
int *a[3],I;int b=10,c=20,d=30;
a[0]=&b;
a[1]=&c;a[2]=&d;
clrscr();
for(i=0;i
-
7/30/2019 Pgm and OUTPUT
50/52
DYANAMIC MEMORY ALLOCATION:
Syntax:
Pointer_variable=(type cast*)malloc(size in bytes);
PROGRAM TO PRINT VARIABLES FROM MEMORY ADDRESS USING MALLOC
#include
#include
#includevoid main()
{int *a,*b;
clrscr();
printf(Enter the size);scanf(%d, &size);
n=(int *)malloc(size * sizeof(int));
printf(Address of the first byte is..%u\n,n);
printf(Enter the values);for(a=n;a=n;a--)
printf(%d is stored in address %u\n,*a,a);
}
OUTPUT:
Enter the Size5
Address of the first byte is ..1952Enter the values.1 2 3 4 5
Printing the Values..
5 is Stored in address 19604 is stored in address 1958
3 is stored in address 1956
2 is stored in address 1954
1 is stored in address 1952
-
7/30/2019 Pgm and OUTPUT
51/52
PROGRAM TO ALTERING THE ALLOCATED MEMORY USING REALLOC:
#include
#includevoid main()
{
char *p;p=(char *)malloc(6);
strcpy(p,MADRAS);
printf(Memory contains :%s\np);
p=(char *)realloc(p,7);strcpy(p,CHENNAI);
printf(Memory now contains %s\n,p);
free(p);
}
OUTPUT:Memory contains: MADRAS
Memory now contains: CHENNAI.
-
7/30/2019 Pgm and OUTPUT
52/52
GRADE CALCULATION-STRUCTURE
Aim: