Posts

Showing posts with the label printf()

First C Program

Image
Your First C Program Here, is a Hello World program in C #include<stdio.h> //Pre-processor directive void main() //main function declaration { printf("Hello World"); //to output the string on a display getch (); //terminating function } Here is the code explanation: Pre-processor directive #include is a pre-processor directive in 'C.' #include <stdio.h> , stdio is the library where the function printf is defined . printf is used for generating output. Before using this function, we have to first include the required file, also known as a header file (.h). You can also create your own functions, group them in header files and declare them at the top of the program to use them. To include a file in a program, use pre-processor directive #include <file-name>.h File-name is the name of a file in which the functions are stored. Pre-processor directives are always placed at the beginning of the program. The main function The

Count number of students above,below and average students

Count no. of students above,below and average students   #include<stdio.h>   #include<conio.h>    void main()     {      int a[10];      int aa=0,ba=0,ae=0,i;      clrscr();      printf("Enter the marks:\n");      for(i=0;i<10;i++)        { scanf("%d",&a[i]); if(a[i]>55) aa++; else if(a[i]<55) ba++; else ae++;        }      printf("No. OF AVG STUDENTS ARE:%d\n",ae);      printf("No. OF ABOVE AVERAGE STUDENTS:%d\n",aa);      printf("No. OF BELOW AVERAGE STUDENTS ARE:%d",ba);     getch();    }

constants usage in C

Basic example showing constants usage in C #include /*constants for bonus rates and sales*/ #define BONUSRATE1 0.1 #define BONUSRATE2 0.15 #define BONUSRATE3 0.2 #define SALES1 2000 #define SALES2 5000 #define SALES3 10000 int main() { int sales; double commission; /*get employees sales*/ printf("Please enter your total sales to the nearest dollar.\n"); scanf("%d", &sales); /*calculate employees bonus based on info*/ if(sales <=2000) { commission = sales * BONUSRATE1; printf("%g\n" , commission); } else if(sales > 2000 && sales <=5000) { commission = sales * BONUSRATE2; printf("%g\n" , commission); } else { commission = sales * BONUSRATE3; printf("%g\n" , commission); } return 0; }

a demo program which represent the ATM transaction

ATM programing ATM C programing language Program code  ‎/*Note Pin code is 1234*/ #include<stdio.h> #include<conio.h> void main(void) { unsigned long amount=1000,deposit,withdr​aw; int choice,pin=0,k=0; char another='y'; while(pin!=1234) { clrscr(); gotoxy(30,25); printf("Enter pin:"); scanf("%d",&pin); } clrscr(); do { printf("********Welcome to ATM Service**************\n"); printf("1. Check Balance\n"); printf("2. Withdraw Cash\n"); printf("3. Deposit Cash\n"); printf("4. Quit\n"); printf("******************​**************************​*\n\n"); printf("Enter your choice: "); scanf("%d",&choice); switch(choice) { case 1: printf("\nYour Balance is Rs : %lu ",amount); break; case 2: printf("\nEnter the amount to withdraw: "); scanf("%lu",&withdraw); if(withdraw%100!=0) { printf("\

Descending an array using c programming

ARRANGE THE ELEMENTS IN ARRAY IN DESCENDING ORDER main() { int a[100],i,n,j,search,temp; printf("\n how many no's in array"); scanf("%d",&n); printf("\n enter %d elements in array",n); for(i=0;i scanf("%d",&a[i]); for(i=0;i { for(j=i+1;j { if(a[i] { temp=a[i]; a[i]=a[j]; a[j]=temp; } } printf("%4d",a[i]); } getch(); }

add two numbers without using operator as like +

Add Two numbers without using operator #include<stdio.h> int main (){        int a , b ;     int sum ;     printf ( "Enter any two integers: " );     scanf ( "%d%d" ,& a ,& b );     //sum = a - (-b);     sum = a - ~ b - 1 ;     printf ( "Sum of two integers: %d" , sum );     return 0 ; }

Add two matrices and store the result into another matrice

Add two matrices and store the result #include<stdio.h> #include<conio.h> void main() { int a[3][3],b[3][3],c[3][3],i,j; clrscr(); printf("Enter the elements into matrix A\n"); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&a[i][j]); printf("\nEnter the elements into matrix B\n"); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&b[i][j]); for(i=0;i<3;i++) for(j=0;j<3;j++) c[i][j]=a[i][j]+b[i][j]; for(i=0;i<3;i++) { printf("\t\t"); for(j=0;j<3;j++) printf("%d\t",c[i][j]); printf("\n\a"); } getch(); }

use of pointer with array to do a sum

Add Pointers #include<stdio.h> #include<conio.h>  void main()  {  int a[10],sum=0;  int *j;  int i,n;  clrscr();  printf("Enter how many elements u want to add:");  scanf("%d",&n);  printf("Enter the elements:");  for(i=0;i<n;i++)  {  scanf("%d",&a[i]);  }   j=&a[0];  for(i=0;i<n;i++)      {        sum=sum+*j;        j++;      }  printf("sum=%d",sum); }

Add numbers using command line arguments (CLA)

Add numbers using command line arguments (CLA) #include<stdio.h> #include<conio.h> #include<stdlib.h> void main(int argc,char *argv[]) { int sum=0,i; //Compare if proper number of arguments have been entered if(argc<3) { printf("Insufficient number of arguments:\n"); getch(); return 0; } //Add all the numbers entered using atoi function for(i=1;i<argc;i++) { sum+=atoi(argv[i]); } //print the sum printf("Ans=%d",sum); getch(); }

bubble sort using user defined function

A bubble sort routine # include # include void bubblesort(int array[],int size); void main() { int values[10],j; for(j=0;j<10;j++) values[j] = rand()%100; /*unsorted*/ printf("\nUnsorted values.\n"); for(j=0;j<10;j++) printf("%d ",values[j]); /*sorted*/ printf("\nSorted values.\n"); bubblesort(values,10); for(j=0;j<10;j++) printf("%d ",values[j]); } void bubblesort(int array[],int size) { int tmp ,i,j; for(i = 0;i for(j=0;j < size;j++) if(array[i] < array[j]) { tmp = array[i]; array[i] = array[j]; array[j] = tmp; } }