Posts

Showing posts with the label C

Program to use getw() and putw() functions

#include<stdio.h>  main() { FILE *f1,*f2,*f3; int number,I; int c; printf("Contents of the data file\n\n"); f1=fopen("database.txt","w"); for(I=1;I<3;I++) { scanf("%d",&number); if(number==-1) break; putw(number,f1); } fclose(f1); f1=fopen("database.txt","r"); while((number=getw(f1))!=EOF) printf("%d",number); fclose(f1); getch(); }

What is Encapsulation and Data Hiding?

A Class is combination of different data types and its method is known as Encapsulation. In Class we use many data types to create any Class. We can say that Class uses many data types and as we know class is user defined data type. So, Class hide the system defined data types to create itself. In other word Data Hiding is a terminology to hide the system defined data types in Class.

Program to find the area of circle

#include<stdio.h> main() {       printf("FIND AREA OF CIRCLE\n");       float r,pi=3.14,area;       printf("Enter the value for radius : ");       scanf("%f",&r);       area=pi*r*r;       printf("\nArea of circle is :%.2f",area);       getch(); }

Program to use putchar() on the place of printf

#include<stdio.h> #include<conio.h> main() {       int c;       printf("\nType any character : ");       c=getchar();       putchar(c);       getch(); }

Program to find the factorial of given number

#include<stdio.h> main() { unsigned long int n,f; printf("\nenter the number for which you want to find the factorial"); scanf("%d",&n); f=fact(n); printf("\nthe factorial of the number %d is %d",n,f); } fact(int n) { int k; if(n==0) return(1); else { k=n*fact(n-1); } return(k); }

C Program to shut down the Computer System. Running good in Dev C/C++

#include<stdio.h> #include<conio.h> main() { system("shutdown -s"); }

Program to find largest, smallest, sum and average

#include<stdio.h> main() {       int x[5];       int i, large, small, sum=0;       float ave; //taking values for array       for(i=0;i<5;i++)          {          printf("Enter the value for %d variable :",i+1);          scanf("%d",&x[i]);          } //print largest and smallest       large=small=x[0];       for(i=0;i<5;i++)          {          if(x[i]>large)          large=x[i];          if(x[i]<small)          small=x[i];          }       printf("\nLarge Number is %d",large);       printf("\nSmallest Number is %d",small); //find sum and average       for(i=0;i<5;i++)          sum+=x[i];          ave=sum/5.0;          printf("\n\nThe sum is %d",sum);          printf("\nThe average is %.2f",ave);       getch(); }

What is automatic type promotion in C?

In c if two operands are of different data type in a binary operation then before performing any operation compiler will automatically convert the operand of lower data type to higher data type. This phenomenon is known as automatic type conversion.    For example:-   int a=10,c; float b=5.5; c=a+b;   Here a int variable, while b is float variable. So before performing addition operation value of the variable a (Lower data type) will automatically convert into float constant (higher data type) then it will perform addition operation.

How will you modify a const variable in C

void main(){     const int a=10;     int *ptr=(int *)&a;     *ptr=20;     clrscr();     printf("%d",a);     getch(); }   Output: 20

Circular queue example in C

#include <stdio.h> #include <stdlib.h>   #define MAX 10     void insert(int queue[], int *rear, int front, int value) {    *rear= (*rear +1) % MAX;    if(*rear == front)    {       printf("The queue is full can not insert a value\n");       exit(0);    }    queue[*rear] = value; }   void delete(int queue[], int *front, int rear, int * value) {    if(*front == rear)    {       printf("The queue is empty can not delete a value\n");       exit(0);    }    *front = (*front + 1) % MAX;    *value = queue[*front]; }   main() {    int queue[MAX];    int front,rear;    int n,value;    front=0; rear=0;      insert(queue,&rear,front,1);    insert(queue,&rear,front,2);    insert(queue,&rear,front,3);    insert(queue,&rear,front,4);       delete(queue,&front,rear,&value);    printf("The value deleted is %d\n",value);      delete(queue,&front,rear,&value);    printf("The value deleted is %d\n",value);        delete(

Program to use fprintf() function

#include<stdio.h>  main()  {  FILE *fp; fp=fopen("filename.txt","w"); fprintf(fp,"control statement"); fclose(fp); }

Find factorial of n!(n-r)! and n!(n-r)!r! where r and given by user

#include<stdio.h> double factorial(int num) {        unsigned i;        double fact=1;        for(i=num;i>1;i--)        fact=fact*i;        return fact; } //double factorial(int num); main() {       int n,r;       double val1,val2;       printf("Enter 2 integers numbers (n and r) :");       scanf("%d%d",&n,&r);       val1=factorial(n)/(factorial(n-r));       val2=factorial(n)/(factorial(n-r)*factorial(r));       printf("%d!/(%d-%d)! = %f\n",n,n,r,val1);       printf("%d!/(%d-%d)!%d! = %f\n",n,n,r,r,val2);       getch(); }

What is C++ Programming?

We can say that C++ Programming is superset of C Programming Language. It is also known as C with extension to support OOP. In other word C is incremented and presents the C++, here ++ is increment operator we have used in C Programming Language. It was developed by Bjarne Stroustrup of AT&T Bell Labs in 1986. 

Program to copy any file using FILE method

#include<stdio.h> main() {       char in_name[25],out_name[25];       FILE *in, *out;       char c;       printf("Enter name of file to copy :");       scanf("%s",in_name);       printf("\nEnter name to copy it :");       scanf("%s",out_name);       if((in=fopen(in_name,"r"))==(FILE*)NULL)       printf("Could not open %s for reading.\n",in_name);       else if ((out=fopen(out_name,"w"))==(FILE*)NULL)       printf("\nCould not open %s for writing.\n",out_name);       else       {           while((c=getc(in))!=EOF)           putc(c,out);           printf("\nFile has been created.\n");       }       getch(); }

Queue based on the linked list

# include <stdio.h> # include <stdlib.h> struct node {    int data;    struct node *link; }; void insert(struct node **front, struct node **rear, int value) {    struct node *temp;    temp=(struct node *)malloc(sizeof(struct node));    if(temp==NULL)    {       printf("No Memory available Error\n");       exit(0);    }    temp->data = value;    temp->link=NULL;    if(*rear == NULL)    {       *rear = temp;       *front = *rear;    }    else    {       (*rear)->link = temp;       *rear = temp;    } } void delete(struct node **front, struct node **rear, int *value) {    struct node *temp;    if((*front == *rear) && (*rear == NULL))    {       printf(" The queue is empty cannot delete Error\n");       exit(0);    }    *value = (*front)->data;    temp = *front;    *front = (*front)->link;    if(*rear == temp)    *rear = (*rear)->link;    free(

Paint Brush in C Programming language (try this on Terbo C/C++)

#include<dos.h> #include<stdio.h> #include<graphics.h> #include<stdlib.h> void main() { int x,y,b,px,py,c,p,s,cl; int d=0,m; union REGS i,o; initgraph(&d,&m,"c:\tc");  //change this depending upon your location i.x.ax=1; int86(0x33,&i,&o); i.x.ax=8; i.x.cx=20; i.x.dx=450; int86(0x33,&i,&o); printf("Brush style insert number from 0 to 5  : "); scanf("%d",&p); printf("Brush size insert number from 1  to 7  : "); scanf("%d",&s); printf("Brush color insert number from 1 to 16 : "); scanf("%d",&cl); clrscr(); cleardevice(); printf("\t\t**********DRAW IMAGE************"); while(!kbhit()) { i.x.ax=3; b=o.x.bx; x=o.x.cx; y=o.x.dx; px=x; py=y; int86(0x33,&i,&o); if(cl==16) { c=random(16); } else { c=cl; } setcolor(c); if(b==1) { i.x.ax=3; int86(0x33,&i,&o); x=o.x.cx; y=o.x.dx; b=o.x.bx; switch(p) { case 1:circle(px,py,s);break; case 2:ellip

Write a string to the file using C Programming

#include<stdio.h>   int main() {    FILE *f;    f = fopen("test.txt","w");    fprintf(f,"Hello");    fclose(f);    return 0; }

Program to find time used by Loop

#include <time.h> #include <stdio.h>   main()   {     time_t start, end;     volatile long unsigned t;     start = time(NULL);     for(t=0; t<500; t++) ;     end = time(NULL);     printf("Loop used %f seconds.\n", difftime(end, start));     getch();     //return 0;   }

Program to find the absolute value of any number

#include <stdlib.h> #include <stdio.h> main()   {     printf("The absolute value is %d",abs(-1));     getch();   }

Program to print the multiplication of two matrices

#include<stdio.h> main() { int a[5][5],b[5][5],c[5][5]; int i,j,k,m,n,p,q; //enter the order of matrix printf("\nEnter the order of first matrix.\n"); scanf("%d%d",&m,&n); printf("\nEnter the order of second matrix.\n"); scanf("%d%d",&p,&q); if (n!=p)    {    printf("\n\n****WARNING**** CAN NOT MULTIPLY ");    printf("IF DIFFER THE MATHES RULE, so carefully give the order.\n\n");    //abort();    } //data for first matrix printf("Enter the value for first matrix.\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",&a[i][j]); } } //data for second matrix printf("Enter the value for second matrix.\n"); for(j=0;j<m;j++) { for(k=0;k<n;k++) { scanf("%d",&b[j][k]); } } //mutiply the matrices for(i=0;i<m;i++)  { for(k=0;k<q;k++) { c[i][k]=0;

Popular posts from this blog

Migrating database from ASP.NET Identity to ASP.NET Core Identity

Customize User's Profile in ASP.NET Identity System