programs of c

24
Lovely Institute of Technology (Polytechnic) Subject Code: CSE 100 Subject: Basics of C Language Name Shamsher Singh Section N1801 Session 2008-2011 Department L.I.T {Polytechnic} Roll Number A05 Degree/Diploma Diploma C.S.E -115 Registration 10800393

Upload: sherawala

Post on 28-Mar-2015

65 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Programs of C

Lovely Institute of Technology (Polytechnic)

Subject Code: CSE 100

Subject: Basics of C Language

Name Shamsher SinghSection N1801Session 2008-2011Department L.I.T {Polytechnic}Roll Number A05Degree/Diploma Diploma C.S.E -115Registration Number 10800393

Page 2: Programs of C

INDEX

Expt. No. Topic Page No. Date Teacher’s Remarks

01.Programs for problems such as solution of quadratic equation.

02.Program to find greatest among three numbers.

03.Program to display 4-digit number in opposite order.

04.Program to convert centigrade temp. Into Fahrenheit temp.

05.Programs for Summation of finite series.

06.Programs for generating Fibonacci series, prime numbers.

07. Program to find the factorial of a number.

08.Program to print average Marks of N students.

09.Program to print GCD of Two numbers.

10.Menu driven programs using switch statement to do different arithmetic operations.

11.

Programs to demonstrate the use of continue, break and conditional operators like days of the Week and months based on selection of a character from user.

12.

Program to print the following pyramid: * *** ***** *******

13.

Program to print the following pyramid: 1 121 12321 1234321 123454321

Page 3: Programs of C

14.

Program to print the following pyramid: 1 234 56789 1234567

Expt. No. Topic Page No. Date Teacher’s Remarks

15.

Program to print the following pattern: 1 23 456 7891 23456

16.

Program to print the following pattern:LLOLOVLOVELOVELLOVELYLOVELLOVELOVLOL

17. Program to print a lowercase string to its uppercase equivalent.18. Program to find whether the string is Palindrome or not.

MID – TERM EXAMS

19. Programs to demonstrate passing of variables to functions by values and by address (pointer).

20. To swap two numbers and other test conditions based on different problems.

21. Programs to demonstrate the use of command line arguments.22. Programs to manipulate arrays – sorting and searching.

23. Program to sort the ‘N’ numbers entered by user using functions.

24. Addition of n numbers using pointer to functions and also to call the function using pointer variable.

25. Arithmetic operations on matrices.

26. String manipulation - comparing, copying, reversing, finding length, extracting characters.

27. Programs demonstrating the use of pointers in arrays.28. Programs demonstrating the use of pointers in strings.29. Programs demonstrating the use of pointers in functions.30. Creating various types of records using structures.31. Program to pass structure variables to a function and returning

Page 4: Programs of C

of a structure variable from a function.

Class Teacher’s Remarks

Program 1. Programs for problems such as solution of quadratic equation.

#include<stdio.h>#include<conio.h>void main(){int a,b,c,d,e,f,g;clrscr();printf("Enter the three values");scanf("%d%d%d",&a,&b,&c);d=((b*b)-(4*a*c));if(d==0){printf("Roots are real and equal");f=-b/(2*a);printf("x1=%d\nx2=%d",f,f);}else if(d>0){printf("Roots are real and distinct");e=sqrt(d);f=(-b+e)/(2*a);g=(-b-e)/(2*a);printf("x1=%d\nx2=%d",f,g);}else{printf("Roots are imaginary");d=-d;e=sqrt(d);f=-b/(2*a);g=e/(2*a);

Page 5: Programs of C

printf("x1=%d+%d\nx2%d-%d",f,g,f,g);}getch();}

Program 2: Program to Find Greatest among three numbers

void main(){int a,b,c,big;clrscr();printf("\nEnter 3 numbers:");scanf("%d %d %d",&a,&b,&c);big=(a>b&&a>c?a:b>c?b:c);printf("\nThe biggest number is:%d",big);getch();

Page 6: Programs of C

Program 3. Program to display 4 digit numbers in opposite Order.

#include<stdio.h>#include<conio.h>void main(){int number;int opp=0,c=1000;int remainder;clrscr();printf("\n Enter a 4 digit number please ");scanf("%d",&number);while(number>0){remainder=number%10;opp=opp+remainder*c;number=number/10;c=c/10;}printf("\nNumber in opposite order is %d ",opp);getch();

}

Page 7: Programs of C

Program 4: Program to convert centigrade temp. into Fahrenheit Temperature

#include<stdio.h>#include<conio.h>void main(){float celcius,farenheit;clrscr();printf( "Enter Celsius\n" );scanf("%f",&celcius);farenheit=1.8*celcius+32;

printf("\nTemerature in Farenheit is %f",farenheit);}getch(); }

Page 8: Programs of C

Program 5 : Program for summation of finite series

#include<stdio.h>#include<conio.h>void main(){int start,end,i,sum=0;clrscr();printf("\n Enter the value of Start of the Series");scanf("%d",&start);printf("\n Enter the value for End of the Series");scanf("%d",&end);for(i=start;i<=end;i++){sum=sum+(3*i+4);}printf("\n Summation of this series is %d ",sum);getch();}

Page 9: Programs of C

Program 6: Program to generate Fibonacci Series, Prime number.

Fibonacci Series Prime Number

#include<stdio.h> #include<stdio.h>#include<conio.h> #include<conio.h>void main() void main()

{ {int a=0,b=1,c=0,n,sum=0; int a,c=0,i,n;clrscr(); clrscr();printf("\n Enter a Value of n "); printf("enter the number to be

checked");scanf("%d",&n); scanf("%d",&n);printf("%d %d ",a,b); for(i=1;i<=n;i++)

{while(sum<=n)

{ a=n%i;sum=a+b; if(a==0)printf("%d ",sum); {a=b; c = c+1;b=sum; }c=c+sum; }

} if(c==2)printf("\n Total = %d ",c); {getch(); printf(“\n The given Number is

Prime”);} else

Page 10: Programs of C

printf("the given number is not prime");

getch();}

Program 7 : Program to find factorial of a number

#include<stdio.h>#include<conio.h>void main(){int number,a;int fact=1;clrscr();printf("\n Enter the number who's factorial you want ");scanf("%d",&number);for(a=1;a<=number;a++){fact=fact*a;}printf("\n Factorial is %d ",fact);getch();}

Page 11: Programs of C

Program 8: program to find average of marks for N students

#include<stdio.h>#include<conio.h>void main(){int number,physics,chemistry,mathematics,a,avg;int sum=0;clrscr();printf("\n How many students Record you want to Enter ");scanf("%d",&number);for(a=1;a<=number;a++){printf("\n Enter the Marks for the student Number %d",a);printf("\n Marks in Physics = ");scanf("%d",&physics);printf("\n Marks in Chemistry = ");scanf("%d",&chemistry);printf("\n Marks in Mathematics = ");scanf("%d",&mathematics);sum=physics+chemistry+mathematics;avg=sum/3;printf("\n Average is %d",avg);

Page 12: Programs of C

printf("\n\n*********************************************************\n\n");}getch();}

Program 9 : Program to Print GCD of two numbers.

#include<stdio.h>#include<conio.h>void main(){int i,a=24,b=16,f=0,gcd=0;clrscr();for (i=1;i<=b;i++){if(a%i==0&&b%i==0)f=i;}printf("%d",f);getch();

}

Page 13: Programs of C

Program 10 : Menu driven programs using switch statement to do different arithmetic operations.

#include<stdio.h>#include<conio.h>void main(){int a,b;int choice;clrscr();printf("Enter first number: ");scanf("%d",&a);printf("Enter second number: ");scanf("%d",&b);printf("Please Enter your Choice :\n");printf("\n\nPlease Enter 1 for Add\n2 for Subtract\n3 for Multipy and\n4 for Divide\n");scanf("%d",&choice);switch(choice){case 1 : printf("\nSum of the two number = %d", a+b);break;case 2 : printf("Difference of two number is = %d", a-b);break;case 3 : printf("Product of two number is = %d", a*b);break;

Page 14: Programs of C

case 4 :if(b!=0)printf("devision of two number is = %d", a/b);break;default:printf("\n Thanks for using this program");}getch();}

Program 11. Programs to demonstrate the use of continue, break and conditional operators like days of the Week and months based on selection of a character from user.

Switch Statement with break and condition Continue Statement

#include<stdio.h> #inlcude<stdio.h>#include<conio.h> #include<conio.h>void main() void main(){ {int choice; int i, j ; clrscr(); clrscr();printf("\n Please Enter your Choice :"); for ( i = 1 ; i <= 2 ; i++ ) scanf("%d",&choice); {if(choice<=7) for ( j = 1 ; j <= 2 ; j++ ) { {switch(choice) if ( i == j ) { continue;case 1 : printf("\nSunday"); printf ( "\n%d %d\n", i, j ) ; break; }case 2 : printf("\nMonday"); }break; }case 3 : printf("\nTuesday");break;case 4 : printf("\nWednesday");break;case 5: printf("\nThursday");

Page 15: Programs of C

break;case 6: printf("\n Friday");break;case 7: printf("\n Saturday");default:printf("\n Thanks for using this program");} }elseprintf("\n Please enter value between 1 to 7");getch();}Program 12: Program to print the following pyramid:

* *** ***** *******

#include<stdio.h>#include<conio.h>void main(){int n, i,j,k,l,m,c=1;clrscr ();printf ("\n Enter the number of Rows");scanf ("%d",&n); clrscr ();m=n;for(i=1;i<=n;i++) {

for(j=m;j>1;j--) {

printf (" ");}m--;for(k=1;k<=c;k++)

Page 16: Programs of C

{printf("* ");

}printf("\n");c=c+2;}getch();}

Program 13 : Program to print the following pyramid: 1 121 12321 1234321 123454321

#include<stdio.h>#include<conio.h>void main(){int i,j,k,n;clrscr();for(i=1;i<=5;i++){ for(n=0;n<5-i;n++) { printf(" "); } for(j=1;j<=i;j++) { printf("%d",j); } for(k=j-2;k>0;k--) { printf("%d",k); } printf("\n");

Page 17: Programs of C

}

getch();}

Program 14 : Program to print the following pyramid: 1 234 56789 1234567

#include<stdio.h>#include<conio.h>void main(){int c=1,i,j,n;clrscr();

for(i=1;i<=4;i++){ for(n=0;n<=5-i;n++) {

printf(" "); } for(j=1;j<=2*i-1;j++) { if(c<=9) { printf("%d",c); c++; } else

Page 18: Programs of C

{ c=1; printf("%d",c); c++; } } printf("\n");}getch();

}Program 15 : Program to print the following pattern: 1

23 456 7891 23456

#include<stdio.h>#include<conio.h>void main(){int c=1,i,j;clrscr();

for(i=1;i<=6;i++){ for(j=1;j<=1*i-1;j++) { if(c<=9) { printf("%d",c); c++; } else { c=1; printf("%d",c); c++;

Page 19: Programs of C

} } printf("\n");}getch();

}

Program 16. Program to print the following pattern:LLOLOVLOVELOVELLOVELYLOVELLOVELOVLOL

#include<stdio.h>#include<conio.h>void main(){char name[10]="LOVELY";int i,j;clrscr();for(i=0;i<=5;i++){

for(j=0;j<=i;j++){printf("%c",name[j]);}printf("\n");

}for (i=5;i>=0;i--)

Page 20: Programs of C

{for(j=0;j<=i-1;j++){printf("%c",name[j]);}printf("\n");

}getch ();

}

Program 17. Program to print a lowercase string to its uppercase equivalent .

#include<stdio.h>#include<conio.h>#include<string.h>void main(){

int length;char *a;char *b;clrscr();printf("\n Enter any String please ");scanf("%s",a);length=strlen(a);b=strupr(a);printf("\n The string you entered is %s",a);printf("\n Length of the string you entered is %d",length);printf("\n upper case conversion is %s ",b);

getch();}

Page 21: Programs of C