Posts

Showing posts with the label C

Program to find current date and time

#include <time.h> #include <stdio.h>   main()   {     struct tm *ptr;     time_t lt;     lt = time(NULL);     ptr = localtime(&lt);     printf(asctime(ptr));     getch();     //return 0;   }

Array Implementation of a Stack in C Programming

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

Program to multiply a 3x5 array by a scalar method

#include<stdio.h> void scalar_multiply(matrix,scalar) int matrix [3][5]; int scalar; {     int row, column;     for(row=0;row<3;++row)     for(column=0;column<5;++column)     {        matrix[row][column]*=scalar;     } } //second function void display_matrix(matrix) int matrix[3][5]; {     int row,column;     for(row=0;row<3;++row)     for(column=0;column<5;++column)        {        printf("%5d",matrix[row][column]);        }        printf("\n"); } //main starts here main() {       static int sample_matrix[3][5]=       {              {7,16,55,13,12},              {12,10,52,0,7},              {-2,1,2,4,9}       };       printf("Original matrix :\n");       display_matrix(sample_matrix);       scalar_multiply(sample_matrix,2);       printf("\nMultiplied by 2 :\n");       display_matrix(sample_matrix);       scalar_multiply(sample_matrix,-1);       pri

Program to find the primes upto entered number

#include<stdio.h> main() {       unsigned index, pcnt=0;       int max;       printf("Enter integer N upto which primes are to be find :");       scanf("%u",&max);       if(max>0)       {          printf("Prim nos. upto %u.\n",max);          {          for(index=1;index<=max;index++)             if(isprime(index))      //calling the function             {               printf("%u\n",index);               pcnt++;             }          }       }       printf("\nNumber of primes = %u",pcnt);       getch(); } //function defining here  (main is giving 1 to 10 numbers //"if max=10 in main" for the variable num) int isprime(unsigned num) {     unsigned index,sq;       if((num==1)||(num==2)||(num==3))       {       return 1;     //it returns true i.e.,num=1 or num=2 or num=3       }     else       sq=sqrt((double)num);       for(index=2;index<=sq;

Program to demonstrate the use of getchar() at the place of scanf

#include<stdio.h> #include<conio.h> main() {       int c;       printf("\nType a character : ");       c=getchar();       printf("\nTyped character is %c",c);       printf("\nTyped character is %d",c);       getch(); }

Declared a variable in c without defining it

extern int a;   Please note that uninitialized extern variables are example of declaration of variables.

Program to draw a rectangle

#include<graphics.h> #include<conio.h> void main() {     int gd=DETECT, gm;     initgraph(&gd, &gm, "c:\\turboc3\\bgi " );     rectangle(200,50,350,150);      getch();     closegraph(); }

What is Compiler?

Compiler is Application Program which converts the source program code (user code) to target code (machine understandable code). It also creates the executable files with .exe extensions. Some of the C++ Compilers are Borland C++, Turbo C++, Microsoft C++, Developer C++ etc. For any program compilation in C++ we should have to write the file name with the extension .CPP. In the process of Compilation if any error will occur the compilation process will automatically be stop and after 100% correctness, compiler will create .exe file. 

Write a C program without using any semicolon which outputs Hello Word

There are many ways to do this:-   Solution: 1   void main(){     if(printf("Hello world")){     } }   Solution: 2   void main(){     while(!printf("Hello world")){     } }

Program to print the hexa and octal number

#include<stdio.h> #include<conio.h> main() {       int x=735;       short int y=40;       unsigned int u=0xf179;       float f=49.759;       double d=99.4321;       char c='k';              printf("Integer printing");       printf("\nInteger : %d, Octal : %o, Hexadecimal : %x",x,x,x);       printf("\nInteger : %d, Octal : %o, Hexadecimal : %x",y,y,y);       printf("\nUnsigned");       printf("\nInteger : %d, Octal : %o, Hexadecimal : %x",u,u,u);       printf("\nFloating point printing");       printf("\n%f   %e",f,f);       printf("\n%.2f    %.2e",f,f);       printf("\nCharacter Printing");       printf("\n%c",c);       getch(); }

Subtract two integer numbers without using subtraction operator

void main(){     int a,b,d;     scanf("%d%d",&a,&b);     d=a+~b+1;     printf("%d",d);     getch(); }

Program to input 10 numbers and print with serial number

#include<stdio.h> main() { int i,response[11]; //start taking values     for(i=1;i<=10;++i)        {        scanf("%d",&response[i]);        } //start printing values     printf("\nRating    Number you inputed");     printf("\n---------------------------\n");     for(i=1;i<=10;++i)        {        printf("%d\t\t",i);        printf("%d\n",response[i]);        } getch();      }

File operation functions in C

There are following functions supported by C Programming:-   fopen(): Creates a new file for use and opens a new existing file for use. fclose(): Closes a file which has been opened for use. getc():    Reads a character from a file putc():    Writes a character to a file fprintf(): Writes a set of data values to a file fscanf(): Reads a set of data values from a file getw(): Reads a integer from a file putw():    Writes an integer to the file fseek(): Sets the position to a desired point in the file ftell(): Gives the current position in the file rewind(): Sets the position to the begining of the fi

Program to print the link list value

# include <stdio.h> # include <stdlib.h>    struct node    {        int data;        struct node *link;    };    struct node *insert(struct node *p, int n){       struct node *temp;       if(p==NULL){           p=(struct node *)malloc(sizeof(struct node));          if(p==NULL) {              printf("Error\n");              exit(0);          }          p-> data = n;          p-> link = p;       } else {          temp = p;          while (temp-> link != p)             temp = temp-> link;             temp-> link = (struct node *)malloc(sizeof(struct node));             if(temp -> link == NULL){                printf("Error\n");                exit(0);             }             temp = temp-> link;             temp-> data = n;             temp-> link = p;           }           return (p);    }    void printlist ( struct node *p )    {       struct node *temp;

Pointer Using Array in C Programming

#include<stdio.h> #include<conio.h> int array_sum(array,n) int array[]; int n; {     int sum=0,*ptr;     int *array_end=array+n;     ptr=array;     for(;ptr<array_end;++ptr)    //or for(ptr=array;ptr<array_end;++ptr)     sum+=*ptr;     return (sum); } main() {       static int values[10]={3, 7, -9, 3, 6, -1, 7, 9, 1, -5};       printf("The sum is %d\n",array_sum(values,10));       getch(); }

Program to find the sin cos tan value using math dot h

#include <math.h> #include <stdio.h> main()   {     double val = 30;     do {       printf("Cosine of %.f is %.2f\n", val, sin(val));       val += 5;     } while(val<=100);     //return 0;     getch();   }

Program to use fprintf() and getc()

#include<stdio.h>  main()  {  int n,i; printf("\nEnter to print table (maximum) range :"); scanf("%d",&n); FILE *table; //will print the table in file named table.txt table=fopen("table.txt","w"); for(i=0;i<=n;i++) fprintf(table,"%d\n",i); fclose(table); //will print the table on screen table=fopen("table.txt","r"); char c; while((c=getc(table))!=EOF) printf("%c",c); fclose(table); getch(); }

What is Inheritance?

Usually we use to say that his father is criminal so he will also be a criminal. That means son is inheriting his father's mottos, not all but he will some. In Class, there could be many sub-classes and all sub-classes inherit the properties of main class. So, Inheritance is the terminology to inherit some (not all) properties of its main class.

Create new file to write and then open it for read

#include<stdio.h> main() {       FILE *f1;       char c;       printf("\nData input.\n");       f1=fopen("Input","w");       while((c=getchar())!=EOF)       putc(c,f1);       fclose(f1);       printf("\nData output.\n");       f1=fopen("Input","r");       while((c=getc(f1))!=EOF)       printf("%c",c);       fclose(f1);       getch(); }

Program to print the quadratic equation

#include<stdio.h> #include<conio.h> #include<math.h> main() { float a,b,c,d,k,x,y; clrscr(); printf("Enetr the values of a,b,c=>"); scanf("%f %f %f",&a,&b,&c); d=((b*b)-(4*a*c)); k=sqrt(d); x= (-b+k)/(2*a); y= (-b-k)/(2*a); printf("The quadratic equation is x=%f and y=%f",x,y); getch(); }

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

Lambda two tables and three tables inner join code samples