Skip to main content

Posts

Showing posts with the label C Language

printf() inside printf ()in C Language.

#include<stdio.h> #include<conio.h> void main() { int num=1342; clrscr(); printf( "%d" ,printf( "%d" ,printf( "%d" ,num))); getch(); } Output : 134241 How ? Firstly Inner printf is executed which results in printing 1324 This Printf Returns total number of Digits i.e 4 and second inner printf will looks like printf( "%d" ,printf( "%d" ,4)); It prints 4 and Returns the total number of digits i.e 1 (4 is single digit number ) printf( "%d" ,1); It prints simply 1 and output will looks like 132441 Rule : Inner printf returns Length of string printed on screen to the outer printf

Make Your Own Header File in C & C++?

Make Your Own Header File ? Step1 : Type this Code int Multiplication( int a, int b) { return (a*b); } In this Code write only function definition as you write in General C Program Step 2 : Save Code Save Above Code with [.h ] Extension . Let name of our header file be myhead [Multhead.h ] Compile Code if required. Step 3 : Write Main Program #include<stdio.h> #include "Multhead.h" void main() { int number1=10,number2=10,number3; number3 = Multiplication(number1,number2); printf( "Addition of Two numbers : %d" ,number3); } Include Our New Header File . Instead of writing < Multhead.h> use this terminology “Multhead.h” All the Functions defined in the Multhead.h header file are now ready for use . Directly call function Multiplication(); [ Provide proper parameter and take care of return type ] Note While running your program precaution to be taken : Both files [ Multhead.h and sample.c ] should be in same folder. Technorati Tag...

Program will accept Year,Month and Date from the user and will display the day of the month.

#include<stdio.h> #include<conio.h> #include<math.h> int isdatevalid( int month, int day, int year) { if (day <= 0) return 0 ; switch ( month ) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: if (day > 31) return 0 ; else return 1 ; case 4: case 6: case 9: case 11: if (day > 30) return 0 ; else return 1 ; case 2: if ( day > 29 ) return 0 ; if ( day < 29 ) return 1 ; else return 0 ; } return 0 ; } //------------------------------------------------ int fm( int date, int month, int year) { int fmonth,leap; //leap function 1 for leap & 0 for non-leap if ((year%100==0) && (year%400!=0)) leap=0; else if (year%4==0) leap=1; else leap=0; fmonth=3+(2-leap)*((month+2)/(2*month))+(5*month+month/9)/2; //f(m) formula fmonth = fmonth % 7; //bring it in range of 0 to 6 return fmonth; } //---------------------------------------------- int day_of_week( int date, int month, int year) { int ...

Write a C program that reads the customer number and power consumed and prints the amount to be paid by the customer.

An electric power distribution company charges its domestic consumers as follows. Consumption Rate of Units Charge ------------------------------------------------------ 0-200 Rs.0.50 per unit 201-400 Rs.100 plus Rs.0.65 per unit excess 200 401-600 Rs.230 plus Rs.0.80 per unit excess of 400. #include<stdio.h> #include<conio.h> void main() { int n, p; float amount; clrscr(); printf( "Enter the customer number: " ); scanf( "%d" ,&n); printf( "Enter the power consumed: " ); scanf( "%d" ,&p); if (p>=0 && p<=200) amount=p*0.50; else if (p>200 && p<400) amount = 100+((p-200) * 0.65); else if (p>400 && p<=600) amount = 230 + ((p-400) * 0.80); printf( "Amount to be paid by customer no. %d is Rs.:%5.2f." ,n,amount); getch(); } Output : Enter the customer number: 1 Enter the power consumed: 100 Amount to be paid by customer no. 1 is Rs.:50.00. ...

WAP FIND OUT SECOND LARGEST NUMBER IN AN UNSORTED ARRAY USING C LANGUAGE

#include<stdio.h> #include<conio.h> void main() { int a[5],i,j=0,big,secondbig; printf(" Enter 5 elements in to the array:); for (i=0;i<=4;i++) scanf(" %d ",&a[i]); big=a[0]; for (i=1;i<5;i++){ if (big<a[i]){ big=a[i]; j = i; } } secondbig=a[5-j-1]; for (i=1;i<5;i++){ if (secondbig <a[i] && j != i) secondbig =a[i]; } printf(" Second biggest Number: %d ", secondbig); return 0; }

Pointer in c language.

  What is Pointer ? Pointer is a address variable that hold the address of another variable.The * ( indirection or dereference operator), that  gives the contents of an object that pointed to by a pointer. To declare a pointer to a variable: data_type_name * variable name int *ptr; Example 1: void main( ) { int a = 10 ; printf ( " \nAddress of a = %u ", &a ); printf ( " \nValue of a = %d ", a ); } Output: address of a=1565 value of a=10 Example 2: void main() { int a = 5 ; printf ( " \nAddress of a = %u ", &a ); printf ( " \nValue of a = %d ", a ) ; printf ( " \nValue of a = %d ", *( &a ) ); } Output: Address of a = 1444 Value of a = 5 Value of a = 5

Program to Count Number of Words and Number of Characters in a String.

  #include <stdio.h> #include < string .h> #include <conio.h> void main() { char str[20]; int i=0, word=0, chr=0; clrscr(); printf( "\nEnter any string: " ); gets(str); while (str[i] != '\0' ) { if (str[i] == ' ' ) { word++; chr++; } else chr++; i++; } printf( "\nNumber of characters: %d" , chr); printf( "\nNumber of words: %d" , word+1); getch(); }

WAP Program to Add Two Matrices in c

#include <stdio.h> void main() { int a[10][10], b[10][10], c[10][10], i, j, row, col; printf( "\nEnter number of rows and columns: " ); scanf( "%d %d" , &row, &col); printf( "\nEnter elements of Array A:\n" ); for (i=0; i<row; i++) for (j=0; j<col; j++) scanf( "%d" , &a[i][j]); printf( "\nEnter elements of Array B:\n" ); for (i=0; i<row; i++) for (j=0; j<col; j++) scanf( "%d" , &b[i][j]); printf( "\nElements of Matrix A:\n\n" ); for (i=0; i<row; i++) { for (j=0; j<col; j++) printf( "\t%d" , a[i][j]); printf( "\n\n" ); } printf( "\nElements of Matrix B:\n\n" ); for (i=0; i<row; i++) { for (j=0; j<col; j++) printf( "\t%d" , b[i][j]); printf( "\n\n" ); }

WAP Program in C to convert Decimal number to Binary

1: #include<stdio.h> 2: #include<conio.h> 3: int binary ( int ); 4: void main() 5: { 6: int num; 7: clrscr(); 8: printf(“\nEnter The Number: “); 9: scanf(“%d”,&num); 10: binary(num); 11: printf(“\n\n\n\n\nPress any key to exit…..”); 12: getch(); 13: } 14: //function to convert deciaml to binary 15: int binary ( int n) 16: { 17: int r; 18: r=n%2; 19: n=n/2; 20: if (n==0) 21: { 22: printf(“\nThe binary equivalent is %d”,r); 23: return (r); 24: } 25: else 26: binary(n); 27: printf(“%d”,r); 28: }

Program in C to check a given year is leap year or not

1: #include<stdio.h> 2: #include<conio.h> 3: void main() 4: { 5: int year; 6: clrscr(); 7: printf(“ENTER YEAR????”); 8: scanf(“%d”,&year); 9: year%100==0?(year %400==0?printf(“leap Year”) 10: :printf(“Not a Leap Year”)):(year%4==0? 11: printf(“Leap Year”):printf(“Not a leap Year”)); 12: printf(“\n\n\n\n\nPress any key to exit…”); 13: getch(); 14: } Technorati Tags: C Language , c Programs

Program in C to calculate or find the day on 1st january of any year

1: #include<stdio.h> 2: #include<conio.h> 3: void main() 4: { 5: int leapdays,firstday,yr; 6: long int normaldays,totaldays; 7: clrscr(); 8: printf(“Enter year “); 9: scanf(“%d”,&yr); 10: normaldays=(yr-1)*365L; 11: leapdays=(yr-1)/4-(yr-1)/100+(yr-1)/400; 12: totaldays=normaldays+leapdays; 13: firstday=totaldays%7; 14: if (firstday==0) printf(“\nMonday”); 15: if (firstday==1) printf(“\Tuesday”); 16: if (firstday==2) printf(“\nWednesday”); 17: if (firstday==3) printf(“\nThrusday”); 18: if (firstday==4) printf(“\nFriday”); 19: if (firstday==5) printf(“\nSaturday”); 20: if (firstday==6) printf(“\nSunday”); 21: printf(“\n\n\n\n\nPress any key to exit….”); 22: getch(); 23: }

Program in C to calculate or find aggregate marks and percentage of a student in 5 subject

1: #include<stdio.h> 2: #include<conio.h> 3: void main() 4: { 5: int m1,m2,m3,m4,m5,aggr; 6: float per; 7: clrscr(); 8: printf(“\nEnter marks in 5 subject:”); 9: scanf(“%d%d%d%d%d”,&m1,&m2,&m3,&m4,&m5); 10: aggr=m1+m2+m3+m4+m5; 11: per=aggr/5; 12: printf(“\nAggregate Marks=%d”,aggr); 13: printf(“\nPercentage Marks=%f”,per); 14: printf(“\n\n\n\nPress any key yo exit….”); 15: getch(); 16: }

Program in C to Reverse the given string

1: #include<stdio.h> 2: int main(){ 3: int a[50],size,i,j=0,big,secondbig; 4: printf( "Enter the size of the array: " ); 5: scanf( "%d" ,&size); 6: printf( "Enter %d elements in to the array: " , size); 7: for (i=0;i<size;i++) 8: scanf( "%d" ,&a[i]); 9: 10: big=a[0]; 11: for (i=1;i<size;i++){ 12: if (big<a[i]){ 13: big=a[i]; 14: j = i; 15: } 16: } 17:   18: secondbig=a[size-j-1]; 19: for (i=1;i<size;i++){ 20: if (secondbig <a[i] && j != i) 21: secondbig =a[i]; 22: } 23: 24: printf( "Second biggest: %d" , secondbig); 25: return 0; 26: } 27:   28: