Posts

Showing posts with the label C programming

c programming for bubble sort

#include<stdio.h> void selection_sort(int arr[],int num); int main() { int arr[100],num,i; printf("Enter the size of array\n"); scanf("%d",&num); printf("\nEnter the elements in array one by one\n"); for(i=0;i<num;i++) { scanf("%d",&arr[i]); } selection_sort(arr,num); printf("The sorted array is\n"); for(i=0;i<num;i++) { printf("%d",arr[i]); } } void selection_sort(int arr[],int num) { int i,j,temp,min; for(i=0;i<=num-1;i++) { min=i; for(j=i+1;j<=num-1;j++) /*here in j=i+1 we are adding i to avoid error as we don't need to check for already sorted number as previously sorted number is kept in the same array */ { if(arr[j]<arr[i]) min=j; } temp=arr[i]; arr[i]=arr[min]; arr[min]=temp; } } OUTPUT- Enter the size of array 5 Enter the elements in array one by one 5 4 3 2 1 The sorted array is ...

c programming for bubble sort

#include<stdio.h> void bubble_sort(int [],int num); int main() { int arr[100],num,i; printf("\n Enter the size of the array\n"); scanf("%d",&num); printf("\n Enter the elements in the array one by one\n"); for(i=0;i<num;i++) { scanf("%d",&arr[i]); } bubble_sort(arr,num); //output for(i=0;i<num;i++) { printf("%d",arr[i]); } } void bubble_sort(int arr[],int num) { int i,j,temp; for(i=1;i<=num-1;i++)//i is for passes, if the size of the array is 4 we need to go for n-1 indices only { for(j=0;j<=num-i-1;j++)// here in j<=num-i-1 we are subtracting i as in each pass one element is sorted so to avoid comaring with that we are sutracting i means with pass { if(arr[j]>arr[j+1]) { temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } } } OUTPUT-  Enter the size of the array 4  Enter the elements in the array one by one 9 8 6 4 4689

c program to find prime factors of a number

#include<stdio.h> int prime_number(int num); int main() { int num; printf("\n Enter a number for prime factor"); scanf("%d",&num); prime_number(num); } int prime_number(int num) { int i=2; while(num!=1) { if(num%i==0) { while(num%i==0) { printf("%d\t",i); num=num/i; } } i++; } } OUT:  Enter a number for prime factor420 2 2 3 5 7 To understand go here  https://www.youtube.com/watch?v=Xg89vIL9aog

C program for prime or not

#include<stdio.h> int main() { int num,flag=0; printf("Enter a number\t"); scanf("%d",&num); //if the number is divisible by half of its number that is if n=6 then it is divided by 3 like n/3 and n is divided by 1,2,3 and if remainder is zero number is not prime for(int i=2;i<=num/2;i++) { if(num%i==0) { flag=1; break; } } if (flag==1) printf("Not Prime\n"); else printf("\n Prime\n"); } out: Enter a number 4 Not Prime

c programming for Euler’s Method

Euler’s Method Algorithm: Start Define function Get the values of x0, y0, h and xn *Here x0 and y0 are the initial conditions h is the interval xn is the required value n = (xn – x0)/h + 1 Start loop from i=1 to n y = y0 + h*f(x0,y0) x = x + h Print values of y0 and x0 Check if x < xn If yes, assign x0 = x and y0 = y If no, goto 9. End loop i Stop source code: #include<stdio.h> #include<math.h> float f(float x, float y) { float f; f=(y-x)/(y+x); return(f); } int main() { int i,n; float x0,xn,y0,h,x,y; printf("\n Enter the initial value of x and y i.e x0 and y0\n"); scanf("%f%f",&x0,&y0); printf("\nEnter the value of x for which we need to find the value of y\n"); scanf("%f",&xn); printf("\nEnter the value of h\n"); scanf("%f",&h); n=((xn-x0)/h)+1; for(i=1;i<=n;i++) { y =y0 + h*f(x0,y0); x= x + h; printf(" \n the valu...

c program for Runga-kutta method of 4th order

#include<stdio.h> #include<math.h> float f(float x,float y) { float f; f=((1/(x+y))); return (f); } int main() { float x0,y0,h,xn,k,k1,k2,k3,k4; printf("\n Enter the initial value that is x0 and y0\n"); scanf("%f%f",&x0,&y0); printf("\n Enter the value of h\n"); scanf("%f",&h); printf("\n Enter the value of x for which we need to find the value of y\n"); scanf("%f",&xn); do { k1=h*f(x0,y0); k2=h*f(x0+(h/2),y0+(k1/2)); k3=h*f(x0+(h/2),y0+(k2/2)); k4=h*f(x0+h,y0+k3); k=(k1+2*k2+2*k3+k4)/6; y0=y0+k; x0=x0+h; printf("\n%f %f",x0,y0); } while(x0<xn); printf("\n value of y=%f",y0); } output:  Enter the initial value that is x0 and y0 0 1  Enter the value of h .1  Enter the value of x for which we need to find the value of y .1 0.100000 1.091389  value of y=1.091389

c programming for raphson method

#include<stdio.h> #include<math.h> #define f(x) (3*x*x+2*x-9) #define df(x) (6*x+2) void main() { float x,a,c; printf("\n Enter the value initial value of x\n"); scanf("%f",&x); do { c=(-f(x)/df(x)); x=x+c; } while (fabs(c)>0.0001); printf("\n The value of the root is=%f",x); } output:- Enter the initial value of x The value of the root is =1.430501

c program for regula falsi method for root finding

#include<stdio.h> #include<math.h> //#include<process.h> float f(double x) { return ((x*x*x)-2*x-5); } void main() { float a,b,c,h; printf("\n Enter the lower limit a and upper limit b\n"); scanf("%f%f",&a,&b); if(f(a)*f(b)>0.0) printf("\n Enter the proper values\n"); do { c=b-((b-a)*f(b))/(f(b)-f(a)); if(f(a)*f(c) <0.0) b=c; else a=c; h=b-((b-a)*f(b))/(f(b)-f(a)); } while(fabs(h-c)>0.0001); printf("\n The value of the root is =%f",h); } output Enter the lower linit a and upper limit b a=2 b=3 the value of the root is =2.0945

condition for leap year with c programimg

if a year is divided by 400 then it is a leap year and if it is divided by 100 then it is not a leap year and if it is divided by 4 then it is a leap year else it is not a leap year some years are divisible by 4 but they are not leap year so we are dividing with both 400 and 4 ex-1700, 1800, 1900, 2100, 2200, 2300, 2500, 2600 This is because they are evenly divisible by 100 but not by 400. The following years are leap years: 1600, 2000, 2400 This is because they are evenly divisible by both 100 and 400. C programming code #include <stdio.h> int main() { int year; printf("Enter a year to check if it is a leap year\n"); scanf("%d", &year); if ( year%400 == 0) printf("%d is a leap year.\n", year); else if ( year%100 == 0) printf("%d is not a leap year.\n", year); else if ( year%4 == 0 ) printf("%d is a leap year.\n", year); else printf("%d is not a leap year.\n", year); ret

c program for bubble sort using functions

#include<stdio.h> void bubble_sort(int a[],int num); void display(int a[],int num); int main() { int a[100],num,i; printf("\nEnter the size of array\n"); scanf("%d",&num); printf("\nEnter the elements one by one in array\n"); for(i=0;i<num;i++) scanf("%d",&a[i]); bubble_sort(a,num); display(a,num); return 0; } void bubble_sort(int a[],int num) { int i,temp,j; printf("\nUnsorted data:"); for(i=0;i<num;i++) printf("%d\n",a[i]); { for(i=0;i<num;i++) { for(j=0;j<num-1;j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } }} void display(int a[],int num) { int i; printf("sorted elements are:"); for(i=0;i<num;i++) printf("%d\t",a[i]); }

c program to add two integers by passing arguments to function using pointers

#include<stdio.h> void sum(int *a,int *b,int *t);//fn declaration int main() { int num1,num2,total; printf("\n Enter first number"); scanf("%d",&num1); printf("Enter the second number"); scanf("%d",&num2); sum( &num1,&num2,&total);//fn calling printf("\n the sum total is %d",total); return 0; } //fn definition void sum(int *a,int *b,int *t) // fn { *t=*a+*b; }

hello world program using pointer

#include<stdio.h> int main() { char *ch = "Hello world"; printf("\n %s",ch); return 0; } output:Hello world

c program to determine the memory location of a variable using pointer

As you know pointer contains the memory location of another variable her in the source code given below num is variable and *pnum is pointer storing the variable of num source code: #include<stdio.h> int main() { int num,*pnum; pnum=&num; printf("Enter the number "); scanf("%d",&num); printf("\n The number that was entered is %d",num); printf("\n the address of memory location is %u"); return 0; } output: Enter the number 10  The number that was entered is 10  the address of memory location is 2147483612 %p string prints the argument as a memory address in hexadecimal form .%u will print in decimal.

program to find the size of various data types on your system

#include<stdio.h> int main() { printf("\n The size of short integer is :%d",sizeof(short int)); printf("\n The size of unsigned integer is :%d",sizeof(unsigned int)); printf("\n The size of signed integer is:%d",sizeof(signed int)); printf("\n The size of integer is:%d",sizeof(int)); printf("\n The size of long integer is:%d",sizeof(long int)); printf("\n The size of character is:%d",sizeof(char)); printf("\n The size of unsigned character is:%d",sizeof(unsigned char)); printf("\n The size of signed integer is:%d",sizeof(signed char)); printf("\n The size of floating point is :%d",sizeof(float)); printf("\n The size of double number is :%d",sizeof(double)); return 0; }

C program to impement circular queue using array

#include<stdio.h> //#include<conio.h> #define MAX 5 void insert(int); void delete(); void display(); int queue[MAX]; int front=-1,rear=-1; void insert(int num) { if((front==0 && rear==MAX-1)|| (front==rear-1)) printf("\n\n Queue is full"); else if(front==-1) { rear=0; front=0; } else if(rear==MAX-1) rear=0; else rear++; queue[rear]=num; } void delete() { int num; if (front==-1) printf("\n\n Queue is empty\n"); else { num=queue[front]; if(front==rear) { front=-1; rear=-1; } else if(front==MAX-1) front=0; front++; printf("\n\n item deleted %d",num); } } void display() { int i; if((front==-1)||(front==rear+1)) printf("\n\n Queue is empty\n"); else { printf("\n The front and rear %d %d",front,rear); printf("\n The Queue is"); for(i=front;i<=rear;i++) printf("\t %d",queue[i]); } } int main() { int ch,num; printf("\n main menu...

C Program to implement Selection sort using functions

Levels of difficulty: Hard / perform operation: Sorting Write a C program to sort given N elements using SELECTION sort method using functions : a) To find maximum of elements b) To swap two elements Selection sort is comparison based sorting technique, It finds the minimum value in list and swaps that value to the first position and so on. Selection sort is inefficient on larger data. source code :#include<stdio.h> void selection_sort(int a[],int num); void display(int a[],int num); int main() { int num,i,a[100]; printf("\nEnter the size of the array\n"); scanf("%d",&num); printf("\n Enter the elements one by one\n"); for(i=0;i<num;i++) { scanf("%d",&a[i]); } selection_sort(a,num); display(a,num); return 0; } void selection_sort(int a[],int n) { int i,j,t,m; for(i=0;i<n-1;i++) { m=i; for(j=i+1;j<n;j++)  {  if(a[j]<a[m])                         ...

For learning c and c++ programming

To learn c and c++ programming these youtube channels are best Engineering mentor Think aloud academy mycodeschool vikas chandra pandey for source codes http://scanftree.com/programs/c/c-program-to-implement-selection-sort/ http://www.sanfoundry.com/c-program-implement-selection-sort-method-using-functions/

c program to implement queue operations using array

#include<stdio.h> //#include<conio.h> #define MAX 10 int queue[MAX]; int front =-1,rear=-1; void insert(void); int delete_element(void); int peep(void); void display(void); main() {     int option,val;     do{         printf("\n *******MAIN MENU**********");         printf("\n 1.Insert an element");         printf("\n 2.Delete an element");         printf("\n 3.peep");         printf("\n 4.display");         printf("\n 5.Exit");         printf("\n********************************");     printf("\n Enter your option\n");     scanf("%d",&option);     switch(option)     {         case 1:         ...

c program for linked list

#include<stdio.h> #include<stdlib.h> //for malloc.h for allocating memory dynamically struct node { int data; struct node *next; }; struct node *start=NULL; struct node *create_ll(struct node *); struct node *display(struct node *); main() { int option; do{ printf("\n\n******MAIN MENU******"); printf("\n1.create linked list"); printf("\n2.Display the linked list"); printf("\n3..Enter your option\n\n"); scanf("%d",&option); switch(option) { case 1: start=create_ll(start); printf("\n LINKED LIST CREATED"); break; case 2: start=display(start); break; } }while(option!=3); return 0; } struct node *create_ll(struct node *start) { struct node *new_node; // we always declare pointer in strructure using keyword struct folwed by the function name int num; printf("\n Enter -1 to end"); printf("\n Enter the data:"); scanf("%d",&num); while(num!=-1) ...

c program to implement stack to perform push(insert) peep(display first element) and pop(delete).

#include<stdio.h> #define MAX 10 int st[MAX],top=-1; void push(int st[],int val); int pop(int st[]); int peep(int st[]); // peep display the topmost element from stack void display(int st[]); int main() { int val,option; do { printf("\n*****stack menu*****\n"); printf("\n1.push\n2.pop\n3.peep\n4.display\n5.exit\n"); printf("\nEnter your choice\n"); scanf("%d",&option); switch(option) { case 1: printf("\nEnter the number to be pushed on to the stack:"); scanf("%d",&val); push(st,val); //fn call break; case 2:val=pop(st); printf("\nThe value deleted from the stack is:%d",val); break; case 3: val=peep(st); printf("\n The value stored at the top of the stack is:%d",val); break; case 4:display(st); break; } }while (option!=5); return 0; } void push(int st[],int val) { if(top==MAX-1) { printf("\n Stack overflow"); } else { top++; st[top]=val; ...