Get Update on recent Technology & Programming

Wednesday, 15 November 2017

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;

}




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