Get Update on recent Technology & Programming

Showing posts with label C Programs. Show all posts
Showing posts with label C Programs. Show all posts

Wednesday, 15 November 2017

C Program to convert a binary number to a decimal number

C Program to convert a binary number to a decimal number







#include<stdio.h>

int main(void)

{

 int n,nsave,rem,d,j=1,dec=0;

 printf("Enter the number in binary : ");

 scanf("%d",&n);

 nsave=n;

 while(n>0)

 {

  rem=n%10; /*taking last digit of n*/

  d=rem*j;

  dec+=d;

  j*=2;

  n/=10;  /*skipping last digit of n*/

 }

 printf("Binary number = %d, Decimal number = %d\n",nsave,dec);

 return 0;

}


The given code is a C program that converts a binary number entered by the user into its decimal equivalent. Here's a breakdown of the code:


The program includes the necessary header file stdio.h and defines the main function. It declares several variables:

  • n represents the input binary number.
  • nsave stores a copy of the original input for printing later.
  • rem is used to store the remainder when dividing the number by 10.
  • d stores the calculated value of each digit.
  • j is used to calculate the power of 2 for each digit.
  • dec holds the final decimal value.

The program prompts the user to enter a binary number and reads it using the scanf function. The original value of n is saved in nsave.




The program uses a while loop to convert the binary number to decimal. In each iteration, it performs the following steps:

  • Calculates the remainder (rem) when dividing n by 10 to obtain the last digit.
  • Multiplies the remainder by j to calculate the decimal value of the current digit.
  • Adds the calculated value (d) to the dec variable.
  • Multiplies j by 2 to increase its power for the next digit.
  • Divides n by 10 to remove the last digit.

Once the loop finishes, the decimal value is stored in dec.


Finally, the program displays the original binary number (nsave) and the converted decimal number (dec) using the printf function. It then returns 0 to indicate successful program execution.

Overall, this code takes a binary number as input and converts it into its decimal equivalent.

C Program to find the factorial of any number

C Program to find the factorial of any number






#include<stdio.h>

int main(void)

{

 int n,num;

 long fact=1;

 printf("Enter a number : ");

 scanf("%d",&n);

 num=n;

 if(n<0)

  printf("No factorial of negative number\n");

 else

 {

  while(n>1)

  {

   fact*=n;

   n--;

  }

  printf("Factorial of %d=%ld\n",num,fact);

 }

 return 0;

}




C Program to find the product of digits of any number

C Program to find the product of digits of any number






#include<stdio.h>

int main(void)

{

 int n,prod=1,rem;

 printf("Enter a number : ");

 scanf("%d",&n);

 while(n>0)

 {

  rem = n%10; /*taking last digit of n*/

  prod*=rem;

  n/=10; /*skipping last digit of n*/

 }

 printf("Product of digits = %d\n",prod);

 return 0;

}





C Program to print the sum of digits of any number

C Program to print the sum of digits of any number





#include<stdio.h>

int main(void)

{

 int n,sum=0,rem;

 printf("Enter a number : ");

 scanf("%d",&n);

 while(n>0)

 {

  rem = n%10; /*taking last digit of n*/

  sum+=rem;

  n/=10;  /*skipping last digit of n*/

 }

 printf("Sum of digits=%d\n",sum);

 return 0;

}




C Program to print numbers in reverse order with a difference of 2

C Program to print numbers in reverse order with a difference of 2




#include<stdio.h>

int main(void)

{

 int k=10;

 while(k>=2)

 {

  printf("%d\t",k);

  k=k-2;

 }

 printf("\n");

 return 0;

}




C Program to find whether a year is leap or not

C Program to find whether a year is leap or not







#include<stdio.h>

int main(void)

{

 int year;

 printf("Enter year : ");

 scanf("%d",&year);



 if(year%100 != 0)

 {

  if(year%4 == 0)

   printf("Leap year\n");

  else

   printf("Not leap\n");  

 }

 else

 { 

  if(year%400 == 0)

   printf("Leap year\n");

  else

   printf("Not leap\n");

 }

 return 0;

}



C Program to find biggest number from three given numbers

C Program to find biggest number from three given numbers 







#include<stdio.h>

int main(void)

{

 int a,b,c,big;

 printf("Enter three numbers : ");

 scanf("%d%d%d", &a, &b, &c);

 if(a>b)

 {

  if(a>c)

   big=a;

  else

   big=c;

 }

 else

 {

  if(b>c)

   big=b;

  else

   big=c;

 }

 printf("Biggest number is %d\n",big);

 return 0;

}/*End of main()*/



The given code is a C program that prompts the user to enter three numbers and then determines the biggest number among them. Here's a breakdown of the code:

The program includes the necessary header file stdio.h, which provides input and output functions. The main function is defined as int (integer) and takes no arguments (void).

The variables a, b, and c are declared as integers, representing the three numbers entered by the user. The variable big will store the biggest number.

The printf function displays a prompt asking the user to enter three numbers.

The scanf function reads the three numbers entered by the user and stores them in the variables a, b, and c.


The program then checks the values of a, b, and c to determine the biggest number. It uses nested if statements to compare the numbers and assign the largest one to the variable big.


Finally, the program prints the value of the biggest number using printf, and returns 0 to indicate successful program execution.

Overall, this code determines the largest number among three inputs provided by the user and displays the result.



C Program to find quotient and remainder

C Program to find quotient and remainder






#include<stdio.h>

int main(void)

{

 int x,y,quo,rem;

 printf("Enter two numbers : ");

 scanf("%d%d",&x,&y);

 if(y)  /*if y is non-zero*/

 {

  quo=x/y; 

  rem=x%y;

  printf("Quotient=%d, Remainder=%d\n",quo,rem);

 } 

 else

  printf("Divide by zero error\n");

 return 0;

}




C Program to print a message if negative number is entered

C Program to print a message if negative number is entered




#include<stdio.h>

int main(void)

{

 int num;

 printf("Enter a number : ");

 scanf("%d",&num);

 if(num<0)

 {

  printf("Number entered is negative\n");

  num=-num;  

 }

 printf("Value of num is : %d\n", num);

 return 0;

}



C Program to print whether a number is even or odd

C Program to print whether a number is even or odd





#include<stdio.h>

int main(void)

{

 int num;

 printf("Enter a number : ");

 scanf("%d",&num);

 if(num%2 == 0)  /*test for even */

  printf("Number is even\n");

 else

 {

  printf("Number is odd\n");

  num*=2;

  printf("Now the number is %d\n",num); 

 }

 return 0;

}



C Program to print the largest Of Two Number

C Program to print the largest Of Two Number





#include<stdio.h>

int main(void)

{

 int a,b;

 printf("Enter two numbers : ");

 scanf("%d%d",&a,&b);

 if(a>b)

  printf("Bigger number=%d\n",a);

 else

  printf("Bigger number=%d\n",b);

 return 0;

}



Tuesday, 14 November 2017

C Program to read records from a file and calculate grade of each student and display it

Program to read records from a file and calculate grade of each student and display it 

grade= A if marks>=80

= B if marks>=60 and < 80


= C if marks<60





#include<stdio.h>

#include<stdlib.h>

int main(void)

{

 struct record

 {

  char name[20];

  int roll;

  int marks;

 }student;

 FILE *fp;

 fp = fopen("stu","rb");/*opened in read mode */

 if(fp==NULL)

 {

  printf("Error in opening file\n");

  exit(1);

 }

 printf("\nNAME\t\tMARKS\t\tGRADE\n\n");

 while(fread(&student,sizeof(student),1,fp)==1)

 {

  printf("%s\t\t",student.name);

  printf("%4d\t\t",student.marks);

  if(student.marks>=80)

   printf("A\n");

  else if(student.marks>=60)

   printf("B\n");

  else

   printf("C\n");

 }

 fclose(fp);

 return 0;

}




C Program to Program to append records to a file

C Program to Program to append records to a file





#include<stdio.h>

#include<stdlib.h>

int main(void)

{

 struct record

 {

  char name[20];

  int roll;

  int marks;

 }student;

 FILE *fp;

 int choice=1;

 fp=fopen("stu","ab");/*opened in append mode*/

 if(fp==NULL)

 {

  printf("Error in opening file\n");

  exit(1);

 }

 while(choice==1)

 {

  printf("Enter name : ");

  scanf("%s",student.name);

  printf("Enter roll no : ");

  scanf("%d",&student.roll);

  printf("Enter marks : ");

  scanf("%d",&student.marks);

  fwrite(&student,sizeof(student),1,fp);

  printf("Want to enter more ?(1 for yes / 0 for no) : ");

  scanf("%d",&choice);

 }

 fclose(fp);

 return 0;

}




C Program to Copy a file to another file

C Program to Copy a file to another file





#include<stdio.h>

#include<stdlib.h>

int main(void)

{

 FILE *sptr, *dptr;

 char ch;

 if((sptr=fopen("source.txt","r"))==NULL)

 {

  printf("Error in opening source file\n");

  exit(1);

 }

 if((dptr=fopen("destination.txt","w"))==NULL)

 {

  printf("Error in opening destination file\n");

  exit(1);

 }

 while((ch=fgetc(sptr))!=EOF)

  fputc(ch,dptr);

 fclose(sptr);

 fclose(dptr);

 return 0;

}




Friday, 10 November 2017

C Program to find Prime numbers using sieve Algorithm



C Program to find Prime numbers using sieve Algorithm





#include<stdio.h>

#define MAX 10000

int main(void)

{

	int p,i,n,a[MAX]={0};

		

	printf("Enter n : ");

	scanf("%d",&n);

    

	p=2;

	while(p*p <= n) 

	{

		for(i=2; i*p<=n; i++) 	/*Cross out all multiples of p*/

			a[i*p]=1;

				

		for(i=p+1; i<=n; i++) /*Find next uncrossed*/

			if(a[i]==0)

			{

				p=i;

				break;

			}

	}

	/*Print all uncrossed integers*/

	for(i=2; i<=n; i++)

		if(a[i]==0)

			printf("%d ",i);

	return 0;

}




C Program of merging two sorted arrays into a third sorted array



C Program of merging two sorted arrays into a third sorted array





#include<stdio.h>

#define MAX 100

void merge(int arr1[],int arr2[],int arr3[],int n1,int n2);

int main(void)

{

	int arr1[MAX],arr2[MAX],arr3[2*MAX],n1,n2,i;

	printf("Enter the number of elements in array 1 : ");

	scanf("%d",&n1);

	printf("Enter all the elements in sorted order :\n");

	for(i=0; i<n1; i++)

	{

		printf("Enter element %d : ",i+1);

		scanf("%d",&arr1[i]);

	}

	printf("Enter the number of elements in array 2 : ");

	scanf("%d",&n2);

	printf("Enter all the elements in sorted order :\n");

	for(i=0; i<n2; i++)

	{

		printf("Enter element %d : ",i+1);

		scanf("%d",&arr2[i]);

	}

	merge(arr1,arr2,arr3,n1,n2);

	printf("\nMerged list : ");

	for(i=0; i<n1+n2; i++)

		printf("%d ",arr3[i]);

	printf("\n");

	return 0;

}

void merge(int arr1[],int arr2[],int arr3[],int n1,int n2)

{

	int i,j,k;

	i=0;  /*Index for first array*/

	j=0;  /*Index for second array*/

	k=0;  /*Index for merged array*/

	while((i<=n1-1)&&(j<=n2-1))

	{

		if(arr1[i]<arr2[j])

			arr3[k++]=arr1[i++];

		else

			arr3[k++]=arr2[j++];

	}

	while(i<=n1-1) /*Put remaining elements of arr1 into arr3*/

		arr3[k++]=arr1[i++];

	while(j<=n2-1) 	/*Put remaining elements of arr2 into arr3*/

		arr3[k++]=arr2[j++];

}/*End of merge()*/





C Program of sorting using insertion sort


C Program of sorting using insertion sort




#include<stdio.h>

#define MAX 100

int main(void)

{

	int arr[MAX],i,j,k,n;

	printf("Enter the number of elements : ");

	scanf("%d",&n);

	for(i=0; i<n; i++)

	{

		printf("Enter element %d : ",i+1);

		scanf("%d", &arr[i]);

	}

	/*Insertion sort*/

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

	{

		k=arr[i]; /*k is to be inserted at proper place*/

		for(j=i-1; j>=0 && k<arr[j]; j--)

			arr[j+1]=arr[j];

		arr[j+1]=k;

	}

	printf("Sorted list is :\n");

	for(i=0; i<n; i++)

		printf("%d ",arr[i]);

	printf("\n");

	return 0;

}/*End of main()*/





C Program to insert an item in a sorted array at the proper place by shifting other elements to the right


C Program to insert an item in a sorted array at the proper place by shifting other elements to the right




#include<stdio.h>

#define SIZE 10

int main(void)

{

	int arr[SIZE];

	int i,item;

	printf("Enter elements of the array(in sorted order) : \n");

	for(i=0; i<SIZE-1; i++) /*rightmost space in the array should be empty*/

			scanf("%d",&arr[i] );

	printf("Enter the item to be inserted : ");

	scanf("%d",&item);

	for(i=SIZE-2; item<arr[i] && i>=0; i--)

		arr[i+1]=arr[i];    /*Shift elements to the right*/

	arr[i+1]=item;	    /*Insert item at the proper place*/



	for(i=0; i<SIZE; i++)

		printf("%d  ",arr[i]);

	printf("\n");

	return 0;

}





C Program to insert an item in an array at a specified index by moving other elements to the right


C Program to insert an item in an array at a specified index by moving other elements to the right




#include<stdio.h>

#define SIZE 10

int main(void)

{

	int arr[SIZE];

	int i,item,index;

	printf("Enter elements of the array : \n");

	for(i=0; i<SIZE-1; i++) /*rightmost space in the array should be empty*/

			scanf("%d",&arr[i]);

	printf("Enter the item to be inserted : ");

	scanf("%d",&item);

	printf("Enter the index where item is to be inserted : ");

	scanf("%d",&index);



	for(i=SIZE-2; i>=index; i--)

		arr[i+1]=arr[i];    /*Shift elements to the right*/

	arr[i+1]=item;	    /*Insert item at the proper place*/



	for(i=0; i<SIZE; i++)

		printf("%d  ",arr[i]);

	printf("\n");

	return 0;

}




Bubble sort In c





#include<stdio.h>

#define MAX 100

int main(void)

{

	int arr[MAX],i,j,temp,n,xchanges;

	printf("Enter the number of elements : ");

	scanf("%d",&n);

	for(i=0; i<n; i++)

	{

		printf("Enter element %d : ",i+1);

		scanf("%d",&arr[i]);

	}

	/*Bubble sort*/

	for(i=0; i<n-1 ;i++)

	{

		xchanges = 0;

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

		{

			if(arr[j] > arr[j+1])

			{

				temp = arr[j];

				arr[j] = arr[j+1];

				arr[j+1] = temp;

				xchanges++;

			}

		}

		if(xchanges==0) /*If list is sorted*/

			break;

	}

	printf("Sorted list is :\n");

	for(i=0; i<n; i++)

		printf("%d ",arr[i]);

	printf("\n");

	return 0;

}







Hey, we've just launched a new custom color Blogger template. You'll like it -
Join Our Newsletter