Category: Programs

C Program to Simulate PRIORITY CPU Scheduling Algorithm

void main(){int et[20],at[10],n,i,j,temp,p[10],st[10],ft[10],wt[10],ta[10];int totwt=0,totta=0;float awt,ata;char pn[10][10],t[10];//clrscr();printf(“Enter the number of process:”);scanf(“%d”,&n);for(i=0; i<n; i++){printf(“Enter process name,arrivaltime,execution time & priority:”);//flushall();scanf(“%s%d%d%d”,pn[i],&at[i],&et[i],&p[i]);}for(i=0; i<n; i++)for(j=0; j<n; j++){if(p[i]<p[j]){temp=p[i];p[i]=p[j];p[j]=temp;temp=at[i];at[i]=at[j];at[j]=temp;temp=et[i];et[i]=et[j];et[j]=temp;strcpy(t,pn[i]);strcpy(pn[i],pn[j]);strcpy(pn[j],t);}}for(i=0; i<n; i++) }OUTPUT: Enter the number of process:3Enter process name,arrivaltime,execution time & priority:1 2 1 3Enter process name,arrivaltime,execution time & priority:3 5 4 1Enter process name,arrivaltime,execution time & priority:3 4 6 2 Pname arrivaltime …

Start reading C Program to Simulate PRIORITY CPU Scheduling Algorithm

Bubble Sort in C

Bubble sort is a beginners sorting program, it is most learned, but not most used widely due to its time complexity. Bubble sort time complexity is O(n2) Code for Bubble Sort in C include int main(){int a[10],i,j,temp,n;printf(“\n Enter the max no.of Elements to Sort: \n”);scanf(“%d”,&n);printf(“\n Enter the Elements : \n”);for(i=0; ia[j]){temp=a[i];a[i]=a[j];a[j]=temp;}}for(i=0; i<n; i++){printf(“%d\t”,a[i]);}return 0;} Output for …

Start reading Bubble Sort in C

C Program to Calculate Sum of Marks to Demonstrate Structures

include struct student{char name[10];int rollno;int subject[5],total;};main ( ){static struct student s[100];int n,i,j;//clrscr();printf(“Enter the number of Students: “);scanf(“%d”,&n);printf(“Enter the Marks of Five Subjects: “);for(i=0; i<n; i++){printf(“\nEnter student[%d] student marks”,i);s[i].total=0;for(j=0; j<5; j++){scanf(“%d”,&s[i].subject[j]);s[i].total=s[i].total+s[i].subject[j];}printf(“%d\n”,s[i].total);}}OUTPUT:Enter the number of Students: 2Enter the Marks of Five Subjects:Enter student[0] student marks 95 95 95 95 95475 Enter student[1] student marks96 96 96 96 …

Start reading C Program to Calculate Sum of Marks to Demonstrate Structures

C Program Example to Initialize Structure Variable

include struct student{char name[50];int roll;float marks;};void main(){struct student s;printf(“Enter information of students:\n\n”);printf(“Enter name: “);scanf(“%s”,s.name);printf(“Enter Roll Number: “);scanf(“%d”,&s.roll);printf(“Enter Marks: “);scanf(“%f”,&s.marks);printf(“\nPrinting Entered Data: \n”);printf(” Name: %s\n Roll Number: %d\n Marks: %.2f\n”,s.name,s.roll,s.marks);getch();}OUTPUT: Enter information of students: Enter name: LuckyEnter Roll Number: 538Enter Marks: 98 Printing Entered Data:Name: LuckyRoll Number: 538Marks: 98.00

Start reading C Program Example to Initialize Structure Variable

C Program to Find Roots of a Quadratic Equation

include include include main(){float a,b,c,d,x1,x2;int ch;//clrscr();printf(“Enter a,b,c values”);scanf(“%f%f%f”,&a,&b,&c);d=pow(b,2)-4ac;if(d==0)ch=1;else if(d>0){ch=2;}elsech=3;printf(“A=%6.2f\nB=%6.2f\nC=%6.2f\n”,a,b,c);printf(“Discriminate D=%6.2f\n”,d);switch(ch){case 1:printf(“Equal Roots”);x1=x2=-b/(2a); printf(“x1=x2=%6.2f\n”,x1); break; case 2: printf(“Roots are Real and inequal”); x1=(-b+sqrt(d))/(2a);x2=(-b-sqrt(d))/(2a); printf(“x1=%6.2f\n”,x1); printf(“x2=%6.2f\n”,x2); break; case 3: printf(“Imaginary roots \n”); printf(“x1=%6.2f+i%6.2f\n”,-b/(2a), sqrt(-d)/(2a)); printf(“x2=%6.2f-i%6.2f\n”,-b/(2a), sqrt(-d)/(2*a));break;}getch();}OUTPUT: Enter a,b,c values10 20 30A= 10.00B= 20.00C= 30.00Discriminate D=-800.00Imaginary rootsx1= -1.00+i 1.41×2= -1.00-i 1.41

Start reading C Program to Find Roots of a Quadratic Equation

C program to find Sum of Digits of a Positive Integer Number

include main(){int r,n,m,sum;//clrscr();printf(“Enter a positive Integer :”);scanf(“%d”,&n);if(n<0)printf(“The given number is not +ve Integer”);else{sum=0;m=n;while(m!=0){r=m%10;sum+=r;m/=10;}printf(“The given number =%d\n”,n);printf(“Sum of digits of the given number is:%d”,sum);}}You can find the flow chart for the above code here. OUTPUT: Enter a positive Integer :456The given number =456Sum of digits of the given number is:15

Start reading C program to find Sum of Digits of a Positive Integer Number

Find Two’s Complement of a Binary Number Using C programming

main(){int i,test;char binary[L],dup[L],a[L];void input(char binary[]);int check(char binary[]);void compl(char binary[],char a[]);//clrscr();printf(“Enter Binary no of Maximum Length 16\n”);input(binary);strcpy(dup,binary);test=check(binary);if(test==0){printf(“\nInvalid Binary Number.”);exit(1);}printf(“\nOriginal Binary No:%s”,dup);compl(binary,a);printf(“\n2’s complement is %s”,a);getch();}void input(char binary[]){printf(“Enter Binary numbers(0 or 1) without spaces up to Max 16 bits:\n”);scanf(“%s”,binary);return;} int check(char binary[]){int i,l,x=1;l=strlen(binary);for(i=0; ichar a[L];/ int l,i; l=strlen(binary); for(i=l-1; i>=0; i–){if(binary[i]==’0′)a[i]=’1′;elsea[i]=’0′;}for(i=l-1; i>=0; i–){if(i==l-1){if(a[i]==’0′)a[i]=’1′;else{a[i]=’0′;check=1;} }OUTPUT:Enter Binary no of Maximum …

Start reading Find Two’s Complement of a Binary Number Using C programming

C Program to Perform Arithmetic Operations Using Switch

ALGORITHM: Begin Enter a, b values. Print ‘MENU’.(i) Print ‘+ Addition’.(ii) Print ‘- Subtraction’.(iii) Print ‘* Multiplication’.(iv) Print ‘/ Division’.(v) Print ‘% Remainder’.(vi) Print ‘E Exit’. Print ‘Enter your choice’. If op==’E’ then goto step 8 otherwise follow the below steps Switch(op)a. case +:i. Print ‘Addition’.ii. c=a+b.iii. Print ‘Sum=’c.iv. breakb. case -:v. Print ‘Subtraction’.vi. c=a-b.vii. …

Start reading C Program to Perform Arithmetic Operations Using Switch

Check a Character is Vowel or not Using C

Check a Character is Vowel or not Using C programming. The following program uses switch case to check all the vowels. #include <stdio.h>int main(){char ch;printf(“\nEnter Single Character: “);scanf(“%c”, &ch);switch (ch){case ‘a’:case ‘A’:printf(“\n\n%c is a vowel”, ch);break;case ‘e’:case ‘E’:printf(“\n\n%c is a vowel”, ch);break;case ‘i’:case ‘I’:printf(“\n\n%c is a vowel”, ch);break;case ‘o’:case ‘O’:printf(“\n\n%c is a vowel”, ch);break;case ‘u’:case …

Start reading Check a Character is Vowel or not Using C