Вы находитесь на странице: 1из 39

C Programming Examples

Edusahayi.com

C Sahayi
C Programming Examples

First Edition

From Edusahayi.com
2|C Sahayi : Basics First Edition

C Programming Examples

Edusahayi.com

C Cahayi C programming examples


Copyright 2013 Edusahayi.com All rights reserved. You are free to copy and distribute this document for personal use. No part of this document can be printed or used for any commercial application of any form without prior written permission from edusahayi.com

3|C Sahayi : Basics

First Edition

C Programming Examples

Edusahayi.com

Preface
After joining engineering degree C programming is the one of the subject people feel very tough. But it is very important to make friendship with C since it is an essential requirement for placement. Most of the students suffer in their practical examinations and interviews due to lack of practice in programming with right logic. Edusahayi is trying to help students with C Sahayi a free ebook which contain solved C Programming questions. We divided the entire C Programming topics to three ebooks. In this first book C Sahayi Part One: Basics you will find basic problems including control staments like ifelse,nested if,switch,etc and loops including for,while,dowhile and nested loops. Please enjoy the book. We tried to include most of the programs. If you find any difficulty in understanding them please post your doubts in discussion board of www.edusahayi.com or in our facebook page www.facebook.com/edusahayi so that others also can see the discussion. If you dont want your question go public you can mail your doubts to edusahayi@gmail.com. If you want any new programs solved or any other suggestion please let us know through the resources specified above. Edusahayi

4|C Sahayi : Basics

First Edition

C Programming Examples

Edusahayi.com

5|C Sahayi : Basics

First Edition

C Programming Examples

Edusahayi.com

Table of Contents
Table of Contents............................................................................................................ 6 Km to feet, inch, cm conversion ..................................................................................... 7 Check for vowel .............................................................................................................. 9 Leap Year ...................................................................................................................... 10 Largest of 3 numbers .................................................................................................... 12 Arithmetic Calculator .................................................................................................... 13 Multiplication Table ...................................................................................................... 15 Prime Number .............................................................................................................. 16 Sum of Digits of a number Using while ..................................................................... 17 Sum of terms of an Arithmetic Series........................................................................... 18 Reverse of a Number Using While ............................................................................. 19 Factorial of a Number ................................................................................................... 20 Amstrong Number ........................................................................................................ 21 Nth Power of a number ................................................................................................. 23 Palindrome Number ..................................................................................................... 24 Star Patterns ................................................................................................................. 26 LCM of given numbers .................................................................................................. 34 Decimal to Binary Conversion ...................................................................................... 36 Generation of Fibonacci Series ..................................................................................... 37

6|C Sahayi : Basics

First Edition

C Programming Examples

Edusahayi.com

Km to feet, inch, cm conversion


Q : Simple C Code for Km to feet ,inch ,cm conversion ( Metric Conversion) 1 Km = 1000m 1 m = 100cm 1m = 3. 2808 ft 1ft = 12 inches Program #include<stdio.h> #include<conio.h> main() { float km,m,ft,inch,cm; clrscr(); printf(Enter distance in Km : ); scanf(%f,&km); m=km*1000; cm=m*100; ft=3.2808*m; inch=12*ft; printf(Km : %16.3f\n,km); printf(meter : %16.3f\n,m);

7|C Sahayi : Basics

First Edition

C Programming Examples

Edusahayi.com

printf(cm : %16.3f\n,cm); printf(feet : %16.3f\n,ft); printf(Inch : %16.3f\n,inch); getch(); }

8|C Sahayi : Basics

First Edition

C Programming Examples

Edusahayi.com

Check for vowel


Q : C program to check whether entered letter is a vowel or not. Using ifelse and logical or. #include<stdio.h> #include<conio.h> main() { char c; clrscr(); printf(enter a digit : ); scanf(%c,&c); if(c==a'||c==e'||c==i'||c==o'||c==u'||c==A'||c==E'||c==I'||c==O'||c==U') printf(Vowel); else printf(Not a vowel); getch(); }

9|C Sahayi : Basics

First Edition

C Programming Examples

Edusahayi.com

Leap Year
Q : C program to check whether entered year is leap year or not. Leap Year Algorithm : if year is divisible by 400 then is_leap_year else if year is divisible by 100 then not_leap_year else if year is divisible by 4 then is_leap_year else not_leap_year Program #include<stdio.h> #include<conio.h> main() { int y; clrscr(); printf("\n enter the year\n ");

scanf("%d",&y); if(y%4==0) { if(y%100==0) { if(y%400==0) { 10 | C S a h a y i : B a s i c s First Edition

C Programming Examples

Edusahayi.com

printf(" common year"); if(y%1000==0) { if(y%4000==0) printf(" common year"); } } } else printf(" leap year"); } else printf(" common year"); getch(); }

11 | C S a h a y i : B a s i c s

First Edition

C Programming Examples

Edusahayi.com

Largest of 3 numbers
Q: C Code to find Largest of 3 given numbers Nested if else #include<stdio.h> #include<conio.h> main() { int a,b,c; clrscr(); printf(Enter the nos :); scanf(%d%d%d,&a,&b,&c); if(a>b) { if(a>c) printf(%d is largest,a); else printf(%d is largest,c); } else { if(b>c) printf(%d is largest,b); else printf(%d is largest,c); } getch(); } 12 | C S a h a y i : B a s i c s First Edition

C Programming Examples

Edusahayi.com

Arithmetic Calculator
Q : An arithmetic calculator to do basic arithmetic operations on two numbers. Select operations (Addition, subtraction, multipkication and division) from a menu.

#include<stdio.h> #include<conio.h> main() { float a,b,,r; int c; clrscr(); printf("\n enter the two No.s:"); scanf("%f%f",&a,&b); printf("\n CODES \n 1.addition\n 2.substraction\n 3.multiplication\n 4.division\n enter the function"); scanf("%d",&c); switch(c) { case 1: r=a+b; break; 13 | C S a h a y i : B a s i c s First Edition

C Programming Examples

Edusahayi.com

case 2: r=a-b; break; case 3: r=a*b; break; case 4: r=a/b; break; default: printf("\n invalid code"); break; } printf("\n%f",&r); getch(); }

14 | C S a h a y i : B a s i c s

First Edition

C Programming Examples

Edusahayi.com

Multiplication Table
Q: Programme to print multiplication table of a give number

#include<stdio.h> #include<conio.h> main() { clrscr(); int i,j,k; clrscr(); printf("\n **MULTIPLICATION TABLE**\n\n\n ENTER THE NO:");

scanf("%d",&j); for(i=1;i<=15;i++) { k=j*i; printf("\n %d x %d = %d ",i,j,k); } getch(); }

15 | C S a h a y i : B a s i c s

First Edition

C Programming Examples

Edusahayi.com

Prime Number
Q: To check whether entered number is prime or not #include<stdio.h> #include<conio.h> main() { int i,j,k; clrscr(); printf("enter the no:"); scanf("%d",&i); for(j=2;i%j!=0;j++) {} if(j==i) printf("\nprime no."); else printf("\nnot prime no"); getch(); }

16 | C S a h a y i : B a s i c s

First Edition

C Programming Examples

Edusahayi.com

Sum of Digits of a number Using while


#include<stdio.h> #include<conio.h> main() { int i,j,k=0; clrscr(); printf("\nenter the no.:"); scanf("%d",&i); while(i!=0) { j=i%10; k=k+j; i=i/10; } printf("sum of digits=%d",k); getch(); }

17 | C S a h a y i : B a s i c s

First Edition

C Programming Examples

Edusahayi.com

Sum of terms of an Arithmetic Series


#include<stdio.h> #include<conio.h> main() { int k,a,b,d; clrscr(); printf("\n enter the initial value:"); scanf("%d",&a); printf("\n common difference:"); scanf("%d",&d); printf("\n enter the final value:"); scanf("%d",&b); while(a<=b) { k=k+a; a=a+d; } printf("sum:%d",k); getch(); }

18 | C S a h a y i : B a s i c s

First Edition

C Programming Examples

Edusahayi.com

Reverse of a Number Using While


#include<stdio.h> #include<conio.h> main() { int i,j; clrscr(); printf("Enter the no;"); scanf("%d",&i); while(i!=0) { j=i%10; printf("%d",j); i=i/10; } getch(); }

19 | C S a h a y i : B a s i c s

First Edition

C Programming Examples

Edusahayi.com

Factorial of a Number
#include<stdio.h> #include<conio.h> main() { float i=1,j=1,l; clrscr(); printf("\nEnter the No.:"); scanf("%f",&l); while(i<=l) { j=j*i; i=i++; } printf("\nfactoril is %.0f",j); getch(); }

20 | C S a h a y i : B a s i c s

First Edition

C Programming Examples

Edusahayi.com

Amstrong Number
Amstrong Number : If sum of cubes of digits of a number is equal to the number then it is called amstrong number

#include<stdio.h> #include<conio.h> main() { int i,j,a,k=0; clrscr(); printf("\n enter the no:"); scanf("%d",&i); a=i; while(i!=0) { j=i%10; k=k+(j*j*j); i=i/10; } if(k==a) printf("amstrong no"); else 21 | C S a h a y i : B a s i c s First Edition

C Programming Examples

Edusahayi.com

printf("not an amstrong no"); getch(); }

22 | C S a h a y i : B a s i c s

First Edition

C Programming Examples

Edusahayi.com

Nth Power of a number


#include<stdio.h> #include<conio.h> main() { clrscr(); int a,b,j=1,i=0; printf("\nenter the No.&power"); scanf("%d%d",&a,&b); while(i<b) { j=j*a; i=i++; } printf("result:%d",j); getch(); }

23 | C S a h a y i : B a s i c s

First Edition

C Programming Examples

Edusahayi.com

Palindrome Number
Palindrom : If a number and its reverse are the same then it is a Palindrom Eg:121 Program #include<stdio.h> #include<conio.h> main() { int a,i,b,r=0; clrscr(); printf("Enter the no : "); scanf("%d",&a); b=a; while(a!=0) { r=r*10+a%10; a=a/10; } if(r==b) printf("Palindrom"); else 24 | C S a h a y i : B a s i c s First Edition

C Programming Examples

Edusahayi.com

printf("Not palindrom"); getch(); }

25 | C S a h a y i : B a s i c s

First Edition

C Programming Examples

Edusahayi.com

Star Patterns
Q : Code to print following patters for specified number of rows 1 * ** *** **** 2 **** *** ** * 3 * ** *** **** *** ** * 4 * ** *** ****

26 | C S a h a y i : B a s i c s

First Edition

C Programming Examples

Edusahayi.com

5 **** *** ** * 6 * ** *** **** *** ** * Program #include<stdio.h> #include<conio.h> main() { int m,i,j,r,k,ch; char x; clrscr(); do { clrscr();

27 | C S a h a y i : B a s i c s

First Edition

C Programming Examples

Edusahayi.com

printf("\n

***STARS***\n\n\n PATTERNS\n -----------\n

1.RIGHT TRIANGLE-up\n 2.RIGHT TRIANGLE-down\n 3.TRIANGLE-common\n 4.EQUILATERAL TRIANGLE-up\n 5.EQUILATERAL TRIANGLE-down\n 6.DIAMOND\n\n "); printf("ENTER NO. OF ROWS:"); scanf("%d",&r); printf("\n ENTER THE PATTERN:"); scanf("%d",&ch); clrscr(); printf("\n\n"); //1 if(ch==1) { for(i=1;i<=r;i++) { for(j=0;j<i;j++) { printf(" *"); } printf("\n"); } } //2 28 | C S a h a y i : B a s i c s First Edition

C Programming Examples

Edusahayi.com

else if(ch==2) { for(i=r;i>=0;i--) { for(j=0;j<i;j++) { printf(" *"); } printf("\n"); } } //3 else if(ch==3) { for(i=1;i<=r;i++) { for(j=0;j<i;j++) { printf(" *"); } printf("\n"); } 29 | C S a h a y i : B a s i c s First Edition

C Programming Examples

Edusahayi.com

for(i=r-1;i>=0;i--) { for(j=0;j<i;j++) { printf(" *"); } printf("\n"); } } //4 else if(ch==4) { for(i=1;i<=r;i++) { m=r-i; for(k=0;k<m;k++) printf(" "); for(j=0;j<i;j++) { printf(" *"); } printf("\n"); 30 | C S a h a y i : B a s i c s First Edition

C Programming Examples

Edusahayi.com

} } //5 else if(ch==5) { for(i=r;i>=0;i--) { m=r-i; for(k=0;k<m;k++) printf(" "); for(j=0;j<i;j++) { printf(" *"); } printf("\n"); } } //6 else if(ch==6) { for(i=1;i<=r;i++) { 31 | C S a h a y i : B a s i c s First Edition

C Programming Examples

Edusahayi.com

m=r-i; for(k=0;k<m;k++) printf(" "); for(j=0;j<i;j++) { printf(" *"); } printf("\n"); } for(i=r-1;i>=0;i--) { m=(r)-i; for(k=0;k<m;k++) printf(" "); for(j=0;j<i;j++) { printf(" *"); } printf("\n"); } } else 32 | C S a h a y i : B a s i c s First Edition

C Programming Examples

Edusahayi.com

{ printf("invalid code\n"); } printf(" DO YOU WANT TO CONTINUE(y/n).."); scanf("%s",&x); } while(x=='y'); getch(); }

33 | C S a h a y i : B a s i c s

First Edition

C Programming Examples

Edusahayi.com

LCM of given numbers


#include<stdio.h> #include<conio.h> main() { int a,b,t,i=1;; clrscr(); printf("Enter Two Nos : "); scanf("%d%d",&a,&b); if(b>a) { t=a; a=b; b=t; } for(i=1;i<=a;i++) { if((i*b)%a==0) { printf("LCM = %d",i*b);

34 | C S a h a y i : B a s i c s

First Edition

C Programming Examples

Edusahayi.com

break(); } } getch(); }

35 | C S a h a y i : B a s i c s

First Edition

C Programming Examples

Edusahayi.com

Decimal to Binary Conversion


#include<stdio.h> #include<conio.h> main() int i,j,n,a[10]; clrscr(); printf("ENTER THE NO.:"); scanf("%d",&n); printf("BINARY CODE IS "); for(i=0;n!=0;i++) { a[i]=n%2; n=n/2; } i--; for(j=i;j>=0;j--) printf("%d",a[j]); getch(); }

36 | C S a h a y i : B a s i c s

First Edition

C Programming Examples

Edusahayi.com

Generation of Fibonacci Series


#include<stdio.h> #include<conio.h> main() { int i=0,m=0,j=1,l,a; clrscr(); printf("\n enter the limit:"); scanf("%d",&l); while(i<l) { printf("\n%d",m); a=m; m=m+j; j=a; i=i++; } getch(); }

37 | C S a h a y i : B a s i c s

First Edition

C Programming Examples

Edusahayi.com

Coming Soon

Part 2 : Strings & Arrays Check edusahayi.com for updates

38 | C S a h a y i : B a s i c s

First Edition

C Programming Examples

Edusahayi.com

Coming Soon

Part 3 : Pointers & Functions Check edusahayi.com for updates

39 | C S a h a y i : B a s i c s

First Edition

Вам также может понравиться