29
C BASICS Program-1 //operators - eg of arithmetic operators. #include<stdio.h> #include<conio.h> /*void main() { int i,j,k; clrscr(); printf("enter the i value:"); scanf("%d",&i); printf("\nthe value of i is:%d",i); printf("\nenter the j value:"); scanf("%d",&j); printf("\nthe value ofj is:%d",j); k=i+j; printf("\nsum of two numbers:%d",k); getch(); } Program-2 */ //logicaloperators /*#include<stdio.h> #include<conio.h> void main() { int a,b,c; clrscr(); printf("enter tha value of a:"); scanf("%d",&a); printf("enter the value of b:"); scanf("%d",&b); printf("enter the value of c:"); scanf("%d",&c); if((a>b)&&(a>c))

C basics

  • Upload
    msc-cst

  • View
    308

  • Download
    0

Embed Size (px)

Citation preview

Page 1: C basics

C BASICS

Program-1//operators - eg of arithmetic operators.

#include<stdio.h> #include<conio.h>/*void main(){ int i,j,k; clrscr(); printf("enter the i value:"); scanf("%d",&i); printf("\nthe value of i is:%d",i); printf("\nenter the j value:"); scanf("%d",&j); printf("\nthe value ofj is:%d",j); k=i+j; printf("\nsum of two numbers:%d",k); getch();}

Program-2 */

//logicaloperators /*#include<stdio.h>#include<conio.h>void main(){ int a,b,c; clrscr(); printf("enter tha value of a:"); scanf("%d",&a); printf("enter the value of b:"); scanf("%d",&b); printf("enter the value of c:"); scanf("%d",&c); if((a>b)&&(a>c)) printf("a is greater"); else if((b>a)&&(b>c)) printf("b is greater"); else printf("c is greater"); getch();}*

Page 2: C basics

Program-3

//relational oprators/*#include<stdio.h>#include<conio.h>void main(){ int a; clrscr(); printf("enter the value of a:"); scanf("%d",&a); if(a%2==0) printf("given number is even"); else printf("given number is odd"); getch();}*/

Program-4

#include<stdio.h>#include<conio.h>void main(){ int a,b; clrscr(); printf("enter the value of a:"); scanf("%d",&a); printf("enter the value of b:"); scanf("%d",&b);// (a>b)?printf("%d",a):printf("%d",b);(a>b)?printf("a is greater"):printf("b is greater"); getch();}

Page 3: C basics

Program-5

#include<stdio.h>#include<conio.h>//#include"conio.h"void main(){ char ch; clrscr(); printf("enter a character:"); scanf("%c",&ch); printf("%c",ch); printf("the ascii value is: %d",ch); getch();}

Program-6

#include<stdio.h>#include<conio.h>void main(){ int i; clrscr(); for(i=1;i<=10;i++) { if(i==5) break; printf("%d\n",i); } getch();}

Program-7

Page 4: C basics

#include<stdio.h>#include<conio.h>#include<alloc.h>//#include<process.h>struct node{ int data; struct node *link;};void insert(struct node **p);void del(struct node **p);void disp(struct node *p);void count(struct node *p);void main(){ struct node *list; int choice; list=NULL; for(;;) { clrscr(); printf("1Insert\n2Delete\n3Display\n4Count\n5Exit\n"); printf("Enter your choice"); scanf("%d",&choice); switch(choice) {

case 1: insert(&list); break;

case 2: del(&list); break;

case 3: disp(list); break;

case 4: count(list); break;

case 5: exit(0);

}

Page 5: C basics

getch(); }}void del(struct node **p){ int d; struct node *temp,*ptr; printf("Enter the data to be deleted"); scanf("%d",&d); if((*p)->data==d) { *p=(*p)->link; return; } temp=*p; ptr=temp; while(temp!=NULL) { if(temp->data==d) { ptr->link=temp->link; return; } ptr=temp; temp=temp->link; } if(temp==NULL) printf("%d nor found\n",d);}void disp(struct node *p){ if(p==NULL) { printf("List Empty"); return; } while(p!=NULL) { printf("\n%d",p->data); p=p->link; }}void count(struct node *p){ int c=0; if(p==NULL)

Page 6: C basics

{ printf("List empty\n"); return; } while(p!=NULL) { c++; p=p->link; } printf("Count=%d",c);}void insert(struct node **p){ struct node *temp,*ptr=NULL; ptr=malloc(sizeof(struct node)); if(ptr==NULL) { printf("memory allocation unsuccessful"); exit(0); } printf("enter the data:"); scanf("%d",&ptr->data); ptr->link=NULL; if(*p==NULL) { *p=ptr; } else { temp=*p; while(temp->link!=NULL) temp=temp->link; temp->link=ptr; }}

Page 7: C basics

Program-8

#include<stdio.h>#include<conio.h>void main(){ int i; clrscr(); for(i=1;i<=10;i++) { if(i==5) continue; printf("%d\n",i); } getch();}

Program-9

#include<stdio.h>#include<conio.h>void main(){ int i=786; char ch='A'; float f=3.14; double d=4000000000; clrscr(); printf("integer value:%d",i); printf("\ncharacter:%d",ch); printf("\nfloat value:%f",f); printf("\ndouble value:%e",d); getch();}

Page 8: C basics

Program-10

#include<stdio.h>#include<conio.h>void main(){ int i=1,n; //give n=0 clrscr(); printf("enter the value:"); //result--print 1. scanf("%d",&n); do { printf("%d\n",i); i=i+1; }while(i<=n); getch();}

Program-11

#include<stdio.h>#include<conio.h>void main(){ enum emp_dept { assembly,manufact,accounts,stores }; struct employee { char name[30]; int age; float bs; enum emp_dept department; }; struct employee e; clrscr(); strcpy(e.name,"jeevan"); e.age=25; e.bs=5546.25; e.department=accounts; printf("name=%s\n",e.name); printf("age=%d\n",e.age); printf("salary=%f\n",e.bs); printf("dept=%d\n",e.department); if(e.department==accounts) printf("%s is an acountants",e.name);

Page 9: C basics

else printf("%s is not an accountants",e.name); getch();}

Program-12

#include<stdio.h>#include<conio.h>void main(){float a;clrscr();scanf("%f",&a);printf("%0.2f",a);getch();}

Program-13

#include<stdio.h>#include<conio.h>void main() { int i; clrscr(); for(i=1;i<=5;i++) {

printf("%d\n",i); }// printf("\n%*.*f",40,6,n); getch(); }

Page 10: C basics

Program-14

#include<stdio.h>#include<conio.h>void main(){ clrscr(); printf("monday"); printf("\ntuesday"); printf("\nwednesday"); printf("\nsequence altered using goto"); printf("\nmonday"); goto a; b: printf("\ntuesday"); goto end; a: printf("\nwednesday"); goto b; end: getch();}

Program-15

#include<stdio.h>void move(int,char,char,char);void main(){ int n; clrscr(); printf("towers of hanoi problem"); printf("enter the number of discs:"); scanf("%d",&n); move(n,'s','d','t'); getch();}void move(int n,char source,char destination,char temp){ if(n>0) { move((n-1),source,temp,destination); printf("move disk#%d from %c to %c\n",n,source,destination); move(n-1,temp,destination,source); }

Page 11: C basics

}

Program-16

#include<stdio.h>#include<conio.h>void main(){ void incre(); void incre(); clrscr(); incre(); incre(); getch();}void incre(){ static int i=10; printf("%d\n",i); i=i+1;}

Program-17#include<stdio.h>#include<conio.h>void main(){ int a; clrscr(); printf("enter the value of a:"); scanf("%d",&a); switch(a) { case 1: printf("one"); break;

case 2: printf("two"); break;

case 3:

Page 12: C basics

printf("three"); break;

case 4: printf("four"); break;

case 5: printf("five"); break;

default: printf("other numbers"); } getch();}

Program-18

/*#include<stdio.h>#include<conio.h>main(){ char ch; clrscr(); printf("enter the single character:"); scanf("%c",&ch); switch(ch) { case 'a': case 'A': printf("Australia"); break;

case 'i': case 'I': printf("India"); break;

case 'k': case 'K': printf("kenya"); break;

Page 13: C basics

case 'l': case 'L': printf("London"); break;

default: printf("other countries"); } getch();} */

Program-19

#include<stdio.h>#include<conio.h>void main(){ int i=1,n; //give n=0 clrscr(); //result--terminate the loop. printf("enter the value:"); scanf("%d",&n); while(i<=n) { printf("%d\n",i); i=i+1; } getch();}

Program-20

#include<stdio.h>#include<conio.h>#include<math.h>void main(){ int c,d; clrscr(); c=fabs(23.9); d=sqrt(16); printf("c=%d\n",c); printf("d=%d",d); getch();}

Program-21

Page 14: C basics

#include <stdio.h>int i,j;int a[5][5],b[5][5] ,d[5][5];void create(flag,r,c)int flag,r,c;{for (i=0;i<r;i++)for (j=0;j<c;j++) if (flag==0) { printf(" \n enter a[%d,%d]",i+1,j+1); scanf("%d",&a[i][j]); } else { printf("\n enter b[%d,%d]",i+1,j+1); scanf("%d",&b[i][j]); } } void disp(a,r,c) int a[5][5],r,c; { for (i=0;i<r;i++) { for (j=0;j<c;j++) printf("%4d",a[i][j]); printf("\n"); } } void add(r,c) int r,c; { for(i=0;i<r;i++) for(j=0;j<c;j++) d[i][j]=a[i][j]+b[i][j]; } void sub(r,c) int r,c; { for(i=0;i<r;i++) for(j=0;j<c;j++) d[i][j]=a[i][j]-b[i][j]; } void mul(r,c,n) int r,c,n; {

Page 15: C basics

int k; for(i=0;i<r;i++) for(j=0;j<n;j++) { d[i][j]=0; for(k=0;k<c;k++) d[i][j]+=a[i][k]*b[i][j]; } } void trans(int r) { for(i=0;i<r;i++) for(j=0;j<r;j++) d[i][j]=a[i][j]; } void main() { int r,c,m,n,ch; static flag=0; clrscr(); printf("\n enter the order of first matrix:"); scanf("%d %d",&r,&c); create(flag,r,c);flag++; printf("\n enter the order of second matrix;"); scanf("%d %d",&m,&n); create(flag,m,n); do { getch(); clrscr(); printf("\n main menu"); printf("\n 1.addition"); printf("\n 2.subraction"); printf("\n 3.multipy"); printf("\n 4.transpose"); printf("\n 5.exit"); printf("\n enter your choice:"); scanf("%d",&ch); if(ch==1 || ch==2 || ch==3) { printf("\n first matrix:\n"); disp(a,r,c); printf("\n second matrix:\n"); disp(b,n,m); } else {

Page 16: C basics

printf("\n given matrox \n"); disp(a,r,c); } switch(ch) { case 1: if(r==m && c==n) { add(r,c); printf("\n resultant matrix :\n"); disp(d,r,c); } else printf("\n addition not possible"); break; case 2: if(r==m && c==n) { sub(r,c); printf("\n resultant matrix"); disp(d,r,c); } else printf("\n subraction is not possible"); break; case 3: if(c==m) { mul(r,c,m); printf(" resultant matrix:\n"); disp(d,r,n); } else printf("\n multiplication is not possible"); break; case 4: if(r==c) { trans(r); printf("\n resultant matrix: \n"); disp(d,r,c); } else printf("\n transpose not possible"); break; } } while(ch!=5); }

Page 17: C basics

Program-22

#include<stdio.h>#include<conio.h>

void main(){register int a;clrscr();//a=1;//scanf("%d",&a); //cannot get values from user,because register

// class values store to cpu register.cpu registerprintf("%d",a); // didn't address.}

DATA STRUCTURES

Program-1

#include<stdio.h>#include<conio.h>//#include<process.h>#define MAX 10#define TRUE 1#define FALSE 0

struct que{ int rear,front; int items[MAX];};//void ins(struct que*,int);//int overflow(struct que*);//int empty(struct que*);void insert(struct que*,int);//int del(struct que*);void display(struct que*);

int overflow(struct que *q){ if(q->rear>MAX-1)

Page 18: C basics

return(1); else return(0);}int empty(struct que *q){ if(q->rear<q->front) return(1); else return(0);}

void main(){ int choice,x; struct que *q; clrscr(); q->rear=-1; q->front=0; do { printf("\n 1.insert \n 2.delete\n 3.display\n 4.exit\n"); printf("enter the choice:\t"); scanf("%d",&choice); switch(choice) {

case 1: if(overflow(q)) printf("\n queue overflow"); else {printf("\n enter the element to be inserted:\t");scanf("%d",&x);insert(q,x); } break;

case 2: if(empty(q)) printf("\n queue underflow"); else printf("the delete element is %d",del(q)); break;

case 3:

Page 19: C basics

display(q); break;

} }while(choice!=4);}void insert(struct que *q,int x){/* if(overflow(q)) {

q->rear=-1;q->front=0;printf("queue overflow");

} else*/ q->items[++(q->rear)]=x;}int del(struct que *q){/* if(empty(q)) {

printf(" queue underflow");return(1);

} else */ int m=q->items[q->front]; q->front++; return(m);}void display(struct que *q){ int i; if(empty(q)) printf("queue empty"); else for(i=q->front;i<=q->rear;i++) printf("\n%d",q->items[i]);}

Program-2

#include<stdio.h>#include<conio.h>//#include<process.h>#define MAX 10

Page 20: C basics

#define TRUE 1#define FALSE 0struct stack{ int top; int items[MAX];};//int empty(struct stack*);//int overflow(struct stack*);void push(struct stack*,int);//int pop(struct stack*);void display(struct stack*);

int empty(struct stack *s){ if(s->top==-1) return(1); else return(0);}

int overflow(struct stack *s){ if(s->top>=MAX-1) return(1); else return(0);}

void main(){ int choice,x; struct stack *s; clrscr(); s->top=-1; do { printf("\n 1.push \n 2. pop \n 3.display\n 4.exit \n"); printf("enter your choice:"); scanf("%d",&choice); switch(choice) {

case 1: if(overflow(s)) printf("\n stack overflow"); else

Page 21: C basics

{ printf("enter the element to be inserted:\t"); scanf("%d",&x); push(s,x); } break;

case 2: if(empty(s)) printf("\n stack underflow"); else printf("the popped element is%d",pop(s)); break;

case 3: display(s); break;

/* case 4: printf("\nEXIT"); // exit(2); */

} } while(choice!=4);}

void push(struct stack *s,int x){ s->items[++(s->top)]=x;}

int pop(struct stack *s){ int m; m=s->items[s->top]; s->top--; return(m);}

void display(struct stack *s){ int i; if(empty(s)) printf("stack is empty"); else {

Page 22: C basics

printf("display"); for(i=s->top;i>=0;i--) printf("\n%d",s->items[i]); }}

FUNCTIONSProgram-1

//no passing arg & no return type#include<stdio.h>#include<conio.h>void main(){ clrscr();

printline(); value(); printline(); getch();} printline(){

int i;for (i=1;i<=35;i++)//printf("-");printf("%c",'-');printf("\n");

}

value(){

int year,period;float inrate,sum,principal;

printf("enter principle amount"); scanf("%f",&principal); printf("\nenter rate of interest:"); scanf("%f",&inrate); printf("\n enterperiod"); scanf("%d",&period);

Page 23: C basics

sum=principal;year=1;while(year<=period){sum=sum*(1+inrate);year=year+1;}printf("\n%8.2f%5.2f%5d%12.2f\n",principal,inrate,period,sum);getch();}

Program-2

//no passing arguments and return type.

#include<stdio.h>#include<conio.h>void main(){ int a,b,c; int fact(); clrscr(); a=fact()+5;//a=fact(); printf("a=%d\n",a); b=fact()+10;//b=fact(); printf("b=%d\n",b); c=fact()+15;//c=fact(); printf("c=%d",c); getch();}int fact(){ int i,n,f=1; printf("enter n:"); scanf("%d",&n); for(i=1;i<=n;i++) { f=f*i; } return f;}

Program-3

Page 24: C basics

#include<stdio.h>#include<conio.h>#include<dos.h> main() { static int marks[5]={40,90,73,81,2}; int i; clrscr(); printf("marks before sorting\n"); for(i=0;i<5;i++) printf("%d",marks[i]); printf("\n"); sort(5,marks); printf("marks after sorting"); for(i=0;i<5;i++) printf("%4d",marks[i]); printf("\n"); getch(); } sort(int m, int x[]) {

int i,j,t;for(i=1;i<=m-1;i++)for(j=1;j<=m-i;j++)if (x[j-1]>=x[j]){

t=x[j-1];x[j-1]=x[j];x[j]=t;

} }

GRAPHICS