Posts

factorial of a number

write a program to find the factorial of a number #include <stdio.h> int main (){ int i=1,f=1,num; printf("\nEnter a number:"); scanf("%d",&num); while (i<=num){ f=f*i; i++; } printf("\nFactorial of %d is:%d",num,f); return 0; }

roots of a quadratic equation

write c program to find the roots of a quadratic equation #include<stdio.h> #include<conio.h> #include<math.h> #include<process.h> void main() { float a,b,c,x1,x2,disc; clrscr(); printf("Enter the co-efficients\n"); scanf("%f%f%f",&a,&b,&c); disc=b*b-4*a*c;/*to find discriminant*/ if(disc>0)/*distinct roots*/ { x1=(-b+sqrt(disc))/(2*a); x2=(-b-sqrt(disc))/(2*a); printf("The roots are distinct\n"); exit(0); } if(disc==0)/*Equal roots*/ { x1=x2=-b/(2*a); printf("The roots are equal\n"); printf("x1=%f\nx2=%f\n",x1,x2); exit(0); } x1=-b/(2*a);/*complex roots*/ x2=sqrt(fabs(disc))/(2*a); printf("The roots are complex\n"); printf("The first root=%f+i%f\n",x1,x2); printf("The second root=%f-i%f\n",x1,x2); getch(); }

simpson method

Write C program to implement Simpson method #include<stdio.h> #include<conio.h> #include<math.h> char postfix[80]; float stack[80]; char stack1[80]; int top=-1,top1=-1; float eval(char postfix[], float x1); void infix_postfix(char infix[]); main() { float x0, xn, h, s,e1,e2, e3; char exp[80], arr[80]; int i,n,l=0; clrscr(); printf(“\nEnter an expression: “); gets(exp); puts(“Enter x0, xn and number of sub-intervals: “); scanf(“%f%f%d”, &x0, &xn, &n); h=(xn-x0)/n; if(exp[0]==’l'&& exp[1]==’o'&& exp[2]==’g') { l=strlen(exp); for(i=0;i<l-3; i++) arr[0]=exp[i+3]; arr[i]=”; infix_postfix(arr); e1=eval(postfix,x0); e2=eval(postfix,xn); e3=4*eval(postfix, x0+h); s=log(e1)+log(e2)+log(e3); for (i=3;i<=n-1;i+=2) s+=4*eval(postfix,x0+i*h)+2*eval(postfix, x0+(i-1)*h); } else { infix_postfix(exp); s=eval(postfix,x0)+eval(postfix,xn)+4*eval(postfix, x0+h);

triangle print

Write C Program to Print a Triangle #include<stdio.h> #include<conio.h> void main() { int n; printf("enter the lines :"); scanf("%d",&n); for(int i=1;i<=n/2;i++) { printf("*\n"); for(int j=1;j<=2*i;j++) printf("%d",j); printf("\n"); } if(n%2!=0) printf("\n*"); getch(); }

example of XOR list

#include <stdio.h> #include <conio.h> #include struct xnode { int data; unsigned long direction; }; struct xnode *add_data(int data, struct xnode* list); void walk_list(struct xnode *list); int main(void) { struct xnode *l2 = add_data(2, NULL); struct xnode *l1 = add_data(1, l2); struct xnode *l3 = add_data(3, l2); struct xnode *l4 = add_data(4, l3); printf("front -> back....\n"); walk_list(l1); printf("back -> front....\n"); walk_list(l4); return 0; } struct xnode *add_data(int data, struct xnode *list) { struct xnode *newxnode = malloc(sizeof(struct xnode)); assert(newxnode); newxnode->direction = (unsigned long)list; newxnode->data = data; if(list != NULL) list->direction ^= (unsigned long)newxnode; return newxnode; } void walk_list(struct xnode *list) { unsigned long prev = 0; while(list != NULL) { unsigned long next = prev ^ list->direction; printf("%d ", list->data); prev = (unsigned long)list; list = (struct xn

C Program to Find the GCD and LCM of Two Integers

This C Program calculates the GCD and LCM of two integers. Here GCD means Greatest Common Divisor. For two integers a and b, if there are any numbers d so that a / d and b / d doesn’t have any remainder, such a number is called a common divisor. Common divisors exist for any pair of integers a and b, since we know that 1 always divides any integer. We also know that common divisors can’t get too big since divisors can’t be any larger than the number they are dividing. Hence a common divisor d of a and b must have d <= a and d <= b. Here, LCM means Least Common Multiplies. For two integer a & b, to know if there are any smallest numbers d so that d / a and d / b doesn't have a remainder. such a number is called a Least Common Multiplier. Here is source code of the C program to calculate the GCD and LCM of two integers. The C program is successfully compiled and run on a Linux system. The program output is also shown below. 1.   /* 2.    * C program to find the GC

Find factorial of a given number using the recursive function method in C

Image
Write a program to find factorial of a given number using the recursive function method in C Hints: Factorial ! Example: 4! is shorthand for 4 x 3 x 2 x 1 The factorial function (symbol: ! ) means to multiply a series of descending natural numbers. Examples: 4! = 4 × 3 × 2 × 1 = 24 7! = 7 × 6 × 5 × 4 × 3 × 2 × 1 = 5040 1! = 1 Source Code: #include<stdio.h> //function declaration before the main int factorial(int n); //main function of the program int main() {     int n;     scanf("%d",&n);     //call function and print the final value at a time     printf("%d", factorial(n));     return 0; } int factorial(int n) {     if(n!=1)      return n*factorial(n-1); }