0% found this document useful (0 votes)
229 views

C Program PDF

This document contains 13 code snippets demonstrating C programming concepts like functions, arrays, structures, data types, loops, and conditionals. Each snippet is 1-2 pages and includes a brief description, code to calculate areas of shapes, add numbers, or matrices, check for Armstrong numbers, or include an array within a structure. The document is a study guide from Gyan Group Institute providing examples of core C programming topics.

Uploaded by

vasundhra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
229 views

C Program PDF

This document contains 13 code snippets demonstrating C programming concepts like functions, arrays, structures, data types, loops, and conditionals. Each snippet is 1-2 pages and includes a brief description, code to calculate areas of shapes, add numbers, or matrices, check for Armstrong numbers, or include an array within a structure. The document is a study guide from Gyan Group Institute providing examples of core C programming topics.

Uploaded by

vasundhra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 194

GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE

AND TECHNOLOGY

OPOSITE POLICE STATION, MAIN ROAD RANJHI JABALPUR


CONTACT NO. 9174879794, 9479638126, 0761-2431352,
EMAIL ID:- [email protected] Page 1
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

1. Add n Numbers
main()
{
int n;
int a;
int i;
int sum;
sum=0;
clrscr();
printf("Enter The Number Of Integers You Want To Add :");
scanf("%d",&n);
printf("\nEnter %d Numbers :",n);
for(i=1;i<=n;i++)
{
scanf("%d",&a);
sum=sum+a;
}
printf("\nRequired Sum :%d",sum);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 2
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

2. Add Two Matrix


main()
{
int r;
int c;
int A[2][2];
int B[2][2];
int C[2][2];
clrscr();
printf("\nEnter Elements Of First Matrix :");
for(r=0;r<=1;r++)
for(c=0;c<=1;c++)
{
scanf("%d",&A[r][c]);
}
printf("\n\nEnter Elements Of Second Matrix :");
for(r=0;r<=1;r++)
for(c=0;c<=1;c++)
{
scanf("%d",&B[r][c]);
}
printf("\n\nDisplay First Matrix :");
for(r=0;r<=1;r++)
for(c=0;c<=1;c++)
{
printf(" %d",A[r][c]);
}
printf("\n\nDisplay Second Matrix :");
for(r=0;r<=1;r++)
for(c=0;c<=1;c++)
{
printf(" %d",B[r][c]);
}
for(r=0;r<=1;r++)
for(c=0;c<=1;c++)
{
C[r][c]=A[r][c]+B[r][c];

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 3
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY
}
printf("\n\nDisplay sum Of Matrix :");
for(r=0;r<=1;r++)
for(c=0;c<=1;c++)
{
printf("\n\n %d",C[r][c]);
}
getch();

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 4
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

3. Add Two Numbers


main()
{
int a=10;
int b=20;
int c;
clrscr();
printf("Enter First Number :%d",a);
printf("\nEnter Second Number :%d",b);
c=a+b;
printf("\nSum :%d",c);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 5
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

4. Addition Of Matrices Using Array

main()
{
int row;
int col;
int r;
int c;
int A[100][100];
int B[100][100];
int C[100][100];
clrscr();
printf("\nEnter The Value Of 'r' :");
scanf("%d",&r);
printf("\nEnter The Value Of 'c' :");
scanf("%d",&c);
printf("\nEnter The Elements Of First Matrix :\n");
for(row=0;row<r;row++)
for(col=0;col<c;col++)
{
scanf("%d",&A[row][col]);
}
printf("\nEnter The Elements Of Second Matrix :\n");
for(row=0;row<r;row++)
for(col=0;col<c;col++)
{
scanf("%d",&B[row][col]);
}
printf("\nDisplay The Elements Of First Matrix :");
for(row=0;row<r;row++)
for(col=0;col<c;col++)
{
printf(" %d",A[row][col]);
}
printf("\n\nDisplay The Elements Of Second Matrix :");
for(row=0;row<r;row++)

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 6
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY
for(col=0;col<c;col++)
{
printf(" %d",B[row][col]);
}
for(row=0;row<r;row++)
for(col=0;col<c;col++)
{
C[row][col]=A[row][col]+B[row][col];
}
printf("\n\nSum Of Entered Matrix :\n");
for(row=0;row<r;row++)
{
for(col=0;col<c;col++)
printf("%d\t",C[row][col]);
printf("\n");
}
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 7
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

5. Addition Of Matrix Using Malloc


main()
{
int i;
int n;
int *a;
int *b;
int *c;
printf("How Many Elements In Each Array :\n");
scanf("%d",&n);
a=(int*)malloc(n*sizeof(int));
b=(int*)malloc(n*sizeof(int));
printf("\nEnter Elemnts Of First List :\n");
for(i=0;i<n;i++)
{
scanf("%d",a+i);
}
printf("\nEnter Elements Of Second List :\n");
for(i=0;i<n;++i)
{
scanf("%d",b+i);
}
for(i=0;i<n;i++)
{
*(c+i)=*(a+i) + *(b+i);
}
printf("\nResultant List Is :\n");
for(i=0;i<n;i++)
{
printf("%d\n",*(c+i));
}
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 8
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

6. All Types Of Functions


// Combined Forms Of Functions

square1()
{
int a;
int b;
clrscr();
printf("\nExample Of Fuction Without Argument");
printf("\nEnter Any Number :");
scanf("%d",&a);
b=a*a;
printf("Square Of The Number :%d",b);
getch();
}
square2(int a)
{
int b;
b=a*a;
printf("Square Of The Number :%d",b);
getch();
}
square3(int a)
{
int b;
b=a*a;
return(b);
}
main()
{
int a;
int b;
square1();
printf("\nExample Of A Function With Argument And No Return Value");
printf("\nEnter Any Number :");
scanf("%d",&a);
square2(a);

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 9
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY
printf("\nExample Of A Function With Argument And Return Value");
printf("\nEnter Any Number :");
scanf("%d",&a);
printf("xSquare Of The Number Is :%d",square3(a) );
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 10
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

7. Area Of Circle

main()
{
int a;
float b;
clrscr();
printf("DISPLAY AREA OF A CIRCLE :");
printf("\n------------------------");
printf("\nEnter Radius Of The Circle:");
scanf("%d",&a);
b=3.14*(a*a);
printf("\nArea Of The Circle :%f",b);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 11
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

8. Area Of Rectangle

main()
{
int a;
int b;
int c;
clrscr();
printf("DISPLAY AREA OF A RECTANGLE");
printf("\n---------------------------");
printf("\nEnter The Value Of Base :");
scanf("%d",&a);
printf("\nEnter The Value Of Height :");
scanf("%d",&b);
c=a*b;
printf("\nArea Of The Rectangle :%d",c);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 12
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

9. Area Of Square

main()
{
int a;
int b;
clrscr();
printf("DISPLAY THE AREA OF A SQUARE");
printf("\n-----------------------------");
printf("\nEnter The Value Of Side Of A Square :");
scanf("%d",&a);
b=a*a;
printf("\nArea Of The Square :%d",b);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 13
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

10. Area Of Triangle

#include<stdio.h>
main()
{
int a;
int b;
float Area;
clrscr();
printf("DISPLAY THE AREA OF A TRIANGLE");
printf("\n------------------------------");
printf("\nEnter The Value Of Base :");
scanf("%d",&a);
fflush(stdin);
printf("\nEnter The Value Of Height :");
scanf("%d",&b);
fflush(stdin);
Area=(a*b)/2;
printf("\nArea Of The Triangle :%f",Area);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 14
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

11. Armstrong Numbers Between N1 & N2


#include<stdio.h>
#include<math.h>
main()
{
int low, high, i, temp1, temp2, remainder, n = 0, result = 0;
clrscr();
printf("Enter two numbers(intervals): ");
scanf("%d %d", &low, &high);
printf("Armstrong numbers between %d an %d are: ", low, high);
for(i = low + 1; i < high; ++i)
{
temp2 = i;
temp1 = i;
// number of digits calculation
while (temp1 != 0)
{
temp1 /= 10;
++n;
}
// result contains sum of nth power of its digits
while (temp2 != 0)
{
remainder = temp2 % 10;
result += pow(remainder, n);
temp2 /= 10;
}
// checks if number i is equal to the sum of nth power of its digits
if (result == i)
{
printf( " %d ", i);
}
// resetting the values to check Armstrong number for next iteration
n = 0;
result = 0;
}
getch();
return 0; }

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 15
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

12. Array Within Structure


//Array Within Struture

struct data
{
int a[10];
int sum;
};
main()
{
struct data key;
int i;
key.sum=0;
clrscr();
printf("\nEnter Number :");
for(i=0;i<=9;i++)
{
scanf("%d",&key.a[i]);
key.sum=key.sum+key.a[i];
}
printf("Sum :%d",key.sum);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 16
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

13. Auto Storage Class


main()
{
auto int i=10;
clrscr();
{
auto int i=20;
printf("\n %d",i);
}
printf("\n\n %d",i);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 17
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

14. Average Of n Numbers

main()
{
int n;
int i;
int a;
int sum;
clrscr();
sum=0;
printf("Enter The No. Of Integers You Want To Add :");
scanf("%d",&n);
printf("\nEnter %d Integers :\n",n);
for(i=1;i<=n;i++)
{
scanf("%d",&a);
sum=sum+a;
}
printf("\nAverage Of Entered Integers :%d",(sum/n));
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 18
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

15. Biodata Using Funct.


// Function Without Argument
add()
{
int a;
int b;
int c;
printf("\n\nEnter First Number :");
scanf("%d",&a);
printf("\nEnter Second Number :");
scanf("%d",&b);
c=a+b;
printf("\nSum :%d",c);
getch();
}
sub()
{
int a;
int b;
int c;
printf("\n\nEnter First Number :");
scanf("%d",a);
printf("\nEnter Second Number :");
scanf("%d",b);
c=a-b;
printf("\nDifference :%d",c);
getch();
}
main()
{
clrscr();
add();
sub();
printf("\n\nPrakhar Mor");
getch();
add();
sub();

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 19
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY
printf("\n\nRanjhi Guest House");
getch();
add();
sub();
printf("\n\nElectronics And Communication Engineering");
getch();
add();
sub();
printf("\n\nJabalpur Engineering College");
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 20
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

16. Biodata With Messages


main()
{
int a;
int b;
int c;
int i;
clrscr();
printf("\n\nEnter First Number :");
scanf("%d",&a);
printf("\nEnter Second Number :");
scanf("%d",&b);
c=a+b;
printf("\nSum :%d",c);
getch();
printf("\n\nPrakhar Mor");
printf("\n\nEnter First Number :");
scanf("%d",&a);
printf("\nEnter Second Number :");
scanf("%d",&b);
c=a+b;
printf("\nSum :%d",c);
getch();
printf("\n\nRanjhi Guest House");
printf("\n\nEnter First Number :");
scanf("%d",&a);
printf("\nEnter Second Number :");
scanf("%d",&b);
c=a+b;
printf("\nSum :%d",c);
getch();
printf("\n\nElectronics And Communication Engineering");
printf("\n\nEnter First Number :");
scanf("%d",&a);
printf("\nEnter Second Number :");
scanf("%d",&b);

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 21
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY
c=a+b;
printf("\nSum :%d",c);
getch();
printf("\n\nJabalpur Engineering College");
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 22
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

17. Break & Continue

main()
{
int a;
clrscr();
for(a=1;a<=20;a++)
{
if(a==10)
break;
printf(" %d",a);
}
printf("\n");
for(a=1;a<=20;a++)
{
if(a==10)
continue;
printf(" %d",a);
}
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 23
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

18. Calculate Percentage

main()
{
int a;
int b;
int c;
int d;
float p;
clrscr();
printf("Enter Your Mathematics Marks :");
scanf("%d",&a);
printf("Enter Your Chemistry Marks :");
scanf("%d",&b);
printf("Enter Your Physics Marks :");
scanf("%d",&c);
printf("Enter Your English Marks :");
scanf("%d",&d);
p=(a+b+c+d)/4;
printf("Your Percentage Is :%f",p);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 24
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

19. Call By Reference


prakhar(int *a,int *b)
{
*a=*a+1;
*b=*b+1;
printf("\nResultant:%d & %d",*a,*b);
}
main()
{
int a;
int b;
clrscr();
printf("\nEnter First Number :");
scanf("%d",&a);
printf("\nEnter Second Number :");
scanf("%d",&b);
printf("\nInput :%d & %d",a,b);
prakhar(&a,&b);
printf("\nInput :%d & %d",a,b);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 25
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

20. Call By Value

prakhar(int a,int b)
{
a=a+1;
b=b+1;
printf("\nResultant:%d & %d",a,b);
}
main()
{
int a;
int b;
clrscr();
printf("\nEnter First Number :");
scanf("%d",&a);
printf("\nEnter Second Number :");
scanf("%d",&b);
printf("\nInput :%d & %d",a,b);
prakhar(a,b);
printf("\nInput :%d & %d",a,b);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 26
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

21. Check Given String Is Palindrome Or Not

main()
{
char a[100],b[100];
clrscr();
printf("Enter The String You Want To Check :");
gets(a);
strcpy(b,a);
strrev(b);
if(strcmp(a,b)==0)
printf("\nEntered String Is A Palindrome");
else
printf("\nEntered String Is Not A Palindrome");
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 27
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

22. Check Leap Year


main()
{
int a;
clrscr();
printf("Enter The Year You Want To Check :");
scanf("%d",&a);
if(a%4==0)
printf("It Is A Leap Year");
else
printf("It Is Not A Leap Yar");
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 28
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

23. Check No. Of Even And Odd Numbers

main()
{
int i,num;
int ec=0,oc=0;
clrscr();
printf("\nEnter Ten Numbers :\n");
for(i=0;i<10;i++)
{
scanf("%d",&num);
if(num%2==0)
{
ec++;
}
else
{
oc++;
}
}
printf("\nThe Total Even Numbers Are :%d",ec);
printf("\nThe Total Odd Numbers Are :%d",oc);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 29
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

24. Check Odd And Even Number

main()
{
int a;
clrscr();
printf("Enter Any Number :");
scanf("%d",&a);
if(a%2==0)
printf("Entered Number Is Even");
else
printf("Entered Number Is Odd");
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 30
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

25. Check Vowel

main()
{
char p;
clrscr();
printf("Input A Character :");
scanf("%c",&p);
switch(p)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
printf("It Is A Vowel");
break;
default:
printf("It Is Not A Vowel");
}
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 31
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

26. Combination Of Array,Loop&Pointers

// You Have To Initialize Only First Value While Combining Array,loop & pointer
//p[0]=&A[0]

main()
{
int i;
int k=1;
int A[6];
int *p[6];
p[0]=&A[0];
*p[5]=0;
clrscr();
for(i=0;i<=4;i++)
{
printf("\nEnter %d Number :",k);
scanf("%d",&*p[i]);
*p[5]=*p[5] + *p[i];
k++;
}
printf("\nSum :%d",*p[5]);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 32
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

27. Combination Of Structure & Pointers

//Combination Of Structure & Pointers


struct data
{
int a;
int b;
int c;
}
main()
{
struct data *key;
clrscr();
printf("Enter First Number :");
scanf("%d",&key->a);
printf("\nEnter Second Numnber :");
scanf("%d",&key->b);
key->c=key->a+key->b;
printf("\nSum :%d",key->c);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 33
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

28. Count No. Of Digits

main()
{
int a;
int b;
clrscr();
b=0;
printf("Enter Any Number :");
scanf("%d",&a);
while(a>0)
{
a=a/10;
b=b+1;
}
printf("Number Of Digits Of The Given Number :%d",b);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 34
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

29. Display One Number Ahead Using Funct.

prakhar1(int a)
{
a=a+1;
printf("\nResultant Is :%d",a);
getch();
}
prakhar2(int b)
{
b=b+1;
printf("\nResultant Is :%d",b);
getch();
}
main()
{
int a;
int b;
clrscr();
printf("\nEnter First Number :");
scanf("%d",&a);
prakhar1(a);
printf("\n\nEnter Second Number :");
scanf("%d",&b);
prakhar2(b);
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 35
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

30. Display Add,Sub,Mul & Div

main()
{
float a;
float b;
float c;
float d;
float e;
float f;
clrscr();
printf("Enter First Number :");
scanf("%f",&a);
printf("\nEnter Second Number :");
scanf("%f",&b);
printf("\nTwo Numbers Are :%f %f",a,b);
c=a+b;
d=a-b;
e=a*b;
f=a/b;
printf("\nSum=%f",c);
printf("\nDifference=%f",d);
printf("\nMultiplication=%f",e);
printf("\nDivision=%f",f);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 36
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

31. Display Biodata Using Gets


add()
{
int a;
int b;
int c;
printf("\n\nEnter First Number :");
scanf("%d",&a);
printf("\nEnter Second Number :");
scanf("%d",&b);
c=a+b;
printf("\nSum :%d",c);
getch();
}
main()
{
clrscr();
add();
printf("\n\nPrakhar Mor");
getch();
add();
printf("\n\nRanjhi Guest House");
getch();
add();
printf("\n\nElectronics And Communication Engineering");
getch();
add();
printf("\n\nJabalpur Engineering College");
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 37
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

32. Display Cube Of First 10 Numbers

main()
{
int i;
clrscr();
for(i=1;i<=10;i++)
{
printf(" %d",i*i*i);
}
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 38
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

33. Display Int,Dec & Char Using Structure


struct data
{
int a;
float b;
char c;
};
main()
{
struct data key;
clrscr();
key.a=10;
printf(" %d",key.a);
key.b=8.8;
printf(" %f",key.b);
key.c='P';
printf(" %c",key.c);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 39
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

34. Display Name,Age & Gender Using Gets


#include<stdio.h>
main()
{
char a[50];
int b;
char c;
clrscr();
printf("Enter Your Name :");
gets(a);
printf("Enter Your Age :");
scanf("%d",&b);
fflush(stdin);
printf("Enter Your Gender :");
scanf("%c",&c);
fflush(stdin);
printf("\nYOUR DETAILS ARE :");
printf("\nName :%s",a);
printf("\nAge :%d",b);
printf("\nGender :%c",c);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 40
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

35. Display No. Of Digits

main()
{
int a;
int b;
clrscr();
b=0;
printf("Enter Any Number :");
scanf("%d",&a);
while(a>0)
{
a=a/10;
b=b+1;
}
printf("Number Of Digits Of The Given Number :%d",b);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 41
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

36. Display Square Of First 10 Numbers


main()
{
int i;
clrscr();
for(i=1;i<=10;i++)
{
printf(" %d",i*i);
}
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 42
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

37. Display Sum 3 Times Using For


main()
{
int a;
int b;
int c;
int i;
clrscr();
for(i=0;i<=3;i++)
{
printf("\n\nEnter First Number :");
scanf("%d",&a);
printf("\nEnter Second Number :");
scanf("%d",&b);
c=a+b;
printf("\nSum :%d",c);
}
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 43
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

38. Display Sum Of 10 No. Using Struct.


struct data
{
int a,b,c,d,e,f,g,h,i,j,k,l;
};
main()
{
struct data key;
clrscr();
printf("\nEnter First Number :");
scanf("%d",&key.a);
printf("\nEnter Second Number :");
scanf("%d",&key.b);
printf("\nEnter Third Number :");
scanf("%d",&key.c);
printf("\nEnter Fourth Number :");
scanf("%d",&key.d);
printf("\nEnter Fifth Number :");
scanf("%d",&key.e);
printf("\nEnter Sixth Number :");
scanf("%d",&key.f);
printf("\nEnter Seventh Number :");
scanf("%d",&key.g);
printf("\nEnter Eigth Number :");
scanf("%d",&key.i);
printf("\nEnter Ninth Number :");
scanf("%d",&key.j);
printf("\nEnter Tenth Number :");
scanf("%d",&key.k);
key.l=key.k+key.a+key.b+key.c+key.d+key.e+key.f+key.g+key.h+key.i+key.j;
printf("Sum :%d",key.l);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 44
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

39. Display Sum Of Numbers From 1 To 10 Using Do While

main()
{
int i;
int sum=0;
clrscr();
i=1;
do
{
sum=sum+i;
i++;
}while(i<=10);
printf("\nThe Sum Of Numbers From 1 To 10 Is :%d",sum);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 45
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

40. Display Sum Of Square Of Series

//Print Sum Of Square Of A Series


main()
{
int i;
int n;
int sum;
clrscr();
sum=0;
printf("Enter The Value Of 'n' :");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+i*i;
}
printf("Required Sum :%d",sum);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 46
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

41. Display Sum Using Array & Loop


main()
{
int a[6];
int i=0;
int k=1;
a[5]=0;
clrscr();
while(i<=4)
{
printf("Enter %d Number :",k);
scanf("%d",&a[i]);
a[5]=a[5]+a[i];
k++;
i++;
}
printf("\n\nSum :%d",a[5]);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 47
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

42. Display Sum Using Array,Loop&Pointer


//Dislaying Sum Of 5 Numbers Using Array,Loop And Pointer

main()
{
int a;
int b;
int c;
int d;
int e;
int f;
int k=1;
int i;
int *p1;
int *p2;
int *p3;
int *p4;
int *p5;
int *p6;
int A[5];
int B[6];
B[5]=0;
p1=&a;
p2=&b;
p3=&c;
p4=&d;
p5=&e;
p6=&f;
clrscr();
printf("DISPLAYING SUM OF 5 NUMBERS USING ARRAY :");
printf("\n----------------------------------------");
printf("\n\nEnter First Number :");
scanf("%d",&A[0]);
printf("\nEnter Second Number :");
scanf("%d",&A[1]);
printf("\nEnter Third Number :");
scanf("%d",&A[2]);

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 48
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY
printf("\nEnter Fourth Number :");
scanf("%d",&A[3]);
printf("\nEnter Fifth Number :");
scanf("%d",&A[4]);
A[5]=A[0]+A[1]+A[2]+A[3]+A[4];
printf("\nSum :%d",A[5]);
printf("\n\nDISPLAYING SUM OF 5 NUMBERS USING LOOP");
printf("\n--------------------------------------");
for(i=0;i<=4;i++)
{
printf("\nEnter %d Number :",k);
scanf("%d",&B[i]);
B[5]=B[5]+B[i];
k++;
}
printf("\nSum :%d",B[5]);
printf("\n\nDISPLAYING SUM OF 5 NUMBERS USING POINTERS");
printf("\n------------------------------------------");
printf("\n\nEnter First Number :");
scanf("%d",&*p1);
printf("\nEnter Second Number :");
scanf("%d",&*p2);
printf("\nEnter Third Number :");
scanf("%d",&*p3);
printf("\nEnter Fourth Number :");
scanf("%d",&*p4);
printf("\nEnter Fifth Number :");
scanf("%d",&*p5);
*p6=*p1 + *p2 + *p3 + *p4 + *p5;
printf("\nSum :%d",*p6);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 49
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

43. Display Sum Using Array

main()
{
int a[6];
clrscr();
printf("Enter First Number :");
scanf("%d",&a[0]);
printf("\nEnter Second Number :");
scanf("%d",&a[1]);
printf("\nEnter Third Number :");
scanf("%d",&a[2]);
printf("\nEnter Fourth Number :");
scanf("%d",&a[3]);
printf("\nEnter Fifth Number :");
scanf("%d",&a[4]);
a[5]=a[0]+a[1]+a[2]+a[3]+a[4];
printf("\n\nSum :%d",a[5]);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 50
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

44. Display Sum Using Pointers


main()
{
int a;
int b;
int c;
int *p1;
int *p2;
int *p3;
p1=&a;
p2=&b;
p3=&c;
clrscr();
printf("Enter First Number :");
scanf("%d",&*p1);
printf("\nEnter Second Number :");
scanf("%d",&*p2);
*p3 = *p1 + *p2;
printf("\nSum :%d",*p3);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 51
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

45. Display Sum Using Structure


struct data
{
int a;
int b;
int c;
};
main()
{
struct data key;
clrscr();
printf("Enter First Number :");
scanf("%d",&key.a);
printf("\nEnter Second Number :");
scanf("%d",&key.b);
key.c=key.a+key.b;
printf("\nSum :%d",key.c);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 52
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

46. Display Sum,Diff,Mult,Div Using Pointers


main()
{
float a;
float b;
float c;
float d;
float e;
float f;
float *p1;
float *p2;
float *p3;
float *p4;
float *p5;
float *p6;
p1=&a;
p2=&b;
p3=&c;
p4=&d;
p5=&e;
p6=&f;
clrscr();
printf("Enter First Number :");
scanf("%f",&*p1);
printf("\nEnter Second Number :");
scanf("%f",&*p2);
*p3 = *p1 + *p2;
*p4 = *p1 - *p2;
*p5 = *p1 * *p2;
*p6 = *p1 / *p2;
printf("\nSum :%f",*p3);
printf("\nDifference :%f",*p4);
printf("\nMultiplication :%f",*p5);
printf("\nDivision :%f",*p6);
getch();

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 53
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

47. Display Table Of Given Number


#include<stdio.h>
main()
{
int a;
int i;
int t;
clrscr();
printf("Enter Table Value :");
scanf("%d",&a);
fflush(stdin);
printf("\nTable Of %d:",a);
i=1;
while(i<=10)
{
t=a*i;
printf("\n\n%d",t);
i=i+1;
}
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 54
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

48. Displaying Sum Using Array,Loop & Pointer


//Dislaying Sum Of 5 Numbers Using Array,Loop And Pointer

main()
{
int C[6];
int k=1;
int i;
int *p[6];
int A[5];
int B[6];
B[5]=0;
p[0]=&C[0];
p[1]=&C[1];
p[2]=&C[2];
p[3]=&C[3];
p[4]=&C[4];
p[5]=&C[5];
clrscr();
printf("DISPLAYING SUM OF 5 NUMBERS USING ARRAY :");
printf("\n----------------------------------------");
printf("\n\nEnter First Number :");
scanf("%d",&A[0]);
printf("\nEnter Second Number :");
scanf("%d",&A[1]);
printf("\nEnter Third Number :");
scanf("%d",&A[2]);
printf("\nEnter Fourth Number :");
scanf("%d",&A[3]);
printf("\nEnter Fifth Number :");
scanf("%d",&A[4]);
A[5]=A[0]+A[1]+A[2]+A[3]+A[4];
printf("\nSum :%d",A[5]);
printf("\n\nDISPLAYING SUM OF 5 NUMBERS USING LOOP");
printf("\n--------------------------------------");
for(i=0;i<=4;i++)
{

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 55
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY
printf("\nEnter %d Number :",k);
scanf("%d",&B[i]);
B[5]=B[5]+B[i];
k++;
}
printf("\nSum :%d",B[5]);
printf("\n\nDISPLAYING SUM OF 5 NUMBERS USING POINTERS");
printf("\n------------------------------------------");
printf("\n\nEnter First Number :");
scanf("%d",&*p[0]);
printf("\nEnter Second Number :");
scanf("%d",&*p[1]);
printf("\nEnter Third Number :");
scanf("%d",&*p[2]);
printf("\nEnter Fourth Number :");
scanf("%d",&*p[3]);
printf("\nEnter Fifth Number :");
scanf("%d",&*p[4]);
*p[5]=*p[0] + *p[1] + *p[2] + *p[3] + *p[4];
printf("\nSum :%d",*p[5]);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 56
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

49. Displaying The Adress In Memory


main()
{
int a=8;
int b=9;
float c=8.8;
float d=9.9;
char e='P';
char f='M';
char g[50]="Prakhar";
char h[50]="Mor";
int *p1;
int *p2;
float *p3;
float *p4;
char *p5;
char *p6;
char *p7;
char *p8;
p1=&a;
p2=&b;
p3=&c;
p4=&d;
p5=&e;
p6=&f;
p7=&g;
p8=&h;
clrscr();
printf("\n\nAdress Of a=%u : Value of a=%d",p1,*p1);
printf("\n\nAdress Of b=%u : Value of b=%d",p2,*p2);
printf("\n\nAdress Of c=%u : Value of c=%f",p3,*p3);
printf("\n\nAdress Of d=%u : Value Of d=%f",p4,*p4);
printf("\n\nAdress Of e=%u : Value Of e=%c",p5,*p5);
printf("\n\nAdress Of f=%u : Value Of f=%c",p6,*p6);
printf("\n\nAdress Of g=%u : Value Of g=%c",p7,*p7);
printf("\n\nAdress Of h=%u : Value Of h=%c",p8,*p8);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 57
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

50. Example Of 1d,2d&3d Array Using For


main()
{
int r;
int c;
int t;
int A[2]={10,20};
int B[2][2]={10,20,30,40};
int C[2][2][2]={10,20,30,40,50,60,70,80};
clrscr();
printf("Example Of 1d Array :");
for(r=0;r<=1;r++)
{
printf(" %d",A[r]);
}
getch();
printf("\nExample Of 2d Array :");
for(r=0;r<=1;r++)
for(c=0;c<=1;c++)
{
printf(" %d",B[r][c]);
}
getch();
printf("\nExample Of 3d Array :");
for(r=0;r<=1;r++)
for(c=0;c<=1;c++)
for(t=0;t<=1;t++)
{
printf(" %d",C[r][c][t]);
}
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 58
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

51. Example Of 1d,2d&3d Array Using Scanf


main()
{
int A[2];
int B[2][2];
int C[2][2][2];
clrscr();
printf("\n Example Of 1d Array :");
scanf("%d %d",&A[0],A[1]);
printf("\n\n Example Of 2d Array :");
scanf("%d %d %d %d",&B[0][0],B[0][1],B[1][0],B[1][1]);
printf("\n\n Example Of 3d Array :");
scanf("%d %d %d %d %d %d %d
%d",&C[0][0][0],C[0][0][1],C[0][1][0],C[0][1][1],C[1][0][0],C[1][0][1],C[1][1][0],C[1][1][1]);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 59
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

52. Example Of 1d,2d&3d Array


main()
{
int A[2]={10,20};
int B[2][2]={10,20,30,40};
int C[2][2][2]={10,20,30,40,50,60,70,80};
clrscr();
printf("\n Example Of 1d Array :%d %d",A[0],A[1]);
printf("\n\n Example Of 2d Array :%d %d %d %d",B[0][0],B[0][1],B[1][0],B[1][1]);
printf("\n\n Example Of 3d Array :%d %d %d %d %d %d %d
%d",C[0][0][0],C[0][0][1],C[0][1][0],C[0][1][1],C[1][0][0],C[1][0][1],C[1][1][0],C[1][1][1]);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 60
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

53. Example Of Increment & Decrement


main()
{
int a=2;
clrscr();
printf("EXAMPLE OF PRE-INCREMENT :");
printf("-------------------------");
printf("Output Is :%d",++a);
printf("EXAMPLE OF POST-INCREMENT :");
printf("--------------------------");
printf("Output Is :%d",a++);//No Output
printf("EXAMPLE OF PRE-DECREMENT :");
printf("-------------------------");
printf("Output Is :%d",--a);
printf("EXAMPLE OF POST-DECREMENT :");
printf("Output Is :%d",a--);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 61
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

54. External Storage Class


extern int i=10;
main()
{
int i=20;
clrscr();
printf("\n %d",i);
show();
getch();
}
show()
{
printf("\n %d",i);
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 62
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

55. Factorial
main()
{
int n;
int r;
float f;
clrscr();
printf("Enter The Values Of N And R :\n");
scanf("%d %d",&n,&r);
f=fact(n)/fact(n-r);
printf("npr Value Is :%5.2f",f);
getch();
}
fact(int n)
{
int i;
int f=1;
for(i=1;i<=n;i++)
f=f*i;
return(f);
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 63
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

56. Fibonacci Series

main()
{
int n;
int first=0;
int second=1;
int next;
int c;
clrscr();
printf("Enter Number Of Terms :");
scanf("%d",&n);
printf("\nFirst %d Terms Of Fibonacci Series Are :",n);
for(c=0;c<n;c++)
{
if(c<=1)
next=c;
else
{
next=first+second;
first=second;
second=next;
}
printf("%d",next);
}
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 64
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

57. Find Armstrong Number


//153=cube of(1+3+5)
main()
{
int num;
int i;
int dig,cube,sum=0;
clrscr();
printf("Enter A Number :");
scanf("%d",&num);
for(i=num;i>0;i=i/10)
{
dig=i%10;
cube=dig*dig*dig;
sum=sum+cube;
}
if(sum==num)
{
printf("%d Is An Armstrong Number",num);
}
else
{
printf("%d Is Not An Armstrong Number",num);
}
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 65
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

58. Find Factorial


main()
{
long int num,i;
long int fac=1;
clrscr();
printf("Enter A Number :");
scanf("%ld",&num);
for(i=num;i>0;i--)
{
fac=fac*i;
}
printf("The Factorial Value For %ld Is %ld",num,fac);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 66
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

59. Find Palindrome Number


main()
{
int num,i;
int dig,j=0;
clrscr();
printf("Enter A Number :");
scanf("%d",&num);
for(i=num;i>0;i=i/10)
{
dig=i%10;
j=j*10+dig;
}
if(j==num)
{
printf("\n%d Is A Palindrome",num);
}
else
{
printf("\n%d Is Not A Palindrome",num);
}
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 67
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

60. Find Smallest Of 10 Numbers


main()
{
int num,i,temp;
clrscr();
i=0;
printf("Enter A Number :");
scanf("%d",&num);
temp=num;
while(i<9)
{
printf("\nEnter A Number :");
scanf("%d",&num);
if(temp>num);
{
temp=num;
}
i++;
}
printf("\n\nThe Smallest Number Is :%d",temp);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 68
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

61. Funct. With Arg. & No Return Value

// Function With Argument And No Return Value


add(int a,int b)
{
int c;
c=a+b;
printf("Sum :%d",c);
getch();
}
main()
{
int a;
int b;
clrscr();
printf("Enter First Number :");
scanf("%d",&a);
printf("Enter Second Number :");
scanf("%d",&b);
add(a,b);
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 69
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

62. Funct. With Arg. & Return Value


// Function With Argument And Return Value

add(int a,int b)
{
int c;
c=a+b;
return(c);
}
main()
{
int a;
int b;
clrscr();
printf("Enter First Number :");
scanf("%d",&a);
printf("\nEnter Second Number :");
scanf("%d",&b);
printf("\nSum :%d",add(a,b));
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 70
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

63. Function-Add
add()
{
int a;
int b;
int c;
clrscr();
printf("Enter First Number :");
scanf("%d",&a);
printf("\nEnter Second Number :");
scanf("%d",&b);
c=a+b;
printf("\nSum :%d",c);
getch();
}
main()
{
add();
add();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 71
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

64. Function-Pline
pline()
{
printf("-----------");
}
main()
{
clrscr();
pline();
printf("\nPrakhar Mor");
printf("\n");
pline();
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 72
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

65. Function-Square
square()
{
int a;
int b;
clrscr();
printf("Type Any Number :");
scanf("%d",&a);
b=a*a;
printf("\nSquare Of The Number Is :%d",b);
getch();
}
main()
{
square();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 73
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

66. Greatest Of 3 Numbers Using Conditional Operator


//To Find The Greatest Of Three Numbers By Conditional Operators
main()
{
int x;
int y;
int z;
int n1;
int n2;
clrscr();
printf("Enter Three Numbers :\n");
scanf("%d %d %d",&x,&y,&z);
n1=x>y?x:y;
n2=n1>z?n1:z;
printf("\nThe Biggest Number Is :%d",n2);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 74
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

67. HCF And LCM


main()
{
int a, b, x, y, t, gcd, lcm;
printf("Enter Two Integers\n");
scanf("%d%d", &x, &y);
a = x;
b = y;
while (b != 0)
{
t = b;
b = a % b;
a = t;
}
gcd = a;
lcm = (x*y)/gcd;
printf("Greatest Common Divisor Of %d And %d = %d\n", x, y, gcd);
printf("Least Common Multiple Of %d And %d = %d\n", x, y, lcm);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 75
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

68. If Else
main()
{
float p;
clrscr();
printf("Enter Your Percentage :");
scanf("%f",&p);
if(p>95)
printf("Pass");
else
printf("Fail");
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 76
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

69. If,Else if & Else


main()
{
float p;
clrscr();
printf("Enter Your Percentage :");
scanf("%f",&p);
if(p>90)
{
printf("First Division");
printf("Congratulations..");
}
else if(p>80)
printf("Second Division");
else if(p>70)
printf("Third Division");
else if(p>60)
printf("Fourth Division");
else if(p>50)
printf("Fifth Division");
else if(p>40)
printf("Sixth Division");
else
{
printf("Fail");
printf("Needs Improvement");
}
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 77
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

70. Job Selection


#include<stdio.h>
main()
{
float a;
int b;
char c;
clrscr();
printf("Enter Your Percentage :");
scanf("%f",&a);
fflush(stdin);
printf("\nEnter Your Age :");
scanf("%d",&b);
fflush(stdin);
printf("\nEnter Your Gender :");
scanf("%c",&c);
fflush(stdin);
printf("\nYOUR DETAILS ARE :-");
printf("\nPercentage :%f",a);
printf("\nAge :%d",b);
printf("\nGender :%c",c);
if( (b>=18 && b<=25 && c=='F')||(a>95) )
{
printf("\nCongratulations...");
printf("\nYou Are Selected For The Job");
}
else
printf("\nYou Are Not Selected For The Job");
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 78
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

71. Loop(For)
main()
{
int i;
clrscr();
printf("Print All Natural Numbers Upto 10 :");
for(i=1;i<=10;i=i+1)
{
printf(" %d",i);
}
printf("\n\nPrint All Odd Numbers Upto 10 :");
for(i=1;i<=10;i=i+2)
{
printf(" %d",i);
}
printf("\n\nPrint All Even Numbers Upto 10 :");
for(i=2;i<=10;i=i+2)
{
printf(" %d",i);
}
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 79
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

72. Loop(While)
main()
{
int i=1;
clrscr();
printf("Display Hello 10 Times :\n\n");
while(i<=10)
{
printf(" Hello");
i=i+1;
}
i=1;
printf("\n\nDisplay First 10 Natural Numbers :\n\n");
while(i<10)
{
printf(" %d",i);
i++;
}
i=1;
printf("\n\nDisplay All Odd Numbers Upto 10 :\n\n");
while(i<10)
{
printf(" %d",i);
i=i+2;
}
i=2;
printf("\n\nDisplay All Even Numbers Upto 10 :\n\n");
while(i<10)
{
printf(" %d",i);
i=i+2;
}
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 80
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

73. Matrix Multiplication


#include<stdio.h>
#include<conio.h>
void main()
{
int A[3][3];
int B[3][2];
int C[3][2];
int i,j,k;
clrscr();
printf("please enter elements for a 3*3 matrix:\n");
for(i=0;i<=2;i++)
{
printf("enter element at A[%d][0] shell\t",i);
scanf("%d",&A[i][0]);
printf("enter element at A[%d][1] shell\t",i);
scanf("%d",&A[i][1]);

printf("enter element at A[%d][2] shell\t",i);


scanf("%d",&A[i][2]);
}
printf("\nthe first matrix is:");
for(i=0;i<=2;i++)

printf("\n%d\t%d\t%d\t",A[i][0],A[i][1],A[i][2]);
printf("\n\nplease entr elements for a 3*2matrix:\n");
for(i=0;i<=2;i++)
{

printf("enter element at B[%d][0] shell\t",i);


scanf("%d",&B[i][0]);
printf("enter element at B[%d][0] shell\t",i);
scanf("%d",&B[i][1]);

}
printf("\n the second matrix is:");
for(i=0;i<=2;i++)

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 81
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY
printf("\n%d\t%d\t",B[i][0],B[i][1]);
for(i=0;i<=2;i++)
{
for(j=0;j<=1;j++)
{
C[i][j]=0;
for(k=0;k<=2;k++)
C[i][j]=C[i][j]+A[i][k]*B[j][k];
}
}
printf("\n\nmultiplied matrix is:");
for(i=0;i<=2;i++)
printf("\n%d\t%d\t",C[i][0],C[i][1]);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 82
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

74. Nested If
main()
{
int date;
clrscr();
printf("Enter The Date :");
scanf("%d",&date);
if(date>=1)
{
if(date<=31)
{
printf("\nThe Entered Date Is Valid");
if(date==24)
{
printf("\nYes!Today Is Special");
}
}
}
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 83
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

75. Nestee For


main()
{
int r;
int c;
clrscr();
printf("Example Of Nestee For :");
for(r=1;r<=3;r++)
for(c=1;c<=4;c++)
{
printf("\n\nWhen r=%d,Then c=%d",r,c);
}
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 84
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

76. Operations Of String


main()
{
char a[50];
char b[50]="Ranjhi";
char c[50];
int i;
clrscr();
printf("Enter Your Name :");
gets(a);
i=strlen(a);//String Length(Used To Find The Length Of String)
strcat(b,a);//String Congant(Used To Add Two String)
strcpy(c,a);//String Copy(Used To Copy One String To Another)
printf("\nYour Name Is :%s",a);
printf("\n\nSum Of 'b' & 'a' Is :%s",b);
printf("\n\nCopy Of 'a' In 'c' Is :%s",c);
printf("\n\nLength Of String 'a' Is :%d",i);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 85
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

77. Operations Using Switch Case


#include<stdio.h>
main()
{
int a;
int b;
int c;
char d;
clrscr();
printf("Enter First Number :");
scanf("%d",&a);
fflush(stdin);
printf("\nEnter Second Number :");
scanf("%d",&b);
fflush(stdin);
printf("\nEnter '+' or '-' or '*' or '/' :");
scanf("%c",&d);
fflush(stdin);
switch(d)
{
case '+':
c=a+b;
printf("\nSum Is :%d",c);
break;
case '-':
c=a-b;
printf("\nDifference Is :%d",c);
break;
case '*':
c=a*b;
printf("\nMultiplication Is :%d",c);
break;
case '/':
c=a/b;
printf("\nDivision Is :%d",c);
break;

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 86
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY
default:
printf("\nWrong Choice");
}
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 87
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

78. Perfect Number


main()
{
int n,i=1,sum=0;
clrscr();
printf("Enter A Number :");
scanf("%d",&n);
while(i<n)
{
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("%d Is A Perfect Number",i);
else
printf("%d Is Not A Perfect Number",i);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 88
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

79. Print Age,Adress & Mo.No.


main()
{
char a[12]="Prakhar Mor";
int b=18;
char c[19]="Ranjhi Guest House";
int d=9425;
clrscr();
printf("Name-%s",a);
printf("\nAge-%d",b);
printf("\nAdress-%s",c);
printf("\nMobile No.-%d",d);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 89
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

80. Print Age,Name & Gender


#include<stdio.h>
main()
{
int a;
char b[50];
char c;
clrscr();
printf("Enter Your Age :");
scanf("%d",&a);
fflush(stdin);
printf("\nEnter Your Name :");
scanf("%s",&b);
fflush(stdin);
printf("\nEnter Your Gender :");
scanf("%c",&c);
fflush(stdin);
printf("YOUR DETAILS ARE :-");
printf("\nAge :%d",a);
printf("\nName :%s",b);
printf("\nGender :%c",c);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 90
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

81. Print From Start To End No.


main()
{
int i;
int b;
clrscr();
printf("Enter Start Number :");
scanf("%d",&i);
printf("Enter End Number :");
scanf("%d",&b);
while (i<b)
{
printf(" %d",i);
i=i+1;
}
Getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 91
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

82. Print Hello


//Print A Word "Hello"

main()
{
clrscr();
printf("Hello");
getch();
}
Print Int,Dec,Letter & Word

//Display Example Of Integer,Decimal,Letter And Word

main()
{
int a=10;
float b=8.8;
char c='P';
char d[12]="Prakhar Mor";
clrscr();
printf("Example Of An Integer :%d",a);
printf("\nExample Of A Decimal :%f",b);
printf("\nExample Of A Letter :%c",c);
printf("\nExample Of A Word :%s",d);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 92
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

83. Print Your Biodata


main()
{
clrscr();
printf("My Name Is Prakhar Mor");
printf("\nPress Any Key To Continue");
getch();
printf("\nRanjhi Guest House");
printf("\nPress Any Key To Continue");
getch();
printf("\nMobile No.-9425185958");
printf("\nPress Any Key To Continue");
getch();
printf("\nElectronics And Communication Engineering");
printf("\nPress Any Key To Continue");
getch();
printf("\nJabalpur Engineering College");
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 93
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

84. Register Storage Class


main()
{
register int i=10;
clrscr();
{
register int i=20;
printf("\n %d",i);
}
printf("\n\n %d",i);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 94
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

85. Reverse A Number


main()
{
long int num;
long int i;
int dig;
clrscr();
printf("Enter A Number :");
scanf("%ld",&num);
printf("\nThe Reverse Of Number %ld Is :",num);
for(i=num;i>0;i=i/10)
{
dig=i%10;
printf("%d",dig);
}
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 95
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

86. Reverse String


main()
{
char a[50];
clrscr();
printf("Enter A String To Reverse :");
gets(a);
strrev(a);
printf("Reverse Of Entered String Is :%s",a);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 96
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

87. Simple Interest

//Integer :2byte
//float :4byte
//character :1byte

main()
{
float a=8.8;
float b=9.9;
int c=8;
int d=9;
char e='P';
char f='M';
char g[70]="Prakhar";
char h[30]="Mor";
clrscr();
printf("\n\nAdress Of Integer c :");
printf("\n %d %u",c,&c);
printf("\n\nAdress Of Integer d :");
printf("\n %d %u",d,&d);
printf("\n\nAdress Of Decimal a :");
printf("\n %f %u",a,&a);
printf("\n\nAdress Of Decimal b :");
printf("\n %f %u",b,&b);
printf("\n\nAdress Of Character e :");
printf("\n %c %u",e,&e);
printf("\n\nAdress Of Character f :");
printf("\n %c %u",f,&f);
printf("\n\nAdress Of String g :");
printf("\n %s %u",g,&g);
printf("\n\nAdress Of String h :");
printf("\n %s %u",h,&h);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 97
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

88. Square Of A Given Number


#include<stdio.h>
main()
{
int num,square;
char ans='y';
clrscr();
while(ans=='y'||ans=='Y')
{
printf("Enter A Number :");
scanf("%d",&num);
square=num*num;
printf("The Square Of %d Is %d",num,square);
printf("\nDo You Wish To Enter Another Number (y For Yes) :");
fflush(stdin);
scanf("%c",&ans);
}
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 98
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

89. Square Root Of A Number


#include<math.h>
#include<stdio.h>
main()
{
long int num;
int root;
clrscr();
printf("Enter The Number You Want To Find The Square Root :");
scanf("%ld",&num);
root=sqrt(num);
printf("Square Root Of %ld Is :%d",num,root);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 99
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

90. Static Storage Class


prakhar()
{
static int a=1;
a=a+1;
printf("\nResultant Value :%d",a);
}
main()
{
clrscr();
prakhar();
prakhar();
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 100
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

91. Sum Of Digits Of A Number


main()
{
long int num,i;
int dig,sum=0;
clrscr();
printf("Enter A Number :");
scanf("%ld",&num);
for(i=num;i>0;i=i/10)
{
dig=i%10;
sum=sum+dig;
}
printf("\nThe Sum Of The Digits Of %ld Is :%d",num,sum);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 101
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

92. Sum Of n Num


main()
{
int n;
int i;
int a;
int sum;
clrscr();
sum=0;
printf("Enter The No. Of Integers You Want To Add :");
scanf("%d",&n);
printf("\nEnter %d Integers :\n",n);
for(i=1;i<=n;i++)
{
scanf("%d",&a);
sum=sum+a;
}
printf("\nSum Of Entered Integers :%d",sum);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 102
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

93. Sum Of Numbers Using 2d Array & For


main()
{
int r;
int c;
int A[2][2];
int Sum;
Sum=0;
clrscr();
printf("\nType Four Numbers :");
for(r=0;r<=1;r++)
for(c=0;c<=1;c++)
{
scanf("%d",&A[r][c]);
}
printf("\nDisplay Four Numbers :");
for(r=0;r<=1;r++)
for(c=0;c<=1;c++)
{
printf(" %d",A[r][c]);
Sum=Sum+A[r][c];
}
printf("\nSum=%d",Sum);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 103
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

94. Sum Of Numbers Using 3d Array & For


#include <stdio.h>
main()
{
int r;
int c;
int t;
int A[2][2][2];
int Sum;
Sum=0;
clrscr();
printf("\nEnter Eight Numbers :");
for(r=0;r<=1;r++)
for(c=0;c<=1;c++)
for(t=0;t<=1;t++)
{
scanf("%d",&A[r][c][t]);
fflush(stdin);
}
printf("\nDisplay Eight Numbers :");
for(r=0;r<=1;r++)
for(c=0;c<=1;c++)
for(t=0;t<=1;t++)
{
printf(" %d",A[r][c][t]);
Sum=Sum+A[r][c][t];
}
printf("\n\nSum :%d",Sum);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 104
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

95. Swapping Two Numbers


main()
{
int x,y,temp;
clrscr();
printf("Enter The Value Of x And y :\n");
scanf("%d %d",&x,&y);
printf("Before Swapping :\nx=%d\ny=%d\n",x,y);
temp=x;
x=y;
y=temp;
printf("After Swaping :\nx=%d\ny=%d\n",x,y);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 105
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

96. Switch Case


main()
{
int a;
int b;
clrscr();
printf("Enter Your Percentage :");
scanf("%d",&a);
b=a/10;
switch(b)
{
case 9:
case 8:
printf("Pass");
getch();
break;
default:
printf("Fail");
}
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 106
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

97. Temperature Conversion


main()
{
float c;
float f;
float k;
clrscr();
printf("TEMPERATURE CONVERSION");
printf("\n-----------------------");
printf("\nEnter The Temperature In Celcius :");
scanf("%f",&c);
f=(9*c)/5+32;
k=(c+273);
printf("\nTemperature In Fahrenhite :%f",f);
printf("\n\nTemperature In Kelvin :%f",k);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 107
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

98. Transpose Of A Matrix


main()
{
int m;
int n;
int r;
int c;
int matrix[10][10];
int transpose[10][10];
clrscr();
printf("\nEnter The No. Of Rows Of A Matrix :");
scanf("%d",&r);
printf("\nEnter The No. Of Columns Of A Matrix :");
scanf("%d",&c);
printf("\nEnter The Elements Of A Matrix :");
for(m=0;m<r;m++)
for(n=0;n<c;n++)
{
scanf("%d",&matrix[m][n]);
}
for(m=0;m<r;m++)
for(n=0;n<c;n++)
{
transpose[n][m]=matrix[m][n];
}
printf("Transpose Of The Matrix :");
for(m=0;m<r;m++)
for(n=0;n<c;n++)
{
printf("%d",transpose[n][m]);
}
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 108
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

99. Type Must Be Same,Name Need Not Be


// Type Must Be Same But Name Need Not Be
sub(int a,int b)
{
int c;
c=a-b;
return(c);
}
main()
{
int ff;
int hh;
clrscr();
printf("\nEnter First Number :");
scanf("%d",&ff);
printf("\nEnter Second Number :");
scanf("%d",&hh);
printf("\nDifference :%d",sub(ff,hh));
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 109
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

100. Use Of Do & While


main()
{
int i=1;
clrscr();
do
{
printf(" Hello");
i++;
}while(i<10);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 110
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

101. Use Of Getchar


main()
{
char a;
clrscr();
printf("Enter Any Character :");
a=getchar();
printf("Entered Character Is :%c",a);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 111
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

102. Use Of Gets


main()
{
char a[50];
clrscr();
printf("Enter Your Name :");
gets(a);
printf("\nYour Name Is :%s",a);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 112
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

103. Use Of Malloc & Realloc


main()
{
int n1;
int n2;
int i;
int *p;
clrscr();
printf("Enter The Size Of Array :");
scanf("%d",&n1);
p=(int*)malloc(n1*sizeof(int));
printf("\nAdress Of Previously Allocated Memory :");
for(i=0;i<n1;i++)
{
printf(" %u",p+i);
}
printf("\n\nEnter The Size Of New Array :");
scanf("%d",&n2);
p=realloc(p,n2);
printf("\nAdress Of New Allocated Memory :");
for(i=0;i<n2;i++)
{
printf(" %u",p+i);
}
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 113
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

104. Use Of Malloc


#include<stdio.h>
main()
{
int i;
int n;
int *ptr;
int sum;
sum=0;
clrscr();
printf("Enter No. Of Elements Of Array :");
scanf("%d",&n);
ptr=(int*)malloc(n*sizeof(int));
if(ptr==NULL)
{
printf("Error! Memory Not Allocated");
exit(0);
}
printf("Enter Elements Of Array :");
for(i=0;i<n;i++)
{
scanf("%d",ptr+i);
}
printf("Display Elements Of Array :");
for(i=0;i<n;i++)
{
printf(" %d",*(ptr+i));
}
for(i=0;i<n;i++)
{
sum=sum+*(ptr+i);
}
printf("\nSum :%d",sum);
getch();
free(ptr);
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 114
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

105. Use Of Strcmp


main()
{
int i;
char a[50];
char b[50]="Ranjhi";
clrscr();
printf("Enter Your Name :");
gets(a);
printf("Your Name Is :%s",a);
i=strcmp(b,a);
printf("\nDiferrence Between Strings 'a' & 'b' Is :%d",i);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 115
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

106. Use Of Union In Place Of Struct


//Union Prints Last Value In The All Preceeding Ones
union data
{
int a;
int b;
int c;
}
main()
{
union data key;
clrscr();
printf("Enter First Number :");
scanf("%d",&key.a);
printf("\nEnter Second Numnber :");
scanf("%d",&key.b);
key.c=key.a+key.b;
printf("\nSum :%d",key.c);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 116
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

107. Verification Of Password Using Strcmp


main()
{
char a[50]="maa";
char b[50];
int i;
clrscr();
printf("Enter Your Password :");
gets(b);
i=strcmp(b,a);
if(i==0)
printf("\nYour Password Is Correct");
else
printf("\nYour Password Is Incorrect");
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 117
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

108. Volume Of Cube


main()
{
int a;
int b;
clrscr();
printf("DISPLAY THE VOLUME OF A CUBE");
printf("\n---------------------------");
printf("\nEnter The Value Of Side Of A Cube :");
scanf("%d",&a);
b=a*a*a;
printf("\nArea Of The Cube :%d",b);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 118
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

109. Volume Of Cylinder


main()
{
int a;
int b;
float c;
clrscr();
printf("DISPLAY THE VOLUME OF A CYLINDER");
printf("\n-------------------------------");
printf("\nEnter The Radius Of A Cylinder :");
scanf("%d",&a);
printf("\nEnter The Height Of A Cylinder :");
scanf("%d",&b);
c=3.14*(a*a)*(b);
printf("\nArea Of The Cylinder :%f",c);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 119
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

110. Volume Of Sphere


main()
{
int a;
float b;
clrscr();
printf("DISPLAY THE VOLUME OF A SPHERE");
printf("\n-------------------------------");
printf("\nEnter The Radius Of A Sphere :");
scanf("%d",&a);
b=(4*3.14*a*a*a)/3;
printf("\nVolume Of The Sphere :%f",b);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 120
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

Pattern Problems
1. A1
main()
{
int n,i,j;
clrscr();
printf("Enter How Many Lines You Want In This Figure :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<=i;j++)
{
printf("*");
}
printf("\n");
}
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 121
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

2. A2
main()
{
int n,i,j;
clrscr();
printf("Enter How Many Lines You Want In This Figure :");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",j);
}
printf("\n");
}
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 122
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

3. A3
main()
{
int n,i,j;
clrscr();
printf("Enter How Many Lines You Want In This Figure :");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",i);
}
printf("\n");
}
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 123
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

4. A4
main()
{
int n,i,j;
clrscr();
printf("Enter How Many Lines You Want In This Figure :");
scanf("%d",&n);
//Loop To Create The Upper Triangle
for(i=0;i<n;i++)
{
for(j=0;j<=i;j++)
{
printf("*");
}
printf("\n");
}
getch();
//Loop To Create The Lower Triangle
for(i=n+1;i>0;i--)
{
for(j=0;j<i;j++)
{
printf("*");
}
printf("\n");
}
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 124
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

5. A5
main()
{
int num,i,j,k,l;
clrscr();
printf("Enter How Many Lines Structure You Want :");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
for(j=1;j<=num-i;j++)
{
printf(" ");
}
for(k=1;k<=i;k++)
{
printf("*");
}
for(l=1;l<i;l++)
{
printf("*");
}
printf("\n");
}
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 125
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

File Handling

1. Read & Write Multiple Objects


//Example To Read And Write Multiple Objects
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct
{
char name[30];
int age;
float perc;
}student;
main()
{
FILE *fp;
student std;
int result,n,i;
clrscr();
fp=fopen("file.txt","wb");
if(fp==NULL)
{
printf("\nError In Opening File");
exit(1);
}
printf("\nEnter Total Number Of Students :");
scanf("%d",&n);
//Input Student Detail
for(i=0;i<n;i++)
{
printf("\nEnter Details Of Student [%d] :\n",i);
printf("Enter Name :");

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 126
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY
fflush(stdin);
gets(std.name);
printf("Enter Age :");
scanf("%d",&std.age);
printf("Enter Percentage :");
scanf("%f",&std.perc);
//Writting In The File
result=fwrite((char*)&std,sizeof(std),1,fp);
if(result==0)break;
}
if(result)
printf("\nData Inserted Sucessfully");
fclose(fp);
//Re-Open File In Read Mode
fp=fopen("file.txt","rb");
if(fp==NULL)
{
printf("\nError In Opening File");
exit(1);
}
//Reading Data From File
printf("\n\nEntered Records Are :\n");
while((result=fread((char*)&std,sizeof(std),1,fp)))
{
if(result)
printf("\nName :%s \nAge :%d \nPercentage :%f",std.name,std.age,std.perc);
}
fclose(fp);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 127
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

2. Read A Complete Text File


//Example To Write And Read A Complete Text File
#include<stdio.h>
#include<stdlib.h>//for exit(1) function
main()
{
FILE*fp;
char ch;
clrscr();
fp=fopen("file1.txt","w");
if(fp==NULL)
{
printf("\nError In Opening File");
exit(1);
}
printf("\nFile Created Successfully");
printf("\nEnter Text Here (press # to close) :\n");
while( (ch=getchar())!='#')
{
fputc(ch,fp);
}
fclose(fp);//To Close File
printf("\nFile Saved And Closed");
/*Open File In Read Mode*/
fp=fopen("file1.txt","r");
if(fp==NULL)
{
printf("\nError In Opening File");
exit(1);//To Exit From Program
}
printf("\nFile Opened.\nFile's Contents :\n");
/*Reading From File*/
while( (ch=fgetc(fp))!=EOF)
{
printf("%c",ch);
}
printf("\n\nFile Closed");
getch(); }

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 128
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

3. Use Of fgetc() & fputc()


//Use Of fgetc() & fputc()
#include<stdio.h>
#include<stdlib.h>//For Exit(1) Function
main()
{
FILE *fp;
char ch;
clrscr();
fp=fopen("file1.txt","w");
if(fp==NULL)
{
printf("\nError In Opening File");
exit(1);//To Exit The Program
}
printf("File Created Successfully");
fputc('A',fp);//write 'A'
fputc('B',fp);//write 'B'
fputc('C',fp);//write 'C'
fclose(fp);//To Close File
printf("\nFile Saved And Closed");
/*Open File In Read Mode*/
fp=fopen("file1.txt","r");
if(fp==NULL)
{
printf("\nError In Opening File");
exit(1);//To Exit The Program
}
printf("\nFile Opened.\nFile's Contents :");
/*Reading From File*/
ch=fgetc(fp); printf("%c",ch);
ch=fgetc(fp); printf("\t%c",ch);
ch=fgetc(fp); printf("\t%c",ch);
fclose(fp);
printf("\nFile Closed");
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 129
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

4. Use Of fopen() & fclose()


//Use Of fopen() & fclose()
#include<stdio.h>
#include<stdlib.h>// for exit (1) function
main()
{
FILE *fp;
clrscr();
fp=("Prakhar.txt","w");
if(fp=NULL)
{
printf("Error In Opening File");
}
printf("\nFile Created Successfully");
fclose(fp);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 130
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

5. Use Of fprintf() & fscanf()


//Use Of fprintf() & fscanf()
#include<stdio.h>
#include<stdlib.h>//For Exit(1) Function
#include<string.h>
main()
{
FILE *fp;
char name[30];
char temp[30];
int age;
float perc;
int result;
clrscr();
fp=fopen("file.txt","wb");
if(fp==NULL)
{
printf("\nError In Opening File");
exit(1);//To Exit From Program
}
/*padding 30 bytes with space,you have to put 30 bytes for name*/
memset(name,' ',30);
printf("Enter Name :");
gets(temp);
memcpy(name,temp,strlen(temp));
printf("Enter Age :");
scanf("%d",&age);
printf("Enter Percentage :");
scanf("%f",&perc);
if(result)
printf("\nData Inserted Successfully");
fclose(fp);
//Open File In Read Mode
fp=fopen("file.txt","rb");
if(fp==NULL)
{
printf("\nError In Opening File");

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 131
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY
exit(1);//To Exit The Program
}
//Reading Data From The File
result=fscanf(fp,"%s,%d,%f",&name,&age,&perc);
if(result)
printf("\nData From File :\nName :%s \nAge :%d \nPercentage :%f",name,age,perc);
else
printf("\nData Reading Failed");
fclose(fp);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 132
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

6. Use Of fputs() & fgets()


//Use Of fputs() & fgets()
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
main()
{
FILE*fp;
char text[50];
clrscr();
fp=fopen("file.txt","w");
if(fp==NULL)
{
printf("\nError In Opening File");
exit(1);//To Exit The Program
}
strcpy(text,"This Is Line 1");
fputs(text,fp);
fputc('\n',fp);//Writting New Line After The String
strcpy(text,"This Is Line 2");
fputs(text,fp);
fputc('\n',fp);//Writting New Line After The String
fclose(fp);
fp=fopen("file.txt","r");
if(fp==NULL)
{
printf("\nError In Opening File");
exit(1);//To Exit The Program
}
fgets(text,20,fp);//20 Is The Maximum Character To Read
printf("%s\n",text);
fgets(text,20,fp);//20 Is The Maximum Character To Read
printf("%s\n",text);
fclose(fp);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 133
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

7. Use Of ftell() & fseek()


//Example Of ftell() & fseek() And Opening File In Both Read And write Mode
#include<stdio.h>
main()
{
FILE*fp;
int ch;
clrscr();
fp=fopen("file.txt","w+");
if(fp==NULL)
{
printf("\nError In Opening File");
exit(1);
}
//Writting A To Z In The File
for(ch='A';ch<='Z';ch++)
{
fputc(ch,fp);
}
printf("\nCurrent File Pointer's Position :%d",ftell(fp));
//Reaing File From The Beginning
fseek(fp,0,SEEK_SET);
printf("\nText From Beginning :\n");
while( (ch=fgetc(fp))!=EOF)
{
printf(" %c",ch);
}
fseek(fp,-10,SEEK_END);
printf("\nAfter -10 Bytes From SEEK_END,file pointer's position :%d",ftell(fp));
printf("\nText from -10 bytes backward :\n");
while( (ch=fgetc(fp))!=EOF)
{
printf(" %c",ch);
}
fclose(fp);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 134
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

8. Use Of fwrite() & fread()


//Use Of fwrite() & fread()
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct
{
char name[30];
int age;
float perc;
}student;
main()
{
FILE *fp;
student std;//*std-student structure's variable
int result;
clrscr();
fp=fopen("file.txt","wb");
if(fp==NULL)
{
printf("\nError In Opening File");
exit(1);//To Exit The Program
}
//Input Student Detail
printf("Enter Name :");gets(std.name);
printf("Enter Age :");scanf("%d",&std.age);
printf("Enter Percentage :");scanf("%f",&std.perc);
//Writting In The File
result=fwrite((char*)&std,sizeof(std),1,fp);
if(result)
printf("\nData Inserted Sucessfully");
fclose(fp);
//Re-Open File In Read Mode
fp=fopen("file.txt","rb");
if(fp==NULL)
{
printf("\nError In Opening File");

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 135
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY
exit(1);
}
//Reading Data From File
result=fread((char*)&std,sizeof(std),1,fp);
if(result)
printf("\nData From File:\nName :%s \nAge :%d \nPercentage :%f",std.name,std.age,std.perc);
else
printf("\nData Readin Failed");
fclose(fp);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 136
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

9. Use Of getw() & putw()


//Use Of Getw() & Putw()
#include<stdio.h>
#include<stdlib.h>
main()
{
FILE*fp;
int num;
int i;
clrscr();
fp=fopen("file.dat","wb");//File Has To Be Opened In Binary Mode If We Are Using getw() &
putw() functions
if(fp==NULL)
{
printf("\nError In Opening File");
exit(1);//To Exit The Program
}
for(i=0;i<10;i++)
{
printf("Enter Number [%d] :",i+1);
scanf("%d",&num);
putw(num,fp);//Write Number Into The File
}
fclose(fp);
//Open File To Read
fp=fopen("file.dat","rb");
if(fp==NULL)
{
printf("\nError In Opening File");
exit(1);//To Exit The Program
}
printf("\nEntered Numbers Are :\n");
for(i=0;i<10;i++)
{
num=getw(fp);
printf(" %d",num);
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 137
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY
fclose(fp);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 138
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

10. Use Of rename() & remove()


//Use Of rename() & remove()
#include<stdio.h>
main()
{
FILE*fp;
clrscr();
fp=fopen("%d:/file1.txt","w");
if(fp==NULL)
{
printf("\nFile To Create, File Name :%s","d:/file1.txt");
}
else
{
printf("\nFile Created,File Name :%s","d:/file1.txt");
}
fclose(fp);
//Rename file1.txt To file2.txt
rename("%d:file1.txt","d:file2.txt");
//Open file1.txt In Read Mode
fp=fopen("%d:/file1.txt","r");
if(fp==NULL)
{
printf("\nFailed To Open,File Name :%s","d:/file1.txt");
}
else
{
printf("\nFile Opened,File Name :%s","d:/file1.txt");
fclose(fp);
}
//Open file2.txt In read Mode
fp=fopen("d:/file2.txt","r");
if(fp==NULL)
{
printf("\nFile Opened,File Name :%s","d:/file2.txt");
fclose(fp);

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 139
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY
}
//Remove file2.txt
remove("d:/file2.txt");
getch();

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 140
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

Graphics

1. Analysis Of Co-Ordinates Of Different points


#include<graphics.h>
main()
{
int gd=DETECT,gm;
int x;
int y;
int k,l;
clrscr();
initgraph(&gd,&gm,"..//BGI");
x=getmaxx()/2;
y=getmaxy()/2;
printf("Maximum Value Of X :%d",x*2);
printf(" Half Value(k) :%d",x);
printf("\nMaximum Value Of Y :%d",y*2);
printf(" Half Value(l) :%d",y);
k=x;
l=y;
outtextxy(319,239,"(319,239)");
setcolor(1);
circle(x,y,30);
outtextxy(319,219,"30");
setcolor(2);
circle(x,y,50);
outtextxy(319,199,"50");
setcolor(3);
circle(x,y,70);
outtextxy(319,179,"70");
setcolor(4);
circle(x,y,90);
outtextxy(319,159,"90");
OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR
CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 141
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY
setcolor(5);
circle(x,y,109);
outtextxy(319,139,"110");
setcolor(6);
circle(x,y,130);
outtextxy(319,119,"130");
setcolor(7);
circle(x,y,150);
outtextxy(319,99,"150");
setcolor(8);
circle(x,y,170);
outtextxy(319,79,"170");
setcolor(9);
circle(x,y,190);
line(319,239,319,49);
line(319,239,129,239);
outtextxy(319,59,"190");
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 142
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

2. Change Background Colour


#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
textcolor(BLUE);
textbackground(WHITE);
cprintf("Text Colour Is Blue With White Background");
textcolor(RED+BLINK);
cprintf("\nText Colour Is Red With Blinking");
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 143
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

3. Change Colour And Blink The Text


//Program To Change The Colour And Blink The Text
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
textcolor(MAGENTA+BLINK);
cprintf("Prakhar");
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 144
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

4. Change Colour Of The Text


//Program To Change The Colour Of The Text
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
textcolor(RED);
cprintf("Prakhar");
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 145
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

5. Clock
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>

#define WBC 5
//^watchbackcolor
#define X 200
#define Y 200

void dial(int x, int y);


void sechand(int timeminute);

void minhand(int t)
{
int x1,y1;
setlinestyle(0,0,3);

x1= X+ (80 * cos(t*0.1047));


y1= Y+ (80 * sin(t*0.1047));

setcolor(BLACK);
line( X, Y, x1, y1);

setcolor(WBC+1);
line( X, Y, X+ 80 * cos((t-1)*0.1047),Y+ 80 * sin((t-1)*0.1047));
circle(X,Y,4);
}

void sechand(int t)
{
int x1,y1;
setlinestyle(0,0,3);

x1= X+(100 * cos(t*0.1047));


y1= Y+(100 * sin(t*0.1047));

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 146
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

setcolor(RED);
line(X, Y, x1, y1);

setcolor(WBC+1);
line(X, Y, X+ 100 * cos((t-1)*0.1047),Y+ 100 * sin((t-1)*0.1047));

circle(X,Y,4);
}

void dial(int x,int y)


{
int const size=200;

setfillstyle(1,WBC);
fillellipse(x,y,size,size);

setfillstyle(1,WBC+1);
fillellipse(x,y,size-20,size-20);

outtextxy(x,y-(size-40),"12");
outtextxy(x,y+(size-40),"6");
outtextxy(x+(size-40),y,"3");
outtextxy(x-(size-40),y,"9");
outtextxy(x+size/3,y-2*size/3,"1");
outtextxy(x+2*size/3,y-size/3,"2");
outtextxy(x+2*size/3,y+size/3,"4");
outtextxy(x+size/3,y+2*size/3,"5");
outtextxy(x-size/3,y+2*size/3,"7");
outtextxy(x-2*size/3,y+size/3,"8");
outtextxy(x-size/3,y-2*size/3,"11");
outtextxy(x-2*size/3,y-size/3,"10");

circle(x,y,4);
}

void main()
{
int gd=DETECT, gm,i,j, flag=1;
OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR
CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 147
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY
initgraph(&gd,&gm,"C:\\turboc3\\bgi");

dial(200,200);
do
{
minhand(i);
for(j=0;j<60;j++)
{
sechand(j);
delay(1000
);
if(kbhit()) {
flag =0;
break;
}
}
i++;
}while(flag);
closegraph();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 148
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

6. Draw A Bar Graph


#include<graphics.h>
#include<conio.h>
main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"..//BGI");
settextstyle(BOLD_FONT,HORIZ_DIR,2);
outtextxy(270,0,"BAR GRAPH");
setlinestyle(SOLID_LINE,0,2);
/*Draw X And Y Axis*/
line(90,410,90,50);
line(90,410,590,410);
line(85,60,90,50);
line(95,60,90,50);
line(585,405,590,410);
line(585,415,590,410);
outtextxy(65,60,"Y");
outtextxy(570,420,"X");
outtextxy(70,415,"O");
/*Draw Bars On Screen*/
setfillstyle(XHATCH_FILL,RED);
bar(150,80,200,410);
bar(225,100,275,410);
bar(300,120,350,410);
bar(375,170,425,410);
bar(450,135,500,410);
getch();
closegraph();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 149
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

7. Draw A Circle
//Program To Draw A Circle
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
main()
{
int gd=DETECT,gm;
int x;
int y;
int radius=80;
clrscr();
initgraph(&gd,&gm,"..//BGI");
x=getmaxx()/2;
y=getmaxx()/2;
outtextxy(x-100,50,"Circle Using Graphics In C");
circle(x,y,radius);
getch();
closegraph();
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 150
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

8. Draw A Pattern Using Rectangle,Circle & Line


#include<graphics.h>
#include<conio.h>
main()
{
int driver=DETECT,mode;
clrscr();
initgraph(&driver,&mode,"");
setlinestyle(DOTTED_LINE,8,THICK_WIDTH);
rectangle(450,350,150,150);
circle(500,250,50);
line(300,200,350,300);
line(300,200,250,300);
line(350,300,250,300);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 151
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

9. Draw A Rectangle And A Bar


//Program To Draw Rectangle And Bar Using C Graphics
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"..//BGI");
//To Draw Rectangle On The Screen
rectangle(150,50,400,150);//Left Upper And Right Bottom Corner
//To Draw Bar On The Screen
bar(150,200,400,350);//Left Upper And Right Bottom Corner
getch();
closegraph();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 152
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

10. Draw Aine

//Program To Draw A Line


#include<graphics.h>
#include<conio.h>
main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"..//BGI");
line(100,100,200,200);
getch();
closegraph();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 153
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

11. Draw An Ellipse


//Program To Draw An Ellipse
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
main()
{
int gd=DETECT,gm;
int x;
int y;
initgraph(&gd,&gm,"..//BGI");
x=getmaxx()/2;
y=getmaxx()/2;
outtextxy(x-100,50,"Ellipse Using Graphics In C");
ellipse(x,y,0,360,120,60);
getch();
closegraph();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 154
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

12. Draw Concentric Circles With Different Colour


//Program To Draw Concentric Circles
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
main()
{
int gd=DETECT,gm;
int x;
int y;
initgraph(&gd,&gm,"..//BGI");
x=getmaxx()/2;
y=getmaxx()/2;
outtextxy(x-80,150,"Concentric Circles");
setcolor(RED);
circle(x,y,30);
setcolor(GREEN);
circle(x,y,50);
setcolor(YELLOW);
circle(x,y,70);
setcolor(BLUE);
circle(x,y,90);
getch();
closegraph();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 155
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

13. Draw OP-AMP


#include<graphics.h>
main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"..//BGI");
line(50,125,100,125);//Horizontal Line -Ve Terminal
line(50,175,100,175);//Horizontal Line +ve Terminal
line(100,100,100,200);//Vertical Line
line(100,100,150,150);//Top To Bottom Slant Line
line(150,150,100,200);//Bottom To Top Slant Line
line(125,100,125,125);//Vertical Line +VCC
line(125,175,125,200);//Vertical Line -VCC
line(150,150,200,150);//Horizontal Line
getch();
closegraph();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 156
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

14. Draw Polygon And Sector


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
main()
{
int gd=DETECT,gm;
int poly[]={320,150,420,300,250,300,320,150};
clrscr();
initgraph(&gd,&gm,"..//BGI");
sector(150,400,30,300,100,50);
drawpoly(4,poly);
getch();
closegraph();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 157
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

15. Draw Simple Geometrical Shapes


#include<graphics.h>
main()
{
int gd=DETECT,gm;
int poly[12]={350,450,350,410,430,400,350,350,300,430,350,450};
clrscr();
initgraph(&gd,&gm,"..//BGI");
circle(100,100,50);
outtextxy(75,170,"Circle");
rectangle(200,50,350,150);
outtextxy(240,170,"Rectangle");
ellipse(500,100,0,360,100,50);
outtextxy(480,170,"Ellipse");
line(100,250,540,250);
outtextxy(300,260,"Line");
sector(150,400,30,300,100,50);
outtextxy(120,460,"Sector");
drawpoly(6,poly);
outtextxy(340,460,"Polygon");
getch();
closegraph();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 158
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

16. Laser
/* *************************PROJECT( BY Prakhar Mor) ***************************
ON
LASER
*******************************************************************/

// HEADER FILES USED

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>

// FUNCTIONS USED

void projectwork1();
void projectwork2();
void projectwork3();
void closing();

void main()
{
int gd=DETECT,gm,x,y;
initgraph(&gd,&gm,"c:\\turboc3\\bgi"); //Initializing Graphics Mode

projectwork1();
projectwork2();
projectwork3();
closing();

getch();
closegraph();
restorecrtmode();
}

void projectwork1() // AMPLIFICATION

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 159
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY
{
setbkcolor(BLUE);
setcolor(WHITE);
circle(170,230,8);

setfillstyle(1,YELLOW);
floodfill(170,230,WHITE);
setcolor(GREEN);
settextstyle(11,0,2);
outtextxy(150,250,"PHOTON");
delay(2000);

settextstyle(7,0,4);
setcolor(WHITE);
outtextxy(110,380,"Amplification of Photons");
delay(1000);

setcolor(WHITE);
circle(250,180,8); //SECOND_UP
circle(250,280,8);
floodfill(250,180,WHITE);
floodfill(250,280,WHITE);
delay(500);

circle(330,120,8); //THIRD_UP
circle(330,195,8);

circle(330,265,8);
circle(330,340,8); //THIRD_DOWN

floodfill(330,120,WHITE);
floodfill(330,195,WHITE);
floodfill(330,265,WHITE);
floodfill(330,340,WHITE);
delay(500);

circle(410,80,8);
circle(410,125,8);
circle(410,165,8);
OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR
CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 160
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY
circle(410,210,8);

circle(410,245,8);
circle(410,285,8);
circle(410,325,8);
circle(410,370,8);

floodfill(410,80,WHITE);
floodfill(410,125,WHITE);
floodfill(410,165,WHITE);
floodfill(410,210,WHITE);
floodfill(410,245,WHITE);
floodfill(410,285,WHITE);
floodfill(410,325,WHITE);
floodfill(410,370,WHITE);

delay(3000);
cleardevice();
}

void projectwork2()
{
setbkcolor(BLUE);
setcolor(WHITE);

setfillstyle(1,RED); // ATOM
circle(310,230,30);
floodfill(310,230,WHITE);
setcolor(YELLOW);
settextstyle(11,0,2);
outtextxy(296,270,"ATOM");

setcolor(WHITE);
setfillstyle(1,GREEN); // PHOTON
circle(190,110,5);
floodfill(190,110,WHITE);
setcolor(GREEN);
outtextxy(170,120,"PHOTON");
OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR
CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 161
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY
delay(2000);
setcolor(BLUE);
outtextxy(170,120,"PHOTON");

setfillstyle(1,BLACK);
floodfill(190,110,WHITE);
setcolor(BLACK);
circle(190,110,5);

setcolor(WHITE);

circle(210,130,5);
setfillstyle(1,GREEN);
floodfill(210,130,WHITE);
delay(300);
setfillstyle(1,BLACK);
floodfill(210,130,WHITE);
setcolor(BLACK);
circle(210,130,5);

setcolor(WHITE);

circle(230,150,5);
setfillstyle(1,GREEN);
floodfill(230,150,WHITE);
delay(300);
setfillstyle(1,BLACK);
floodfill(230,150,WHITE);
setcolor(BLACK);
circle(230,150,5);

setcolor(WHITE);

circle(250,170,5);
setfillstyle(1,GREEN);
floodfill(250,170,WHITE);
delay(300);
OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR
CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 162
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY
setfillstyle(1,BLACK);
floodfill(250,170,WHITE);
setcolor(BLACK);
circle(250,170,5);

setcolor(WHITE);

circle(270,190,5);
setfillstyle(1,GREEN);
floodfill(270,190,WHITE);
delay(300);
setfillstyle(1,BLACK);
floodfill(270,190,WHITE);
setcolor(BLACK);
circle(270,190,5);

setcolor(WHITE);

circle(295,215,5);
setfillstyle(1,GREEN);
floodfill(295,215,WHITE);
delay(300);
setfillstyle(1,RED);
floodfill(295,215,WHITE);
setcolor(RED);
circle(295,215,5);

setcolor(WHITE);

setfillstyle(1,YELLOW); // ATOM
circle(310,230,30);
floodfill(310,230,WHITE);
setcolor(YELLOW);
settextstyle(7,0,3);
outtextxy(115,50,"Stimulation of ATOM to Release Photons");
setcolor(WHITE);
delay(300);
setfillstyle(1,RED); // ATOM
circle(310,230,30);
OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR
CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 163
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY
floodfill(310,230,WHITE);

setcolor(WHITE);

circle(346,225,5);
setfillstyle(1,GREEN);
floodfill(346,225,WHITE);
circle(346,235,5);
floodfill(346,235,WHITE);
delay(800);
setfillstyle(1,BLACK);
floodfill(346,225,WHITE);
floodfill(346,235,WHITE);
setcolor(BLACK);
circle(346,225,5);
circle(346,235,5);

setcolor(WHITE);

circle(365,205,5);
setfillstyle(1,GREEN);
floodfill(365,205,WHITE);
circle(365,255,5);
floodfill(365,255,WHITE);
delay(300);
setfillstyle(1,BLACK);
floodfill(365,205,WHITE);
floodfill(365,255,WHITE);
setcolor(BLACK);
circle(365,205,5);
circle(365,255,5);

setcolor(WHITE);

circle(385,185,5);
setfillstyle(1,GREEN);
floodfill(385,185,WHITE);
circle(385,275,5);
floodfill(385,275,WHITE);
OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR
CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 164
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY
delay(300);
setfillstyle(1,BLACK);
floodfill(385,185,WHITE);
floodfill(385,275,WHITE);
setcolor(BLACK);
circle(385,185,5);
circle(385,275,5);

setcolor(WHITE);

circle(405,165,5);
setfillstyle(1,GREEN);
floodfill(405,165,WHITE);
circle(405,295,5);
floodfill(405,295,WHITE);
delay(300);
setfillstyle(1,BLACK);
floodfill(405,165,WHITE);
floodfill(405,295,WHITE);
setcolor(BLACK);
circle(405,165,5);
circle(405,295,5);

setcolor(WHITE);

circle(425,145,5);
setfillstyle(1,GREEN);
floodfill(425,145,WHITE);
circle(425,315,5);
floodfill(425,315,WHITE);
delay(300);
setfillstyle(1,BLACK);
floodfill(425,145,WHITE);
floodfill(425,315,WHITE);
setcolor(BLACK);
circle(425,145,5);
circle(425,315,5);

setcolor(WHITE);
OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR
CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 165
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

circle(445,125,5);
setfillstyle(1,GREEN);
floodfill(445,125,WHITE);
circle(445,335,5);
floodfill(445,335,WHITE);
delay(300);
setfillstyle(1,BLACK);
floodfill(445,125,WHITE);
floodfill(445,335,WHITE);
setcolor(BLACK);
circle(445,125,5);
circle(445,335,5);

setcolor(WHITE);

circle(465,105,5);
setfillstyle(1,GREEN);
floodfill(465,105,WHITE);
circle(465,355,5);
floodfill(465,355,WHITE);
delay(300);
setfillstyle(1,BLACK);
floodfill(465,105,WHITE);
floodfill(465,355,WHITE);
setcolor(BLACK);
circle(465,105,5);
circle(465,355,5);

setcolor(WHITE);

circle(485,95,5);
setfillstyle(1,GREEN);
floodfill(485,95,WHITE);
circle(485,375,5);
floodfill(485,375,WHITE);
circle(190,110,5);
floodfill(190,110,WHITE);
setcolor(YELLOW);
OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR
CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 166
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY
settextstyle(11,0,2);
outtextxy(170,120,"PHOTON");
outtextxy(465,110,"PHOTON");
outtextxy(465,390,"PHOTON");

delay(5000);
cleardevice();
}

void projectwork3()
{

setbkcolor(BLACK);
setcolor(WHITE);

rectangle(200,150,460,160);
rectangle(200,300,460,310);
setfillstyle(1,GREEN);
floodfill(250,155,WHITE);
floodfill(250,305,WHITE);
ellipse(200,230,90,89,15,70); //MIRROR
setfillstyle(1,BLUE);
floodfill(205,232,WHITE);
ellipse(460,230,90,89,15,70); // SEMI SILVERED MIRROR
setfillstyle(1,CYAN);
floodfill(465,232,WHITE);

rectangle(300,365,360,380); // POWER SOURCE


setfillstyle(1,RED);
floodfill(305,370,WHITE);

moveto(300,365);
lineto(310,355);
lineto(370,355);
lineto(360,365);
moveto(360,380);
lineto(370,370);
lineto(370,355);
floodfill(305,362,WHITE);
OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR
CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 167
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY
floodfill(366,370,WHITE);
setcolor(YELLOW);
settextstyle(11,0,2);
outtextxy(288,390,"POWER SOURCE");
outtextxy(300,120,"ELECTRODES");
outtextxy(130,230,"MIRROR");
outtextxy(480,175,"SEMI SILVERED");
outtextxy(510,190,"MIRROR");
setcolor(WHITE);
moveto(370,363);
lineto(411,363);
lineto(411,265);
moveto(370,366);
lineto(413,366);
lineto(413,265);

rectangle(395,260,428,265);
floodfill(396,261,WHITE);
rectangle(395,200,428,205);
floodfill(396,201,WHITE);
rectangle(240,260,273,265);
floodfill(241,261,WHITE);
rectangle(240,200,273,205);
floodfill(241,201,WHITE);

moveto(255,265);
lineto(255,376);
lineto(300,376);
moveto(257,265);
lineto(257,373);
lineto(300,373);

setcolor(RED);

moveto(280,205);
lineto(280,260);
lineto(273,260);
moveto(273,205);
OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR
CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 168
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY
lineto(280,205);

moveto(435,205);
lineto(435,260);
lineto(428,260);
moveto(428,205);
lineto(435,205);

setcolor(WHITE);
circle(100,50,8);
setfillstyle(1,RED);
floodfill(100,50,WHITE);
circle(100,85,3);
setfillstyle(1,YELLOW);
floodfill(100,85,WHITE);
setcolor(YELLOW);
outtextxy(128,47,"ATOM");
outtextxy(128,82,"PHOTON");

// ATOM AND PHOTON MOVEMENT

setcolor(WHITE);
setfillstyle(1,RED); //ATOM
circle(365,205,8);
floodfill(365,205,WHITE);

circle(325,285,8);
floodfill(325,285,WHITE);
delay(3000);

circle(325,260,8);
floodfill(325,260,WHITE);

setfillstyle(1,BLACK);
floodfill(325,285,WHITE);
setcolor(BLACK);
circle(325,285,8);

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 169
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY
setcolor(WHITE);

setfillstyle(1,RED);
circle(345,190,8);
floodfill(345,190,WHITE);
setfillstyle(1,BLACK);
floodfill(365,205,WHITE);
setcolor(BLACK);
circle(365,205,8);
delay(500);

setfillstyle(1,RED);
setcolor(WHITE);
circle(325,170,8);
floodfill(325,170,WHITE);
setfillstyle(1,BLACK);
floodfill(345,190,WHITE);
setcolor(BLACK);
circle(345,190,8);

setcolor(WHITE);
setfillstyle(1,YELLOW);
circle(250,250,3);
floodfill(250,250,WHITE);
delay(100);
circle(275,225,3);
floodfill(275,225,WHITE);
delay(100);
circle(295,205,3);
floodfill(295,205,WHITE);
delay(100);
circle(315,185,3);
floodfill(315,185,WHITE);
delay(100);
circle(320,205,3);
floodfill(320,205,WHITE);
delay(100);
OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR
CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 170
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY
circle(345,205,3);
floodfill(345,205,WHITE);
circle(365,205,3);
floodfill(365,205,WHITE);
delay(100);
circle(385,210,3);
floodfill(385,210,WHITE);

circle(395,220,3);
floodfill(395,220,WHITE);
circle(305,225,3);
floodfill(305,225,WHITE);

circle(325,245,3);
floodfill(325,245,WHITE);
delay(100);
circle(345,265,3);
floodfill(345,265,WHITE);
delay(100);
circle(345,235,3);
floodfill(345,235,WHITE);
delay(100);
circle(365,285,3);
floodfill(365,285,WHITE);
circle(365,230,3);
floodfill(365,230,WHITE);
delay(100);
circle(395,245,3);
floodfill(395,245,WHITE);
delay(100);
circle(415,225,3);
floodfill(415,225,WHITE);
delay(100);
circle(425,245,3);
floodfill(425,245,WHITE);
delay(300);

setlinestyle(SOLID_LINE,1,3);
setcolor(YELLOW);
OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR
CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 171
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY
moveto(460,230);
lineto(600,230); //LASER
setcolor(YELLOW);
outtextxy(510,240,"LASER");
moveto(540,230);
lineto(535,225);
moveto(540,230);
lineto(535,235);
delay(3000);
cleardevice();
}

void closing()
{
setbkcolor(RED);
setcolor(YELLOW);
settextstyle(7,HORIZ_DIR,4);
outtextxy(160,180,"!! THANK YOU !!");
outtextxy(120,280," Press any key to EXIT");
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 172
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

17. Make Counter


//Program To Make Counter Using C Graphics
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<string.h>
#include<dos.h>
main()
{
int gd=DETECT,gm;
int i,midx,midy,count;
char string[100];
clrscr();
printf("Enter A Number :\n");
scanf("%d",&count);
initgraph(&gd,&gm,"X:\\TC\\BGI");
/* Get Mid Position In X And Y Axis */
midx=getmaxx()/2;
midy=getmaxx()/2;
for(i=0;i<=count;i++)
{
/* Draws The Gray Board */
setcolor(WHITE);
setfillstyle(SOLID_FILL,WHITE);
rectangle(midx-50,midy-50,midx+50,midy+50);
floodfill(midx,midy,WHITE);
/*Place The Counter Inside Rectangle*/
setcolor(BLUE);
sprintf(string,"%s","Counter");
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,5);
settextjustify(CENTER_TEXT,CENTER_TEXT);
outtextxy(midx,midy-100,"Counter");
/*Print The Counter Value*/
sprintf(string,"%d",i);
outtextxy(midx,midy,string);
/*Delay For A Second(1000 milli second) */
delay(1000);
/* Clears Screen */

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 173
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY
cleardevice();
}
getch();
closegraph();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 174
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

18. Marks Analysis


#include<stdio.h>
#include<conio.h>
main()
{
int marks[4],tot=0,per,i;
clrscr();
for(i=0;i<4;i++)
{
printf("\nEnter Marks[%d subject]:",i+1);
scanf("%d",&marks[i]);
tot=tot+marks[i];
}
per=tot/4;
if(marks[0]>=40 && marks[1]>=40 && marks[2]>=40 && marks[3]>=40)
{
if(per>=70)
{
printf("\nDistinction");
}
else if(per>=60)
{
printf("\nFirst Class");
}
else if(per>=50)
{
printf("\nSecond Class");
}
else
{
printf("\Pass");
}
printf("\n----------\n");
for(i=0;i<4;i++)
{
printf("\nSubject %d :%d",i+1,marks[i]);
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 175
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY
printf("\n--------\n");
printf("\nTotal :%d",tot);
printf("\nPercentage :%.2f",(float)per);
printf("\n---------\n");
}
else
{
gotoxy(35,12);
textcolor(RED+BLINK);
cprintf("Fail");
}
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 176
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

19. Moving Car


#include<graphics.h>
#include<dos.h>
main()
{
int i,j=0,gd=DETECT,gm;
initgraph(&gd,&gm,"..//BGI");
settextstyle(DEFAULT_FONT,HORIZ_DIR,2);
outtextxy(25,240,"Press Any Key To View The Moving Car");
getch();
for(i=0;i<=420;i=i+10)
{
j++;
rectangle(50+i,275,150+i,400);
rectangle(150+i,350,200+i,400);
circle(75+i,410,10);
circle(175+i,410,10);
setcolor(j);
delay(100);
if(i==420)
break;
if(j==15)
j=2;
cleardevice;
}
getch();
closegraph();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 177
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

20. Rainbow
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
int gdriver = DETECT,gmode;
int x,y,i;
initgraph(&gdriver,&gmode,"C:\\Turboc3\\BGI");
x=getmaxx()/2;
y=getmaxy()/2;
for(i=30;i<200;i++)
{
delay(100);
setcolor(i/10);
arc(x,y,0,180,i-10);
}
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 178
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

21. Rotating Circle


#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
main()
{
int gd,gm;
gd=DETECT;
initgraph(&gd,&gm,"..//BGI");
setcolor(3);
setfillstyle(SOLID_FILL,RED);
bar(50,50,590,430);
setfillstyle(1,14);
bar(100,100,540,380);
while(!kbhit())
{
putpixel(random(439)+101,
random(279)+101,random(16));
setcolor(random(16));
circle(320,240,random(100));
}
getch();
closegraph();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 179
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

22. Sound
/*This program Displays Colourful Characters in Text Mode
and Initialises speaker & Plays Sound of Random Frequencies.
Program contributed by: Akhilesh Dhar Dubey */
#include<stdio.h>
#include<dos.h>
#include<conio.h>
#include<stdlib.h>

void main()
{
int count=50;
clrscr();
while(count--)
{
sound(10*random(100));
delay(75);
nosound();
textattr(random(16)+'a'+BLINK);
cprintf("*");
}
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 180
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

23. Use Of Floodfill


#include<graphics.h>
#include<conio.h>
main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"..//BGI");
setcolor(RED);
circle(100,100,50);
floodfill(100,100,RED);
getch();
closegraph();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 181
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

24. Use Of Move to


#include<graphics.h>
main()
{
int driver,mode;
clrscr();
driver=DETECT;
initgraph(&driver,&mode,"");
line(80,10,80,500);
line(10,100,700,100);
line(550,10,550,500);
line(40,403,700,403);
moveto(80,100);
lineto(550,403);
moveto(550,100);
lineto(80,403);
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 182
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

25. Use Of Putpixel


#include<graphics.h>
#include<stdio.h>
#include<stdlib.h>
main()
{
int gd,gm,i;
clrscr();
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"..//BGI");
while(!kbhit())
{
cleardevice();
for(i=1;i<=500;i++)
putpixel(random(640),random(500),15);
settextstyle(1,0,6);
outtextxy(145,200,"Prakhar Is Great");
delay(2000);
}
getch();
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
PAGE 183
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

1. Select the pattern to which the question belongs i.e it must be


Star ,Number , Alphabet(Character)pattern or even
in complicated programs they are mixed and also understand the
symmetry between triangle's.
2. After deciding which type of pattern it is,then count the
number of different triangles are there including space traingle.
3. Try to analyse that how many loops would be required.
4. Try to sort out in the form of inner loop and outer loop.
5. Try to find out relation of inner loop and outer loop.I mean as
number of lines increases or decreases the amount of star increases
or decreases.
6. Accordingly,how many times the loop should run should be
decided.

For example's this pattern below

How to analyse this:


1. It is star pattern so print statement will require "*" in it.
2. Here only 1 triangle is used.
3. Two loops would be required.One for different lines and
other for stars at each line.
4. So one inner loop and one outer loop would be required.
5. As line number increases number of star's increases in same
number.
6. So the loop goes as follow
for(i=0;i<n;i++) //loop runs for N times
{
for(j=0;j<=i;j++) // Inner loop will run as many times as that of the line
number.

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


PAGE 184
CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

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

Let us analyse above pattern


1. It is star pattern so print statement will require "*" in it.
2. Here only 2 triangle's as we have to print spaces also.
3. Three loops would be required.One for different lines one for
spaces and one for stars.
4. one outer loop would be required with two inner loop.
5. As line number increases number of spaces will decrease
and number of star's increases in same number.
6. So the loop goes as follow
for(i=0;i<n;i++) //outer loop for N line
{
for(j=0;j<n-i;j++) //loop should run for n-i times i.e if line no is 2
{ // and there are 5 lines in total then 3 times space
should be printed
printf(" "); //prints space
}
for(j=0;j<=i;j++) // this is same as above program.
{
printf("*");
}
printf("\n");
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


PAGE 185
CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

Lets analyse this triangle.


1. It is star pattern so print statement will require "*" in it.
2. Here 3 triangle's as we have to print spaces in between.
3. Four loops would be required.One for different lines one for
each triangle.
4. one outer loop would be required with Three inner loops.
5. As line number increases number of stars in 1st and in 3rd
triangle will decrease,One thing you should realize that number of
spaces at each line increases by 2 i.e twice the number of line.
6. So the loop goes as follow
for(i=0;i<n;i++) //Outer loop runs for n times
{ for(j=0;j<n-i;j++) //it is similar to print spaces in previous pattern.
{
printf("*"); //1st inner loop.
}
for(k=0;k<(2*i);k++) //as line number increases spaces increases twice the
line number
{
printf(" "); //2nd inner loop i.e for spaces.
}
for(j=0;j<n-i;j++) // no need to explain this
{
printf("*"); //3rd Inner loop.
}
printf("\n");

Above image is mirror image of previous pattern:

1. It is star pattern so print statement will require "*" in it.


2. Here 6 triangle's as Shown in above image.
3. Six loops would be required.

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


PAGE 186
CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

4. Two outer loop would be required with Three inner loops


each.
5. 1st half of patter. As line number increases number of stars in
1st and in 3rd triangle will decrease,One thing you should realize
that number of spaces at each line increases by 2 i.e twice the
number of line.
6. 2nd half of pattern.As line number increases number of
stars in 4th and in 6th triangle will increase,While number of
spaces at each line decreases by 2.
7. The code is as follows
for(i=0;i<n;i++)
{ for(j=0;j<n-i;j++)
{
printf("*");
}
for(k=0;k<(2*i);k++)
{
printf(" ");
}
for(j=0;j<n-i;j++)
{
printf("*");
}
printf("\n");
} //Till here the code is same as that of above pattern.
for(i=0;i<n;i++)
{
for(j=0;j<=i;j++)
{
printf("*");
}
for(k=2*(n-i-1);k>0;k--)
{
printf(" ");
}
for(j=0;j<=i;j++)
{
printf("*");
}
printf("\n");
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


PAGE 187
CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

Analyse the above triangle

1. It is star pattern so print statement will require "*" in it.


2. Here only 2 triangle is used,1st for space.
3. Three loops would be required.One for different lines and
other two for space and stars each.
4. So one inner loop and two outer loop would be required.
5. As line number increases number of space's increases in
same number and star's decreases in sequence of 2*(n-i)-1 where N
is number of lines and i is number of current line.
6. So the loop goes as follow
for(i=0;i<n;i++)
{
for(j=0;j<i;j++)
{
printf(" ");
}
for(k=2*(n-i)-1;k>0 ;k--)
{
printf("*");
}
printf("\n");
}
Lets move towards number patterns:

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


PAGE 188
CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

1. It is Number pattern so print statement will require "%d" in it.


2. Here only 2 triangle is used, 1 for space and other for
numbers.
3. Three loops would be required.
4. So two inner loop and one outer loop would be required.
5. Looking at the triangle we realize that numbers start from 1
and ends with line number and number of spaces goes on
decreasing with increasing line number.
6. So the loop goes as follow

for(i=1;i<=n;i++)

{
for(j=1;j<=n-i;j++)
{
printf(" ");
}
for(k=1;k<=i;k++)
{
printf("%d",k);
}
printf("\n");
}
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


PAGE 189
CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

1. It is Number pattern so print statement will require "%d" in it.


2. Here only 1 triangle is used.
3. Two loops would be required.
4. So two inner loop and one outer loop would be required.
5. Looking at the triangle we realize that numbers start from line
number and ends with 1.
6. So the loop goes as follow 7.
for(i=1;i<=n;i++)
{
for(j=i;j>0;j--)
{
printf("%d ",j);
}
printf("\n");
}

1. It is Number pattern so print statement will require "%d" in it.


2. Here only 2 triangle should be considered but we have
to considered 3 as the 3rd triangle's code cannot be included in 2
triangle .
3. four loops would be required.
4. So three inner loops and one outer loop would be required.

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


PAGE 190
CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

5. Looking at the triangle we realize that numbers start from line


number and ends with 1 in triangle 2 and in triangle no 3,it starts
with 2 ends with line number.
6. So the loop goes as follow
for(i=0;i<n;i++)
{
for(j=n-i;j>0;j--)
printf(" ");
for(k=i;k>0;k--)
{
printf("%d ",k);
}
for(l=2;l<=i;l++)
{
printf("%d ",l);
}
printf("\n");
}

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


PAGE 191
CONTACT NO. 9479638126, 0761-2431352,
EMAIL ID:- [email protected]
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

1. It is Number pattern so print statement will require "%d" in it.


2. Pattern contains a single triangle,but on analyzing the
upper and lower part of triangle we can say that two separate
loops would be required for both part.
3. Four loops would be required.
4. So two inner loops and two outer loops would be required.
5. As line number increases the number of values to be
printed also increases in first triangle and decrease in second
triangle.
6. So the loop goes as follow
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%3d",(j*i));
}
printf("\n");
}
for(k=n-1;k>0;k--)
{
for(j=1;j<=k;j++) printf("%3d",(k*j));
printf("\n");
}

Before starting with alphabet patterns one should have enough knowledge
of ascii values.

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352, PAGE 192
EMAIL ID:- [email protected]
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY

Ascii value of 'A' is 65 'B' is 66 and so on 'Z' is 90


Ascii value of 'a' is 97 'b' is 98 and so on 'z' is 122 so
difference of ascii between A and a is 32.
Lets see Alphabet patterns:

1. It is Alphabet pattern so print statement will require "%c" in it.


2. Pattern contains a two triangle.
3. Three loops would be required.
4. So two inner loops and one outer loop would be required.
5. Assume 65 in place of A and so on,it would be easy to
analyze.
6. So the loop goes as follow c='A'
for(i=0;i<=n;i++)
{
for(j=0;j<=i;j++)
{
printf(" ");
}
for(k=n-i-1;k>=0;k--)
{
printf("%c",c); //you can also have i+65 instead of c
}
c++;
printf("\n");
}
for(i=0;i<=n;i++)
{
for(j=0;j<=i;j++)

OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR


CONTACT NO. 9479638126, 0761-2431352, PAGE 193
EMAIL ID:- [email protected]
GYAN GROUP INSTITUTE FOR COMPUTER SCIENCE
AND TECHNOLOGY
{
printf(" ");
}
for(k=n-i-1;k>=0;k--)
{
printf("%c",c); //you can also have i+65 instead of c
}
c++;
printf("\n");
}

1. It is Alphabet pattern so print statement will require "%c" in it.


2. Pattern contains a two triangle.
3. Three loops would be required.
4. So two inner loops and one outer loop would be required.
5. Assume 65 in place of A and so on,it would be easy to
analyze.In this pattern you can see that value of each character
printed is increased by 1.
6. So the loop goes as follow c='A';
for(i=0;i<=n;i++)
{
for(j=0;j<=i;j++)
{
printf(" ");
}
for(k=n-i-1;k>=0;k--)
{
printf("%c",c); c++;
}
printf("\n");
}
OPOSITE POLICE STATION, MAIN ROADRANJHI JABALPUR
CONTACT NO. 9479638126, 0761-2431352, PAGE 194
EMAIL ID:- [email protected]

You might also like