Programs of C

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 20

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
Number
INDEX
Expt. Page Teacher’s
Topic Date
No. No. Remarks
Programs for problems such as solution of
01. quadratic equation.

Program to find greatest among three numbers.


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

Program to convert centigrade temp. Into


04. Fahrenheit temp.

Programs for Summation of finite series.


05.
Programs for generating Fibonacci series, prime
06. numbers.

07. Program to find the factorial of a number.


Program to print average Marks of N students.
08.
Program to print GCD of Two numbers.
09.
Menu driven programs using switch statement to
10. do different arithmetic operations.

Programs to demonstrate the use of continue,


break and conditional operators like days of the
11. Week and months based on selection of a
character from user.

Program to print the following pyramid:


*
12. ***
*****
*******
13. Program to print the following pyramid:
1
121
12321
1234321
123454321
Program to print the following pyramid:
1
234
14. 56789
1234567

Expt. Page Teacher’s


Topic Date
No. No. Remarks
Program to print the following pattern:
1
23
15. 456
7891
23456
Program to print the following pattern:
L
LO
LOV
LOVE
LOVEL
16. LOVELY
LOVEL
LOVE
LOV
LO
L
Program to print a lowercase string to its
17. uppercase equivalent.
Program to find whether the string is Palindrome or
18. not.
MID – TERM EXAMS
Programs to demonstrate passing of variables to
19. functions by values and by address (pointer).
To swap two numbers and other test conditions
20. based on different problems.
Programs to demonstrate the use of command line
21. arguments.
Programs to manipulate arrays – sorting and
22. searching.
Program to sort the ‘N’ numbers entered by user
23. using functions.
Addition of n numbers using pointer to functions
24. and also to call the function using pointer variable.
25. Arithmetic operations on matrices.
String manipulation - comparing, copying,
26. reversing, finding length, extracting characters.
Programs demonstrating the use of pointers in
27. arrays.
Programs demonstrating the use of pointers in
28. strings.
Programs demonstrating the use of pointers in
29. functions.
30. Creating various types of records using structures.
Program to pass structure variables to a function
31. and returning 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);
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();
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();

}
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();
}
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();
}
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
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();
}
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);
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();

}
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;
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");
break;
case 6: printf("\n Friday");
break;
case 7: printf("\n Saturday");
default:
printf("\n Thanks for using this program");
}}
else
printf("\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++)
{
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");
}

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
{
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++;
}
}
printf("\n");
}
getch();

Program 16. Program to print the following pattern:


L
LO
LOV
LOVE
LOVEL
LOVELY
LOVEL
LOVE
LOV
LO
L

#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--)
{
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();
}

You might also like