c programming

36
1 CO 103 20120327 Program 1 : Write a program to find area and circumference of circle using symbolic constant // Source Code #include <stdio.h> #include <conio.h> #define PF printf //PF is defined for the function printf #define SF scanf //SF is defined for the function scanf #define PI 3.14 //PI defined for the value 3.14 void main() { float r, ar,cf; clrscr(); PF("Enter the radius of the circle=>" ); SF("%f",&r); cf=2*PI*r; ar=PI*r*r; PF("\nThe Circumfrence of given circle is %f",cf); PF("\n\nThe Area of given circle is %f",ar); getch(); } // Screenshots

Upload: anukruti-tamrakar

Post on 05-Dec-2015

239 views

Category:

Documents


0 download

DESCRIPTION

This file contains some basic C programs

TRANSCRIPT

Page 1: C programming

1

CO 103 20120327

Program 1 : Write a program to find area and circumference of circle using symbolic constant

// Source Code

#include <stdio.h>#include <conio.h>#define PF printf //PF is defined for the function printf#define SF scanf //SF is defined for the function scanf#define PI 3.14 //PI defined for the value 3.14

void main(){

float r, ar,cf;clrscr();PF("Enter the radius of the circle=>" );SF("%f",&r);

cf=2*PI*r; ar=PI*r*r;

PF("\nThe Circumfrence of given circle is %f",cf);

PF("\n\nThe Area of given circle is %f",ar);

getch();}

// Screenshots

Page 2: C programming

2

CO 103 20120327

Program 2 : Write a program to find the Largest of three numbers using macros with Arguments and conditional operator

// Source Code

#include <stdio.h>#include <conio.h>#define PF printf#define SF scanf#define LARGEST(a,b,c) (a>b ? a>c ? a : c: b>c ? b : c ) // MACROS defined with argumentsvoid main(){int p,q,r,max;clrscr();PF("Enter the 3 no.s to find largest from=>\n");SF("\n%d\n%d\n%d",&p,&q,&r);

max= LARGEST(p,q,r);PF("The largest of %d %d %d is %d",p,q,r,max);getch();}

// Screenshots

Page 3: C programming

3

CO 103 20120327

Program 3: Write a menu driven program for +,-,*, / and swap of two integer numbers using switch and do-while.

// Source Code

#include <stdio.h>#include <conio.h>#define PF printf#define SF scanfvoid main(){int a,b,ch1,sum=0,dif=0,pro=0,c;float div=1;char ch2;clrscr();

PF("Enter any two nos=>");SF("%d %d",&a,&b);

PF("\n");PF("\n******************");PF("\n* 1. Addition *");PF("\n* 2. Subtraction *");PF("\n* 3. Product *");PF("\n* 4. Division *");PF("\n* 5. Swap *");PF("\n* 6. Exit *");PF("\n******************");

do{PF("\nEnter your choice=>");SF("%d",&ch1);

switch(ch1){case 1: sum=a+b;

PF("Sum of %d and %d= %d",a,b,sum);break;

case 2: dif=a-b;PF("Difference of %d and %d= %d",a,b,dif);break;

Page 4: C programming

4

CO 103 20120327

case 3: pro=a*b;PF("Product of %d and %d= %d",a,b,pro);break;

case 4: if(b=! 0){div=a/b;PF("Division of %d and %d= %f",a,b,div);}elsePF("Denominator can't be zero");break;

case 5: PF("Before Swapping a=%d and b=%d ",a,b);c=a;a=b;b=c;PF("After Swapping a=%d and b=%d ",a,b);break;

case 6: exit(0);break;

default: PF("Invalid choice!! Please select from the given choices");

} PF("\n\nDo yo wanna CONTINUE ?(Y/N)"); ch2=getche(); }while(ch2=='Y' || ch2=='y'); getch();}

// Screenshots

Page 5: C programming

5

CO 103 20120327

Page 6: C programming

6

CO 103 20120327

Program 4 : Write a menu driven program to check

1. number is even or odd

2. prime or not and

3. factorial of the number

// Source Code

#include <stdio.h>#include <conio.h>#define PF printf#define SF scanfvoid main(){int a,b,ch1,r,i,fact=1;char ch2;clrscr();

PF("Enter any no=>");SF("%d",&a);

PF("\n");PF("\n*************************");PF("\n* 1. Even or Odd *");PF("\n* 2. Prime Or Non Prime *");PF("\n* 3. Factorial *");PF("\n* 4. Exit *");PF("\n*************************");

do{PF("\nEnter your choice=>");SF("%d",&ch1);

switch(ch1){case 1: if(a%2==0)

PF("%d is an Even no !!",a);else

Page 7: C programming

7

CO 103 20120327

PF("%d is an Odd no !!",a);break;

case 2: for(i=2;i<a;i++){r=a%i;}if (r=!0)PF("%d is a Prime no !!",a);elsePF("%d is a Non Prime no !!",a);break;

case 3: for(i=1;i<=a;i++)fact=fact*i;PF("The factorial for %d = %d ",a,fact);break;

case 4: exit(0);break;

default: PF("Enter a valid choice !");

} PF("\n\nDo yo wanna CONTINUE ?(Y/N)");ch2=getche();

}while(ch2=='y'|| ch2=='Y');getch();}

Page 8: C programming

8

CO 103 20120327

// Screenshots

Page 9: C programming

9

CO 103 20120327

Program 5 : Write a program to check whether a year is leap year or not.

// Source Code

#include <stdio.h>#include <conio.h>#define PF printf#define SF scanf

void main(){int yr;clrscr();PF("Enter the year to check for leap year=> ");SF("%d",&yr);if((yr%4)==0 && (yr%100)==!0 || ((yr%400)==0))

PF("%d is a leap year!!",yr) ;elsePF("%d is not a leap year!!",yr) ;getch();}

// Screenshots

Page 10: C programming

10

CO 103 20120327

Program 6: Write a menu driven program to check

1. sum of the digits of a number

2. reverse of the digits of a number

// Source Code

#include <stdio.h>#include <conio.h>#define PF printf#define SF scanf

void main(){int n,i,sum=0,r,t,ch,a=0;char ch2;

clrscr();

PF("Enter any no=>");SF("%d",&n);t=n;PF("\n********************");PF("\n* 1. Sum of Digits *");PF("\n* 2. Reverse num *");PF("\n********************");

do{PF("\nEnter your choice=>");SF("%d",&ch);

switch(ch){

case 1 : while (n!=0) { r=n%10; sum=sum+r; n=n/10; } n=t; PF("The sum of digits of %d = %d ",n,sum); break;

Page 11: C programming

11

CO 103 20120327

case 2 :PF("Reverse of %d is ",n); while (n!=0) { r=n%10; PF("%d",r); n=n/10; } n=t; break;

default : PF("Enter a valid choice"); } PF("\n\n Do you wanna CONTINUE ?(Y/N)");

ch2 = getche(); }while(ch2=='y' || ch2=='Y');

getch();}

// Screenshots

Page 12: C programming

12

CO 103 20120327

Program 5 : Write a program to display n lines of the pattern

1 121 123211234321

// Source Code

#include <stdio.h>#include <process.h>#include <conio.h>#define PF printf#define SF scanf

void main() {int n,i,j,k,l;char ch;clrscr();do {PF("Enter any no=>");SF("%d",&n);

for(i=1;i<=n;++i) { for(j=1;j<=(n-i);j++) {

PF(" "); }

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

PF("%d",k);

for(l=i-1;l>=1;l--) PF("%d",l);

PF("\n"); }

Page 13: C programming

13

CO 103 20120327

PF("\n");PF("\n");

PF("Do you wanna CONTINUE ? (Y/N)=>"); SF("%c",&ch);

} while(ch=='y' || ch=='Y'); ch=getch(); }

// Screenshots

Page 14: C programming

14

CO 103 20120327

Program 8: Write a menu driven program for first n Fibonacci series numbers and prime numbers

// Source Code

#include <conio.h>#define PF printf#define SF scanf

void main() {int a=1,b=1,c,n,i;

clrscr();

PF("Enter no of terms u want in series=>");SF("%d",&n);PF("%d terms are displayed of the given series=> ",n);PF("\n");PF("%d %d",a,b);

PF(" ");for(i=1;i<=(n-2);++i){

c=a+b;PF("%d",c);PF(" ");a=b;b=c;

}getch(); }

// Screenshots

Page 15: C programming

15

CO 103 20120327

Program 9:Write a program to delete array element from a specific position.

// Source Code

#include<stdio.h>#include<conio.h>#define PF printf#define SF scanf

void main(){ int A[10],n,i,p; clrscr(); PF("No. of array elements:"); SF("%d",&n);

PF("Enter array elemets=>\n");

for(i=0;i<n;i++){PF("A[%d]=",i+1);SF("%d",&A[i]);}PF("The elements are =>\n");for(i=0;i<n;i++)

PF("%d ",A[i]);

//Deletion of required array element

PF("Enter the postion of element to be deleted=>") ;SF("%d",&p);

for(i=p;i<n;i++)A[i]=A[i-1];

//Modified array

Page 16: C programming

16

CO 103 20120327

PF("The elements are =>\n");for(i=0;i<n;i++)

PF("%d ",A[i]);

getch();}

Page 17: C programming

17

CO 103 20120327

// Screenshot

Page 18: C programming

18

CO 103 20120327

Program 10:Write a program to remove the duplicate elements from array

Page 19: C programming

19

CO 103 20120327

Program 10:Write a program to remove the duplicate elements from array

// Source Code

#include<stdio.h>#include<conio.h>void main(){ int n, a[10], b[10], count = 0, c, d; clrscr();

printf("Enter number of elements in array\n"); scanf("%d",&n); printf("Enter %d integers\n", n); for(c=0;c<n;c++) scanf("%d",&a[c]); for(c=0;c<n;c++) { for(d=0;d<count;d++) {

if(a[c]==b[d]) break;

} if(d==count) {

b[count] = a[c]; count++;

} } printf("Array obtained after removing duplicate elements\n"); for(c=0;c<count;c++) printf("%d\n",b[c]); getche();}

Page 20: C programming

20

CO 103 20120327

// Screenshot

Page 21: C programming

21

CO 103 20120327

Program 11:Write a program to search an item in a sorted array using binary search.

// Source Code

#include<stdio.h>

#include<stdio.h>#define PF printf#define SF scanf

void main(){int A[4],i,el,flag=0,end=4,beg=0,mid;clrscr();PF("Enter array elemets=>\n");

for(i=0;i<=4;i++){PF("A[%d]=",i+1);SF("%d",&A[i]);}

PF("Enter any element to search in array=>");SF("%d",&el) ;while(beg<end){mid=(beg+end)/2;if(A[mid]==el){flag=1;break;}if(el>A[mid])

beg=mid+1;

elseend=mid-1;}if(flag==1)

Page 22: C programming

22

CO 103 20120327

PF("%d is found at postion %d",el,mid+1);

elsePF("%dis not found",el);

getch();

}

// Screenshot

Page 23: C programming

23

CO 103 20120327

Program 12:Write a program to sort an array in ascending order using bubble sort

// Source Code

#include<stdio.h>#include<stdio.h>#define PF printf#define SF scanf

void main(){int A[5],i,j,c;clrscr();PF("Enter array elemets=>\n");

for(i=0;i<=4;i++){PF("A[%d]=",i+1);SF("%d",&A[i]);}PF("The elements are =>\n");for(i=0;i<=4;i++)

PF("%d ",A[i]);

for(j=4;j>0;j--) { for (i=0;i<=(j-1);i++) { if(A[i]>A[i+1]) { c=A[i]; A[i]=A[i+1]; A[i+1]=c; } } }PF("\nThe Sorted array is :");for(i=0;i<=4;i++)PF(" %2d",A[i]);

getch();}

Page 24: C programming

24

CO 103 20120327

// Screenshot

Page 25: C programming

25

CO 103 20120327

Program 13:Write a program to sort an array in ascending order using selection sort

// Source Code

#include<stdio.h>#include<stdio.h>#define PF printf#define SF scanf

void main(){int A[5],i,j,c;clrscr();PF("Enter array elemets=>\n");

for(i=0;i<=4;i++){PF("A[%d]=",i+1);SF("%d",&A[i]);}PF("The elements are =>\n");for(i=0;i<=4;i++)PF(" %2d ",A[i]);

for(j=0;j<4;j++) { for (i=j+1;i<5;i++)

{ if(A[i]<A[j]) { c=A[i]; A[i]=A[j]; A[j]=c; } }

}PF("\nThe sorted elements are :\n");for(i=0;i<=4;i++)

Page 26: C programming

26

CO 103 20120327

PF(" %2d ",A[i]);getch();}

// Screenshot

Page 27: C programming

27

CO 103 20120327

Program 14:Write a program to merge two sorted arrays in ascending order into third array

// Source Code

#include<stdio.h>#include<stdio.h>#define PF printf#define SF scanf

void main(){int A[10],B[10],C[20],i,j,a,b,k,l,m,n;clrscr();PF("Enter no of array elements in Array A and B respectively ");SF("%d%d",&a,&b);

PF("Enter elements of Array A:\n");for(i=0;i<a;i++){PF("A[%d]=",i+1);SF("%d",&A[i]);}PF("The elements of A are =>\n");for(i=0;i<a;i++)PF(" %d ",A[i]);PF("\n");PF("Enter elements of Array B:\n");for(j=0;j<b;j++){PF("B[%d]=",j+1);SF("%d",&B[j]);}PF("The elements are =>\n");for(j=0;j<b;j++)PF(" %d ",B[j]);k=0;i=0;j=0;

Page 28: C programming

28

CO 103 20120327

PF("\nMerging The array...");while(i<a && j<b){if(A[i]<B[j]){C[k]=A[i];i++;k++;}else{C[k]=B[j];j++;k++;}}if(i==a){for(l=j;l<b;l++){C[k]=B[l];k++;}}else{for(m=i;m<a;m++){C[k]=A[m];k++;}}n=a+b;for(k=0;k<n;k++) { PF(" %d ",C[k]); }

getch();}

Page 29: C programming

29

CO 103 20120327

// Screenshot

Page 30: C programming

30

CO 103 20120327

Program 15:Write a program for the Transpose of a square matrix

// Source Code

#include <stdio.h>#include<conio.h>#define PF printf#define SF scanf

void main(){int A[10][10],r,c,i,j;clrscr();

PF("Enter the Matrix size (a,b)=>(<10)");SF("%d%d",&r,&c);

PF("Enter the matrix of order %d*%d",r,c) ;

for(i=0;i<r;i++){ for(j=0;j<c;j++)

SF("%d",&A[i][j]);

}

PF("The req Matrix is :\n") ;

Page 31: C programming

31

CO 103 20120327

for(i=0;i<r;i++){ for(j=0;j<c;j++)

PF("%d ",A[i][j]);

PF("\n");}

PF("The Transpose of given Matrix is :\n") ; for(i=0;i<c;i++){ for(j=0;j<r;j++)

PF("%d ",A[j][i]);

PF("\n");}

getch();

}

// Screenshot

Page 32: C programming

32

CO 103 20120327