Sie sind auf Seite 1von 33

CHANDIGARH UNIVERSITY, GHARUAN

1 Name: Puneet Goyal

UID: 13BCS1193
Program-1

To Print a Message on the Screen

Syntax:-
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
printf(Welcome);
getch();
}

CHANDIGARH UNIVERSITY, GHARUAN
2 Name: Puneet Goyal
UID: 13BCS1193
Program-2

To Find the Given Number Is Even or Odd

Syntax:-
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
int a;
printf(Enter a number);
scanf(%d ,&a);
if (a%2==0)
{
printf(Number Is Even);
}
else
{
printf(Number Is Odd);
}
getch();
}

CHANDIGARH UNIVERSITY, GHARUAN
3 Name: Puneet Goyal
UID: 13BCS1193
Program-3

To Find the Greatest among 3 Number Using if else
Statement

Syntax:-
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
int a,b,c;
printf(Enter a number);
scanf(%d%d%d ,&a,&b,&c);
if (a>b & a>c)
{
printf(a is greater then b and c:%d ,a);
}
else if (b>a & b>c)
{
printf(b is greater then a and c:%d ,b);
}
else
printf(c is greater then a and b:%d ,c);
}
getch();
}

CHANDIGARH UNIVERSITY, GHARUAN
4 Name: Puneet Goyal
UID: 13BCS1193

Program-4

To Find the Greatest among 3 Number Using if else
Statement
Syntax:-
#include<stdio.h>
#include<conio.h>
void main()
{
int ch,a,b,c;
clrscr();
printf("enter two numbers:");
scanf("%d%d",&b,&c);
printf("\n Now enter 1 for Add,2 for Sub, 3 for multiply,4 for division");
scanf("%d",&ch);
switch (ch)
{ case 1:
a=b+c;
printf("sum of two no is:%d",a);
break;
case 2:
a=b-c;
printf("sub of two no is:%d",a);
break;
case 3:
a=b*c;
CHANDIGARH UNIVERSITY, GHARUAN
5 Name: Puneet Goyal
UID: 13BCS1193
printf("multipication of two no is:%d",a);
break;
case 4:
a=b/c;
printf("division of two no is:%d",a);
break;
default:
printf("wrong value");
}
getch();
}

CHANDIGARH UNIVERSITY, GHARUAN
6 Name: Puneet Goyal
UID: 13BCS1193
Program-5

To Print Pyramid of Different Pattern

a) Syntax:-
#include<conio.h>
#include<stdio.h>
void main()
{
int i,j,k=1;
clrscr();
for (i=1;i<=5;i++)
{
for( k=4;k>=i;k--)
{
printf("\t");
}
for( j=1;j<=i;j++)
{
printf("*\t\t");
}
printf("\n");
}
getch();
}

CHANDIGARH UNIVERSITY, GHARUAN
7 Name: Puneet Goyal
UID: 13BCS1193
b) Syntax:-
#include<conio.h>
#include<stdio.h>
void main()
{
int i,j,k=1;
clrscr();
for (i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
printf("%d\t",i);

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

c) Syntax:-
#include<conio.h>
#include<stdio.h>
void main()
{
CHANDIGARH UNIVERSITY, GHARUAN
8 Name: Puneet Goyal
UID: 13BCS1193
int i,j,k=1;
clrscr();
for (i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
printf("%d\t",j);

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

d) Syntax:-
#include<conio.h>
#include<stdio.h>
void main()
{
int i,j,k=1;
clrscr();
for (i=1;i<=4;i++)
{
for(j=1;j<=i ;j++)
CHANDIGARH UNIVERSITY, GHARUAN
9 Name: Puneet Goyal
UID: 13BCS1193
{
printf("%d\t",k);
k++;
}
printf("\n");
}
getch();
}

Program-6

To Make A Program Of Palindrome No.
Syntex:-
#include<conio.h>
#include<stdio.h>
void main()
{
int rem=0,rev=0,c=0;
int num,z;
clrscr();
printf("Enter a number: ");
scanf("%d",&z);
num=z;
CHANDIGARH UNIVERSITY, GHARUAN
10 Name: Puneet Goyal
UID: 13BCS1193
while(num>0)
{
rem=num%10;
num=num/10;
rev=(rev*10)+rem;
}
printf("Reversed number is : %d",rev);
if(z==rev)
{
printf("\nNumber is palindrome");
c=1;
}
if(c==0)
printf("\nNot Palindrome");
getch();
}

CHANDIGARH UNIVERSITY, GHARUAN
11 Name: Puneet Goyal
UID: 13BCS1193
Program-7

To Find Factorial Of A Number
#include<stdio.h>
#include<conio.h>
void main()
{
int i,f=1,r;
clrscr();
printf("Enter a number whose factorial u want to find= ");
scanf("%d"&i);
for(r=i;r>=1;r--)
{
f=f*r;
}
printf("%d",f);
getch();
}

CHANDIGARH UNIVERSITY, GHARUAN
12 Name: Puneet Goyal
UID: 13BCS1193
Program-8

To Make A Program Of A Function

Syntax:-
#include<stdio.h>
#include<conio.h>
int sum (int x,int y);
void main()
{
int t;
clrscr();
t=sum(10,50);
printf("sum is %d",t);
getch();
}
int sum(int x,int y)
{
int s;
s=x+y;
return s;
}

CHANDIGARH UNIVERSITY, GHARUAN
13 Name: Puneet Goyal
UID: 13BCS1193
Program-9

Program Of Root

Syntax:-
#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
int a,b,c,d;
float r1,r2;
printf("enter the values of a,b,c");
scanf("%d%d%d",&a,&b,&c);
printf(" equation is: %d x2 + %d x + %d",a,b,c);
d= (b*b) - (4*a*c);
printf(" discrimant is:%d",d);
if(d<0)
{
printf("roots of the equation are imaginary");
} else {
printf("roots are real and their value is:");
r1= (float)(b+ pow(d,0.5))/ ( 2*a);
r1= (float)(b- pow(d,0.5))/ ( 2*a);
printf("%f,%f",r1,r2);
} getch(); }

CHANDIGARH UNIVERSITY, GHARUAN
14 Name: Puneet Goyal
UID: 13BCS1193
Program-10

Program To Find Area Using Function

Syntax:-
#include<stdio.h>
#include<math.h>
double area_of_triangle(double, double, double);
main()
{
double a, b, c, area;
clrscr();
printf("Enter the sides of triangle\n");
scanf("%lf%lf%lf",&a,&b,&c);
area = area_of_triangle(a, b, c);
printf("Area of triangle = %.2lf\n", area);
getch();
}
double area_of_triangle( double a, double b, double c )
{
double s, area;
s = (a+b+c)/2;
area = sqrt(s*(s-a)*(s-b)*(s-c));
return area;
}
CHANDIGARH UNIVERSITY, GHARUAN
15 Name: Puneet Goyal
UID: 13BCS1193

Program-11

Program Of Swapping No. By Call By Value And Call By
Reference
Syntax:-a) Call By Value
#include<stdio.h>
#include<conio.h>
void swap(int x, int y)
{
int temp;
clrscr();
temp = x;
x = y;
y = temp;
printf("Swapped values are a = %d and b = %d", x, y);
}
void main()
{
int a = 7, b = 4;
swap(a, b);
printf("\nOriginal values are a = %d and b = %d", a, b);
printf("\nThe values after swap are a = %d and b = %d", a, b);
getch();
CHANDIGARH UNIVERSITY, GHARUAN
16 Name: Puneet Goyal
UID: 13BCS1193
}

Syntax:-b)Call By Reference
#include<stdio.h>
#include<conio.h>
void swap(int *x, int *y)
{
int temp;
clrscr();
temp = *x;
*x = *y;
*y = temp;
printf("Swapped values are a = %d and b = %d", *x, *y);
}
void main()
{
int a = 7, b = 4;
swap(&a, &b);
printf("\nOriginal values are a = %d and b = %d",a,b);
printf("\nThe values after swap are a = %d and b = %d",a,b);
getch();
}
CHANDIGARH UNIVERSITY, GHARUAN
17 Name: Puneet Goyal
UID: 13BCS1193

CHANDIGARH UNIVERSITY, GHARUAN
18 Name: Puneet Goyal
UID: 13BCS1193
Program-12

Program Of GCD
Syntax:-
#include<stdio.h>
#include<conio.h>
int gcd(int x,int y);
void main()
{
int a,b,x,y,r;
clrscr();
printf("enter any 2 no.");
scanf("%d%d",&a,&b);
r=gcd(x,y);
printf("gcd is %d",r);
getch();
}
int gcd(int x,int y)
{
if(x>y)
return gcd(y,x-y);
else if(y>x)
return gcd(x,y-x);
else if (x==y)
return 1;
}

CHANDIGARH UNIVERSITY, GHARUAN
19 Name: Puneet Goyal
UID: 13BCS1193
Program-13

Program Of Print Prime No Up To n No.
Syntax:-
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j;
printf("enter the range:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=2;j<=(n-1);j++)
{
if(i%j==0)
{
break;
}
}
if(i==j)
{
printf("%d\n",i);
}
}
getch();
}

CHANDIGARH UNIVERSITY, GHARUAN
20 Name: Puneet Goyal
UID: 13BCS1193
Program-14

Program Of Area Of Circle By Call By Value And Call By
Reference
Syntax:-a) Call By Value

#include<stdio.h>
#include<conio.h>
void area(int);
void main()
{
int r;
float a;
clrscr();
scanf("%d",&r);
area(r);
getch();
}
void area (int x)
{
float a;
a=3.14*x*x;
printf("area of the circle is %f",a);
}

Syntax:-b)Call By Reference
#include<stdio.h>
#include<conio.h>
void area(int*);
CHANDIGARH UNIVERSITY, GHARUAN
21 Name: Puneet Goyal
UID: 13BCS1193
void main()
{
int r;
clrscr();
scanf("%d",&r);
area(&r);
getch();
}
void area(int *x)
{
float a;
a=(3.14*(*x)*(*x));
printf("area of the circle is %f",a);
}

CHANDIGARH UNIVERSITY, GHARUAN
22 Name: Puneet Goyal
UID: 13BCS1193
Program-15

Program Of Fibonacci

Syntax:-a) By Using Function
#include<stdio.h>
#include<conio.h>
int main()
{
int n, first=0,second=1,next,c;
clrscr();
printf("Enter the number of terms\n");
scanf("%d",&n);
printf("First %d terms of Fibonacci series are :-\n",n);
for (c=0;c<n;c++)
{
if (c<=1)
next = c;
else
{
next = first + second;
first = second;
second = next;
}
printf("%d\n",next);
}

getch();
}
CHANDIGARH UNIVERSITY, GHARUAN
23 Name: Puneet Goyal
UID: 13BCS1193

Syntax:-a) By Using Recursion
#include<stdio.h>
#include<conio.h>
int Fibonacci(int);
int main()
{
int n, i=0, c;
clrscr();
scanf("%d",&n);
printf("Fibonacci series\n");
for ( c=1;c<=n;c++)
{
printf("%d\n", Fibonacci(i));
i++;
}
getch();
}
int Fibonacci(int n)
{
if (n==0)
return 0;
else if (n==1)
return 1;
CHANDIGARH UNIVERSITY, GHARUAN
24 Name: Puneet Goyal
UID: 13BCS1193
else
return (Fibonacci(n-1) + Fibonacci(n-2));
}

CHANDIGARH UNIVERSITY, GHARUAN
25 Name: Puneet Goyal
UID: 13BCS1193
Program-16
Write a program to display the biggest value in an
integer array.
Syntax:-
#include<stdio.h>
#include<conio.h>
void main () {
int big,a[20],n,i,j;
clrscr();
printf("enter the number of elements");
scanf("%d",&n);
for(i=1;i<=n;i++)
{printf("enter the elements");
scanf("%d",&a[i]);}
big=a[1];
for(j=1;j<n;j++)
{
if(big<a[j])
{
big=a[j];
}}
printf("\nbiggest element is %d",big);
getch();
}

CHANDIGARH UNIVERSITY, GHARUAN
26 Name: Puneet Goyal
UID: 13BCS1193
Program-17
Write a program to sort the array using bubble sort.
Syntax:-
#include<stdio.h>
#include<conio.h>
void main()
{ int n,a[20],temp,m,i,l,k;
clrscr();
printf("enter nUMBER OF ELEMENTS");
scanf("%d",&n);
for(i=1;i<=n;i++)
{printf("enter the elements");
scanf("%d",&a[i]);
}
for(k=1;k<=n-1;k++)
{
for(l=1;l<=n-k;l++)
{
if(a[l]>a[l+1])
{
temp=a[l];
a[l]=a[l+1];
a[l+1]=temp;
}}}
for(m=1;m<=n;m++)
{printf("%d\t",a[m]);}
getch();}

CHANDIGARH UNIVERSITY, GHARUAN
27 Name: Puneet Goyal
UID: 13BCS1193
Program-18
Write a program to multiply two matrices using arrays.
Syntax:-
#include<stdio.h>
#include<conio.h>
void main()
clrscr();
printf("Enter number of rows and columns of first matrix \n");
scanf("%d%d",&r1,&c1);
printf("Enter number of rows and columns of second matrix \n");
scanf("%d%d",&r2,&c2);
if(r2==c1)
{printf("Enter rows and columns of First matrix \n");
printf("Row wise\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&m1[i][j]);}
printf("You have entered the first matrix as follows:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%d\t",m1[i][j]);
printf("\n");}
printf("Enter rows and columns of Second matrix \n");
printf("Again row wise\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
scanf("%d",&m2[i][j]);
}
printf("You have entered the second matrix as follows:\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
printf("%d\t",m2[i][j]);
printf("\n");
}

printf("Now we multiply both the above matrix \n");
printf("The result of the multiplication is as follows:\n");

for(i=0;i<r1;i++)
CHANDIGARH UNIVERSITY, GHARUAN
28 Name: Puneet Goyal
UID: 13BCS1193
{
for(j=0;j<c2;j++)
{
mult[i][j]=0;
for(k=0;k<r1;k++)
{
mult[i][j]+=m1[i][k]*m2[k][j];

}
printf("%d\t",mult[i][j]);
}
printf("\n");
}

}
else
{
printf("Matrix multiplication cannot be done");
} getch();
}

CHANDIGARH UNIVERSITY, GHARUAN
29 Name: Puneet Goyal
UID: 13BCS1193
Program-19
Write a program to concatenate two strings & store the result
in third string.
Syntax:-
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
char a[20];
char b[20];
char c[20];
clrscr();
printf("enter the first string\n ");
scanf("%s",a);
printf("enter the second string\n ");
scanf("%s",b);
strcpy(c,strcat(a,b));
printf("\n %s",c);
getch();}

CHANDIGARH UNIVERSITY, GHARUAN
30 Name: Puneet Goyal
UID: 13BCS1193
Program-20
Write a program to check whether the string is palindrome.

a) with using string inbuilt functions
b) without using string inbuilt functions.

a)with using string inbuilt functions b)without using string
inbuilt functions

Syntax:- Syntax:-
#include<conio.h> #include<conio.h>
#include<stdio.h> #include<stdio.h>
#include<string.h> #include<string.h>
void main() void main()
{ {
char a[20]; char a[20];
char b[20]; char b[20];
clrscr(); clrscr();
printf("enter the string\n"); printf("enter the string\n");
scanf("%s",a); gets(a);
strcpy(b,a); for(i=0;a[i]='\0'; i++)
strrev(a); for(j=0;i=0;i--)
printf("reverse of string is=%s",a); { b[j++]=a[i];}
if((strcmp(b,a)==0)) b[j]='\0';
{printf("\n string is pallindrome");} for(i=0;a[i]='\0';i++)
else { if(a[i]=b[i])
{printf("\n string is not a pallindrome"); } { f=0; break;}}
getch();} if(f==0)
printf("string is not
pallindrome");
else
printf("string is
pallindrome");
getch();
}

CHANDIGARH UNIVERSITY, GHARUAN
31 Name: Puneet Goyal
UID: 13BCS1193

CHANDIGARH UNIVERSITY, GHARUAN
32 Name: Puneet Goyal
UID: 13BCS1193
Program-21
Write a program to read & print the record of 15 students
using structures .
Syntax:-
#include<conio.h>
#include<stdio.h>
struct student
{
int roll_no;
char name [20];
char course [20];
};
void main( )
{
struct student stud[15];
int i,j;
clrscr();
printf ("Enter the data for all the students:\n");
for (i=0;i<=14;i++)
{
printf ("Enter the roll number of %d student=",i+1);
scanf ("%d",&stud[i].roll_no);
printf("Enter the name of %d student=",i+1);
scanf ("%s",stud[i].name);
printf ("Enter the course of %d student=",i+1);
scanf ("%s",stud[i].course);
}
printf ("The data you have entered is as follows:\n");
for (i=0;i<=14;i++)
{
printf ("The %d the student's detail is %d %s
%s\n",i+1,stud[i].roll_no,stud[i].name,stud[i].course);
}
getch();
}

CHANDIGARH UNIVERSITY, GHARUAN
33 Name: Puneet Goyal
UID: 13BCS1193