Showing posts with the label CPP

What is Object?

Object is an instance. For example: mouse, printer, menu, iron, points, circuit, angles etc.

Additing Two Matrices in C Programming

#include<stdio.h> main() { int a[3][3],b[3][3],c[3][3]; int i,j,m=3,n=3; //data for first matrix printf("Enter a 3x3 matrix (1)\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",&a[i][j]); } } //data for second matrix printf("Enter a 3x3 matrix (2)\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",&b[i][j]); } } //add the matrices for(i=0;i<m;i++)    {    for(j=0;j<n;j++)       {       c[i][j]=a[i][j]+b[i][j];       }    } //printing the matrix. printf("The product matrix is :\n"); for(i=0;i<m;i++)  {   printf("\n"); for(j=0;j<n;j++) { printf("%d\t",c[i][j]); }  } getch(); }

Function to copy one string to another without using strcpy() function

#include <stdio.h>  #include <conio.h> int main()  { void copyString(const char *src, char *dest);  char str1[100];  char str2[100];  printf("Enter the string: ");  gets(str1);  copyString(str1, str2);  printf("Copied string: %s\n", str2);  getch();  } 
void copyString(const char *src, char *dest)  {  while (*dest++ = *src++);  }

C program to restart computer system

main ()
system("shutdown -r -t 00");  
//this will shutdown your computer and restart also.
//system("shutdown -s -t 10"); 
//this will shutdown your computer within 10 sec.

Print local and UTC (essentially Greenwich mean) time using C

#include <time.h> #include <stdio.h>
main()   {     struct tm *local, *gm;     time_t t;
    t = time(NULL);     local = localtime(&t);     printf("Local time and date: %s\n", asctime(local));     gm = gmtime(&t);     printf("Coordinated Universal Time and date: %s", asctime(gm));
    getch();     //return 0;   }

Program to show the graphics effect

#include<graphics.h> #include<conio.h> #include<stdlib.h>
void main() {     int gd,gm;     gd=DETECT;
    initgraph(&gd, &gm, "");     setcolor(3);     setfillstyle(SOLID_FILL,RED);     bar(50, 50, 590, 430);     setfillstyle(1, 14);     bar(100, 100, 540, 380);
    while(!kbhit())     {         putpixel(random(439)+101,  random(279)+101,random(16));         setcolor(random(16));         circle(320,240,random(100));     }     getch();     closegraph(); }

How to reverse a string using a recursive function, with swapping

#include<stdio.h> #include<conio.h> char a1[50];  //GLOABAL VAR. void reverse(int); void main() { int count=0; printf("enter the string :"); scanf("%s",a1); for(int i=0;a1[i]!='\0';i++) count++; reverse(count); getch(); }
void reverse(int count1) { char temp; static int i=0;   if(i<=count1/2)     {    temp=a1[i];     a1[i]=a1[count1-1];     a1[count1-1]=temp;     i++;    reverse(--count1);     }    else     printf("\nthe reversed string is :%s",a1); }

Program to reorder, rearrange list of numbers given by user

#include<stdio.h> #include<stdlib.h> #include<conio.h> main() {       int i,n,*x;       void reorder(int n, int *x);       printf("\nNumber of elements :");       scanf("%d",&n);       x=(int *)malloc(n*sizeof(int));       for(i=0;i<n;i++)       {          printf("\nEnter integer :");          scanf("%d",x+i);       }       reorder(n,x);       system("cls");       printf("\nReordered list \n");       for(i=0;i<n;++i)       {           printf("%d\t",*(x+i));       }       getch();       } void reorder(int n, int *x) {      int i,j,temp;      for(i=0;i<n-1;i++)      for(j=i+1;j<n;j++)      if(*(x+i)>*(x+j))      {          temp=*(x+i);          *(x+i)=*(x+j);          *(x+j)=temp;      } }

What is variable number of arguments in C

A function in which we can pass variables numbers of argument in function call statement such functions are known as function with variable number of arguments.

Try out this example:-

void display(int,...);
void main(){
void display(int x,...){

In above example, consecutive dot is known as ellipsis in c.

Greatest common divisor of two non-negative numbers

#include<stdio.h> //main function main () {      int u, v;      printf("\nEnter the two numbers to find GCD.\n");      scanf("%d%d",&u,&v);      gcd(u,v);      getch(); } //calling function gcd(u,v)    //gcd(u,v) being called by main function { int temp; printf("The gcd of %d and %d is ",u,v); while(v!=0)     {     temp=u%v;     u=v;     v=temp;     } printf("%d\n",u); }

What is Function?

When program becomes larger/bigger then we broke down into many independent routines (self dependent that is similar to small modules) is known as function. Also the technique of splitting a program into functions and modules is called Structured Programming or Procedure Oriented Programming (POP).

Program to find any number from array and print its position

#include<stdio.h> main () {      int a[100],item,num,i;      printf("Enter the number of element for array : ");      scanf("%d",&num);      printf("Enter the array elements (%d) for array : \n",num);      for(i=0;i<num;i++)      {         scanf("%d",&a[i]);      }      printf("Enter the item you want to search : ");      scanf("%d",&item); //start searching      for(i=0;i<num;i++)      if(item==a[i])      {         printf("\nItem %d is found at the position %d",item,i+1);      }      if(item!=a[i])      {         printf("\nItem not found.");      } getch(); }

Find how much time elapsed in this program to run

#include <time.h> #include <stdio.h>
  main()   {    printf("Elapsed time: %u secs.\n", clock()/CLOCKS_PER_SEC);    getch();   }

Creating a student data file using fread() function

#include<stdio.h> #include<conio.h> typedef struct { int dd; int mm; int yy; }DATE; typedef struct { char name[20]; int rno; double fees_inst; DATE pay_date; }STDREC; main() {       char wish;       char fees_inst[20];       STDREC std;       FILE *stdfile;       stdfile=fopen("student.dat","rb");       getch();       if(stdfile==0)       {       printf("\nError in opening.\n");       getch();       }       else       {           system("cls");           fread(&std,sizeof(STDREC),1,stdfile);           while(!feof(stdfile))           {           printf("\nName : %s",;           getch();           printf("\nRoll No : %d",std.rno);           printf("\nAmt Paid : %4.2f",std.fees_inst);           printf("\nPay Date :%d/%d/%d",std.pay_date.dd,,std.pay_date.yy);           fread(&std,sizeof(STDREC),1,stdfile);           getch();           }       fflush(stdin);       }fclose(stdfile); }

Difference between w+ and r+ modes

Both r+ and w+ we can read ,write on file but r+ does not truncate  (delete) the content of file  as well it doesn't create a new file if such file doesn't exits while in w+ truncate the content of file as well as create a new file if such file doesn't exists.

What is OOP?

OOP is stands for Object Oriented Programming. To combine the Data and its functions known as Objects and it's programmatically study is known as Object Oriented Programming.

Find the largest element and its position in array

#include<stdio.h> main() {       int A[10];       int largest, position, num, i;       printf("\nEnter the number of elements you want in array : ");       scanf("%d",&num);       printf("\nEnter the %d number for array.\n\n",num);       for(i=0;i<num;i++)       {          scanf("%d",&A[i]);       }       largest=A[0];       for(i=1;i<num;i++)       {          if(largest<A[i])             {             largest=A[i];             position=i+1;             }       }       printf("\nThe largest element is %d.\n",largest);       printf("\nPosition of the largest element %d is %d.\n\n",largest,position);       getch(); }

Reverse a String without using C functions

char * rev_string (char * str) { char temp; int i , j; for (i = 0 ; str[i]!= '\0' ; i++);
for(j = 0 ; j < i ; j++ , i--) {       temp = str[j];       str[j] = str[i];       str[i] = temp; }
return str; }

Simple graphics program in C

#include<stdio.h> #include<graphics.h> #include<conio.h>
void main() { int gd=DETECT, gm; initgraph(&gd, &gm, "c:\\tc\\bgi ");
circle(100,100,50); outtextxy(75,170,"Circle"); line(100,250,540,250); outtextxy(300,260,"Line"); rectangle(200,50,350,150); outtextxy(240,170,"Rectangle"); ellipse(500,100,0,360,100,50); outtextxy(480,170,"Ellipse"); line(100,250,540,250); outtextxy(300,260,"Line"); sector(150,400,30,300,100,50); outtextxy(120,460,"Sector"); getch(); clrscr(); closegraph(); }

What is command line argument?

Getting the arguments from command prompt in c is known as command line arguments.  In c main function has three arguments.

(a) Argument counter

(b) Argument vector

(c) Environment vector

For example:-

void main(int argc,char *argv[],char *env[]){
    int i;

Popular posts from this blog

5 Ways to send data between ASP.NET Pages