Bca C Lab

Download as pdf or txt
Download as pdf or txt
You are on page 1of 78

C lab

1.write a program for using arithmetic operators?

int a = 12, b = 3; /* Program to Perform Arithmetic Operations in C */

#include<stdio.h>

int main()

int addition, subtraction, multiplication, division, modulus;

addition = a + b; //addition of 3 and 12

subtraction = a - b; //subtract 3 from 12

multiplication = a * b; //Multiplying both

division = a / b; //dividing 12 by 3 (number of times)

modulus = a % b; //calculation the remainder


printf("Addition of two numbers a, b is : %d\n", addition);

printf("Subtraction of two numbers a, b is : %d\n", subtraction);

printf("Multiplication of two numbers a, b is : %d\n", multiplication);

printf("Division of two numbers a, b is : %d\n", division);

printf("Modulus of two numbers a, b is : %d\n", modulus);

NOTE: When we are using c division (/) operator the result will completely

#include<stdio.h>

int main()

int a = 7, b = 3;

int integerdiv, modulus;

float floatdiv;
integerdiv = a / b; // dividing 7 by 3

modulus = a % b; // calculation the remainder

floatdiv = (float)a / b; // Converting int to float

printf("Division of two numbe

// Working of arithmetic operators


#include <stdio.h>
int main()
{
int a = 9,b = 4, c;

c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d
\n",c);

return 0;
}

Output
a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when a divided by b=1
c/d = 2

Example 2: Increment and Decrement Operators

// Working of increment and decrement


operators
#include <stdio.h>
int main()
{
int a = 10, b = 100;
float c = 10.5, d = 100.5;

printf("++a = %d \n", ++a);


printf("--b = %d \n", --b);
printf("++c = %f \n", ++c);
printf("--d = %f \n", --d);

return 0;
}

Output
++a = 11
--b = 99
++c = 11.500000
--d = 99.500000
Example 3: Assignment Operators

// Working of assignment operators


#include <stdio.h>
int main()
{
int a = 5, c;

c = a; // c is 5
printf("c = %d\n", c);
c += a; // c is 10
printf("c = %d\n", c);
c -= a; // c is 5
printf("c = %d\n", c);
c *= a; // c is 25
printf("c = %d\n", c);
c /= a; // c is 5
printf("c = %d\n", c);
c %= a; // c = 0
printf("c = %d\n", c);

return 0;
}

Output
c = 5
c = 10
c = 5
c = 25
c = 5
c = 0
C Relational Operators
Example 4: Relational Operators

// Working of relational operators


#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10;

printf("%d == %d is %d \n", a, b, a == b);


printf("%d == %d is %d \n", a, c, a == c);
printf("%d > %d is %d \n", a, b, a > b);
printf("%d > %d is %d \n", a, c, a > c);
printf("%d < %d is %d \n", a, b, a < b);
printf("%d < %d is %d \n", a, c, a < c);
printf("%d != %d is %d \n", a, b, a != b);
printf("%d != %d is %d \n", a, c, a != c);
printf("%d >= %d is %d \n", a, b, a >= b);
printf("%d >= %d is %d \n", a, c, a >= c);
printf("%d <= %d is %d \n", a, b, a <= b);
printf("%d <= %d is %d \n", a, c, a <= c);

return 0;
}

Output
5 == 5 is 1
5 == 10 is 0
5 > 5 is 0
5 > 10 is 0
5 < 5 is 0
5 < 10 is 1
5 != 5 is 0
5 != 10 is 1
5 >= 5 is 1
5 >= 10 is 0
5 <= 5 is 1
5 <= 10 is 1

Example 5: Logical Operators

// Working of logical operators

#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;

result = (a == b) && (c > b);


printf("(a == b) && (c > b) is %d \n",
result);

result = (a == b) && (c < b);


printf("(a == b) && (c < b) is %d \n",
result);

result = (a == b) || (c < b);


printf("(a == b) || (c < b) is %d \n",
result);

result = (a != b) || (c < b);


printf("(a != b) || (c < b) is %d \n",
result);

result = !(a != b);


printf("!(a != b) is %d \n", result);
result = !(a == b);
printf("!(a == b) is %d \n", result);

return 0;
}

Output
(a == b) && (c > b) is 1
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a != b) is 1
!(a == b) is 0

Example 6: sizeof Operator

#include <stdio.h>
int main()
{
int a;
float b;
double c;
char d;
printf("Size of int=%lu
bytes\n",sizeof(a));
printf("Size of float=%lu
bytes\n",sizeof(b));
printf("Size of double=%lu
bytes\n",sizeof(c));
printf("Size of char=%lu
byte\n",sizeof(d));

return 0;
}

Output
Size of int = 4 bytes
Size of float = 4 bytes
Size of double = 8 bytes
Size of char = 1 byte

© Parewa Labs Pvt. Ltd. All rights reserved.


#include <math.h>
#include <stdio.h>
int main() {
double a, b, c, discriminant, root1,
root2, realPart, imagPart;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf", &a, &b, &c);

discriminant = b * b - 4 * a * c;

// condition for real and different roots


if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2
* a);
root2 = (-b - sqrt(discriminant)) / (2
* a);
printf("root1 = %.2lf and root2 =
%.2lf", root1, root2);
}

// condition for real and equal roots


else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
printf("root1 = root2 = %.2lf;",
root1);
}

// if roots are not real


else {
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 *
a);
printf("root1 = %.2lf+%.2lfi and root2
= %.2f-%.2fi", realPart, imagPart, realPart,
imagPart);
}

return 0;
}

Output
Enter coefficients a, b and c: 2.3
4
5.6
root1 = -0.87+1.30i and root2 = -0.87-1.30i

2.program to reverse the digits

int main(){
int num = 1024;

while(num != 0){
int digit = num % 10;
num = num / 10;
printf("%d\n", digit);
}
return 0;
}

#include <stdio.h>
int main() {
int n, rev = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n);
while (n != 0) {
remainder = n % 10;
rev = rev * 10 + remainder;
n /= 10;
}
printf("Reversed number = %d", rev);
return 0;
}

Output
Enter an integer: 2345
Reversed number = 5432

Apps

3.program for sum of digits

Sum of digits of a number in C

include <stdio.h>
int main()
{
int n, t, sum = 0, remainder;

printf("Enter an integer\n");
scanf("%d", &n);

t = n;

while (t != 0)
{
remainder = t % 10;
sum = sum + remainder;
t = t / 10;
}

printf("Sum of digits of %d =
%d\n", n, sum);

return 0;
}

C program to find sum of digits using for loop


#include <stdio.h>
int main()
{
int n, sum = 0, r;

printf("Enter a number\n");

for (scanf("%d", &n); n != 0; n = n/10) {


r = n % 10;
sum = sum + r;
}
printf("Sum of digits of a number =
%d\n", sum);

return 0;
}

Calculate sum of digits in C without modulus operator

int main()
{
int c, sum, t;
char n[1000];

printf("Input an integer\n");
scanf("%s", n);

sum = c = 0;

while (n[c] != '\0') {


t = n[c] - '0'; // Converting
character to integer
sum = sum + t;
c++;
}

printf("Sum of digits of %s =
%d\n", n, sum);

return 0;
}
Output of program:
Input an integer
9876543210
Sum of digits of 9876543210 = 45
C program to find sum of digits of a number using recursion
#include <stdio.h>
int add_digits(int);
int main()
{
int n, result;

scanf("%d", &n);

result = add_digits(n);

printf("%d\n", result);

return 0;
}

int add_digits(int n) {
if (n == 0) // Base case
return 0;

return (n%10 + add_digits(n/10));


}

4. program to print multiplication table


Program #1 : Write a c program to print multiplication table using
for loop.Take input from user.

1. #include <stdio.h>
2. int main()
3. {
4. int n, i;
5.
6. printf("Enter a Number ");
7. scanf("%d",&n);
8.
9. for(i=1; i<=10; ++i)
10. {
11. printf("%d * %d = %d \n", n, i, n*i);
12. }
13.
14. getch();
15.
16. }

Output:

Program #2: Write a c program to print multiplication table using


while loop
write a c program to print multiplication table of any number using
for loop

1. #include <stdio.h>
2. int main()
3. {
4. int n, i;
5.
6. printf("Enter a Number ");
7. scanf("%d",&n);
8. i=1;
9. while(i<=10){
10.
11. printf("%d * %d = %d \n", n, i, n*i);
12. ++i;
13. }
14.
15. getch();
16.
17. }

Output:

1. Enter a Number
2. 2
3. 2 * 1 = 2
4. 2 * 2 = 4
5. 2 * 3 = 6
6. 2 * 4 = 8
7. 2 * 5 = 10
8. 2 * 6 = 12
9. 2 * 7 = 14
10. 2 * 8 = 16
11. 2 * 9 = 18
12. 2 * 10 = 20

4. program to print Armstrong number


Check Armstrong Number of three digits

#include <stdio.h>
int main() {
int num, originalNum, remainder, result =
0;
printf("Enter a three-digit integer: ");
scanf("%d", &num);
originalNum = num;

while (originalNum != 0) {
// remainder contains the last digit
remainder = originalNum % 10;

result += remainder * remainder *


remainder;

// removing last digit from the orignal


number
originalNum /= 10;
}

if (result == num)
printf("%d is an Armstrong number.",
num);
else
printf("%d is not an Armstrong
number.", num);

return 0;
}

Output
Enter a three-digit integer: 371
371 is an Armstrong number.

Check Armstrong Number of n digits


#include <math.h>
#include <stdio.h>

int main() {
int num, originalNum, remainder, n = 0;
float result = 0.0;

printf("Enter an integer: ");


scanf("%d", &num);

originalNum = num;

// store the number of digits of num in n


for (originalNum = num; originalNum != 0;
++n) {
originalNum /= 10;
}

for (originalNum = num; originalNum != 0;


originalNum /= 10) {
remainder = originalNum % 10;

// store the sum of the power of


individual digits in result
result += pow(remainder, n);
}

// if num is equal to result, the number is


an Armstrong number
if ((int)result == num)
printf("%d is an Armstrong number.", num);
else
printf("%d is not an Armstrong number.",
num);
return 0;
}
Output
Enter an integer: 1634
1634 is an Armstrong number.

5.program to print magic numbers


1. #include <stdio.h>
2. #include <conio.h>
3.
4. int main ()
5. {
6. // declare integer variables
7. int n, temp, rev = 0, digit, sum_of_digits = 0;
8.
9. printf (" Enter a Number: \n");
10. scanf (" %d", &n); // get the number
11.
12. temp = n; // assign the number to temp variable
13.
14. // use while loop to calculate the sum of digits
15. while ( temp > 0)
16. {
17. // extract digit one by one and store into the sum_of_digits
18. sum_of_digits = sum_of_digits + temp % 10; /* use modulus symbol t
*/
19. temp = temp / 10;
20. }
21.
22. temp = sum_of_digits; // assign the sum_of_digits to temp variable
23. printf (" \n The sum of the digits = %d", temp);
24.
25. // get the reverse sum of given digits
26. while ( temp > 0)
27. {
28. rev = rev * 10 + temp % 10;
29. temp = temp / 10;
30. }
31.
32. printf (" \n The reverse of the digits = %d", rev);
33.
34.
35. printf (" \n The product of %d * %d = %d", sum_of_digits, rev, rev * su
36. // use if else statement to check the magic number
37. if ( rev * sum_of_digits == n)
38. {
39. printf (" \n %d is a Magic Number. ", n);
40. }
41. else
42. {
43. printf (" \n %d is not a Magic Number. ", n);
44. }
45. return 0;
46.
47. }

When we execute the above program in the C compiler, it produces the below ou
Enter a number
1729
The sum of the digits = 19
The reverse of the digits = 91
The product of 19 * 91 = 1729
1729 is a Magic Number.

Example 2: Program to check whether the number is a Magic number using the

1. /* program to check the magic number using the user-defined function in


2. #include <stdio.h>
3. #include <conio.h>
4.
5. // create getReverse () function to get the reverse of the given number
6. int getReverse (int num)
7. {
8. int revNum = 0;
9.
10. //use while loop to get the reverse of original nummber (n)
11. while ( num > 0)
12. {
13. revNum = (revNum * 10) + (num % 10);
14. num = num / 10;
15. }
16. printf (" \n The reverse of the number = %d", revNum);
17. return revNum;
18. }
19.
20. // create getSum() function to get the sum of each digits of the given numb
21. int getSum ( int num)
22. {
23. int sum = 0; // initialize the sum variable with 0
24. while ( num > 0)
25. {
26. sum = sum + ( num % 10);
27. num = num / 10;
28. }
29. printf ( " \n The sum of the given number = %d", sum);
30. return sum;
31. }
32.
33. int main ()
34. {
35. // declare variable
36. int num, sum, revNum;
37. printf (" Enter the number: ");
38. scanf (" %d", &num);
39.
40. sum = getSum (num); // call getSum () function
41. revNum = getReverse (sum); // callgetReverse () function and argument
42.
43. int result = sum * revNum;
44. printf (" \n The product of %d and %d = %d ", sum, revNum, result);
45.
46. // use if else to check condition
47. if (sum * revNum == num)
48. {
49. printf (" \n %d is a magic number. ", num);
50. }
51. else
52. {
53. printf ( " \n %d is not a magic number. ", num);
54. }
55. }

When we execute the above program in the C compiler, it produces the below ou
Enter the number: 1729
The sum of the given number = 19
The reverse of the number = 91
The product of 19 and 91 = 1729
1729 is a Magic Number.

2nd execution:
Enter the number: 5189
The sum of the given number = 23
The reverse of the number = 32
The product of 23 and 32 = 736
5189 is not a magic number.
1. /* create a program to get the magic number by dividing the given number
2. #include <stdio.h>
3. #include <conio.h>
4. int main ()
5. {
6. // declare integer variable
7. int num;
8. printf ( " Enter the number: ");
9. scanf (" %d", &num);
10.
11. // get the modulus of the input number by 9 to check magic number
12. if ( num % 9 == 1)
13. {
14. printf ( " It is a Magic Number. ");
15. }
16. else
17. {
18. printf ( " it is not a Magic Number. ");
19. }
20. return 0;
21. }

When we execute the above program in the C compiler, it produces the below ou
Enter the number: 1789
It is a Magic Number.

2nd execution:
Enter the number: 55057
It is not a Magic Number.

5.program to fing given number is odd r even

1. #include<stdio.h>
2. int main(){
3. int n,i,m=0,flag=0;
4. printf("Enter the number to check prime:");
5. scanf("%d",&n);
6. m=n/2;
7. for(i=2;i<=m;i++)
8. {
9. if(n%i==0)
10. {
11. printf("Number is not prime");
12. flag=1;
13. break;
14. }
15. }
16. if(flag==0)
17. printf("Number is prime");
18. return 0;
19. }

Output:
Enter the number to check prime:56
Number is not prime

Enter the number to check prime:23


Number is prime

5.program to print cosine series

Javatpoint

Program code for Cosine Series in C:


?
#include<stdio.h>
1
#include<conio.h>
2

3
void main()
4

5 {

6 int i, n;

7 float x, sum=1, t=1;

8 clrscr();

10 printf(" Enter the value for x : ");

11 scanf("%f",&x);

12

13 printf(" Enter the value for n : ");


14
scanf("%d",&n);
15

16
x=x*3.14159/180;
17
18 /* Loop to calculate the value of Cosine */

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

20 {

21 t=t*(-1)*x*x/(2*i*(2*i-1));
22
sum=sum+t;
23
}
24

25
printf(" The value of Cos(%f) is : %.4f", x,
26 sum);

27 getch();

6.program for sine series

Program code for Sine Series in C:


?
1 #include<stdio.h>

2 #include<conio.h>
3

4 void main()

5 {

6 int i, n;

7 float x, sum, t;
8
clrscr();
9

10
printf(" Enter the value for x : ");
11
scanf("%f",&x);
12

13
printf(" Enter the value for n : ");
14
scanf("%d",&n);
15

16
x=x*3.14159/180;
17
t=x;
18
sum=x;
19

20
21 /* Loop to calculate the value of Sine */

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

23 {

24 t=(t*(-1)*x*x)/(2*i*(2*i+1));
25
sum=sum+t;
26
}
27

28
printf(" The value of Sin(%f) = %.4f",x,sum);
29
getch();


7.program to convert binary to decimal
Example 1: C Program to Convert Binary Number to Decimal

// convert binary to decimal

#include <stdio.h>
#include <math.h>

// function prototype
int convert(long long);

int main() {
long long n;
printf("Enter a binary number: ");
scanf("%lld", &n);
printf("%lld in binary = %d in decimal", n,
convert(n));
return 0;
}

// function definition
int convert(long long n) {
int dec = 0, i = 0, rem;

while (n!=0) {
rem = n % 10;
n /= 10;
dec += rem * pow(2, i);
++i;
}

return dec;
}

Output
Enter a binary number: 1101
1101 in binary = 13 in decimal

Example 1: Program to Convert Decimal to Octal

#include <stdio.h>
#include <math.h>

int convertDecimalToOctal(int decimalNumber);


int main()
{
int decimalNumber;
printf("Enter a decimal number: ");
scanf("%d", &decimalNumber);

printf("%d in decimal = %d in octal",


decimalNumber,
convertDecimalToOctal(decimalNumber));

return 0;
}

int convertDecimalToOctal(int decimalNumber)


{
int octalNumber = 0, i = 1;

while (decimalNumber != 0)
{
octalNumber += (decimalNumber % 8) *
i;
decimalNumber /= 8;
i *= 10;
}

return octalNumber;
}
Enter a decimal number: 78
78 in decimal = 116 in octal

Example 2: Program to Convert Octal to Decimal

#include <stdio.h>
#include <math.h>

long Output
long convertOctalToDecimal(int octalNumber);
int main()
{
int octalNumber;

printf("Enter an octal number: ");


scanf("%d", &octalNumber);

printf("%d in octal = %lld in decimal",


octalNumber,
convertOctalToDecimal(octalNumber));

return 0;
}

long long convertOctalToDecimal(int


octalNumber)
{
int decimalNumber = 0, i = 0;

while(octalNumber != 0)
{
decimalNumber += (octalNumber%10) *
pow(8,i);
++i;
octalNumber/=10;
}

i = 1;

return decimalNumber;
}

Output
Enter an octal number: 116
116 in octal = 78 in decimal

8.program to convert from hexadecimal to decimal


number system:

#include <stdio.h>
#include <math.h>
#include <string.h>
#define ARRAY_SIZE 20
int main()
{
char hex[ARRAY_SIZE];
long long decimal = 0, base = 1;
int i = 0, value, length;
/* Get hexadecimal value from user */
printf("Enter hexadecimal number: ");
fflush(stdin);
fgets(hex,ARRAY_SIZE,stdin);
length = strlen(hex);
for(i = length--; i >= 0; i--)
{
if(hex[i] >= '0' && hex[i] <= '9')
{
decimal += (hex[i] - 48) * base;
base *= 16;
}
else if(hex[i] >= 'A' && hex[i] <= 'F')
{
decimal += (hex[i] - 55) * base;
base *= 16;
}
else if(hex[i] >= 'a' && hex[i] <= 'f')
{
decimal += (hex[i] - 87) * base;
base *= 16;
}
}
printf("\nHexadecimal number = %s", hex);
printf("Decimal number = %lld\n", decimal);
return 0;
}
Output:

Enter hexadecimal number: 1A


Hexadecimal number = 1A
Decimal number = 26

9.program to for pascal triangle

1. /*
2. * C Program to Generate Pascal
Triangle 1 D Array
3. */
4. #include <stdio.h>
5.
6. void main()
7. {
8. int array[30], temp[30], i, j, k,
l, num; //using 2 arrays
9.
10. printf("Enter the number of lines
to be printed: ");
11. scanf("%d", &num);
12. temp[0] = 1;
13. array[0] = 1;
14. for (j = 0; j < num; j++)
15. printf(" ");
16. printf(" 1\n");
17. for (i = 1; i < num; i++)
18. {
19. for (j = 0; j < i; j++)
20. printf(" ");
21. for (k = 1; k < num; k++)
22. {
23. array[k] = temp[k - 1] +
temp[k];
24. }
25. array[i] = 1;
26. for (l = 0; l <= i; l++)
27. {
28. printf("%3d", array[l]);
29. temp[l] = array[l];
30. }
31. printf("\n");
32. }
33. }
$ cc pgm69.c
$ a.out
Enter the number of lines to be printed: 4
1
1 1
1 2 1
1 3 3 1

Example 1: Half Pyramid of *

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

C Program
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= i; ++j) {
printf("* ");
}
printf("\n");
}
return 0;
}

Example 2: Half Pyramid of Numbers

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

C Program
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= i; ++j) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}

Example 3: Half Pyramid of Alphabets

A
B B
C C C
D D D D
E E E E E

C Program
#include <stdio.h>
int main() {
int i, j;
char input, alphabet = 'A';
printf("Enter an uppercase character you
want to print in the last row: ");
scanf("%c", &input);
for (i = 1; i <= (input - 'A' + 1); ++i) {
for (j = 1; j <= i; ++j) {
printf("%c ", alphabet);
}
++alphabet;
printf("\n");
}
return 0;
}
Example 4: Inverted half pyramid of *

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

C Program
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = rows; i >= 1; --i) {
for (j = 1; j <= i; ++j) {
printf("* ");
}
printf("\n");
}
return 0;
}

Example 5: Inverted half pyramid of numbers

1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

C Program
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = rows; i >= 1; --i) {
for (j = 1; j <= i; ++j) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}

Example 6: Full Pyramid of *

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

C Program
#include <stdio.h>
int main() {
int i, space, rows, k = 0;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i, k = 0) {
for (space = 1; space <= rows - i;
++space) {
printf(" ");
}
while (k != 2 * i - 1) {
printf("* ");
++k;
}
printf("\n");
}
return 0;
}

Example 7: Full Pyramid of Numbers

1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5

C Program
#include <stdio.h>
int main() {
int i, space, rows, k = 0, count = 0,
count1 = 0;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (space = 1; space <= rows - i;
++space) {
printf(" ");
++count;
}
while (k != 2 * i - 1) {
if (count <= rows - 1) {
printf("%d ", i + k);
++count;
} else {
++count1;
printf("%d ", (i + k - 2 *
count1));
}
++k;
}
count1 = count = k = 0;
printf("\n");
}
return 0;
}

Example 8: Inverted full pyramid of *

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

C Program
#include <stdio.h>
int main() {
int rows, i, j, space;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = rows; i >= 1; --i) {
for (space = 0; space < rows - i;
++space)
printf(" ");
for (j = i; j <= 2 * i - 1; ++j)
printf("* ");
for (j = 0; j < i - 1; ++j)
printf("* ");
printf("\n");
}
return 0;
}

Example 9: Pascal's Triangle

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1

C Program
#include <stdio.h>
int main() {
int rows, coef = 1, space, i, j;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 0; i < rows; i++) {
for (space = 1; space <= rows - i;
space++)
printf(" ");
for (j = 0; j <= i; j++) {
if (j == 0 || i == 0)
coef = 1;
else
coef = coef * (i - j + 1) / j;
printf("%4d", coef);
}
printf("\n");
}
return 0;
}

Example 10: Floyd's Triangle.

1
2 3
4 5 6
7 8 9 10

C Program
#include <stdio.h>
int main() {
int rows, i, j, number = 1;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++) {
for (j = 1; j <= i; ++j) {
printf("%d ", number);
++number;
}
printf("\n");
}
return 0;
}

Share on:
Did you find this article helpful?

9.program for factorial using loop

Factorial Program using loop

Let's see the factorial Program using loop.

1. #include<stdio.h>
2. int main()
3. {
4. int i,fact=1,number;
5. printf("Enter a number: ");
6. scanf("%d",&number);
7. for(i=1;i<=number;i++){
8. fact=fact*i;
9. }
10. printf("Factorial of %d is: %d",number,fact);
11. return 0;
12. }

Output:
Enter a number: 5
Factorial of 5 is: 120

10.program for using factorial using recursion

Factorial Program using recursion in C

Let's see the factorial program in c using recursion.

1. #include<stdio.h>
2.
3. long factorial(int n)
4. {
5. if (n == 0)
6. return 1;
7. else
8. return(n * factorial(n-1));
9. }
10.
11. void main()
12. {
13. int number;
14. long fact;
15. printf("Enter a number: ");
16. scanf("%d", &number);
17.
18. fact = factorial(number);
19. printf("Factorial of %d is %ld\n", number, fact);
20. return 0;
21. }

Output:
Enter a number: 6
Factorial of 5 is: 720
10.program to print Fibonacci series

Fibonacci Series up to n terms

#include <stdio.h>
int main() {

int i, n;

// initialize first and second terms


int t1 = 0, t2 = 1;

// initialize the next term (3rd term)


int nextTerm = t1 + t2;

// get no. of terms from user


printf("Enter the number of terms: ");
scanf("%d", &n);

// print the first two terms t1 and t2


printf("Fibonacci Series: %d, %d, ", t1,
t2);

// print 3rd to nth terms


for (i = 3; i <= n; ++i) {
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
return 0;
}

Output
Enter the number of terms: 10
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21,
34,

Fibonacci Sequence Up to a Certain Number

#include <stdio.h>
int main() {
int t1 = 0, t2 = 1, nextTerm = 0, n;
printf("Enter a positive number: ");
scanf("%d", &n);

// displays the first two terms which is


always 0 and 1
printf("Fibonacci Series: %d, %d, ", t1,
t2);
nextTerm = t1 + t2;

while (nextTerm <= n) {


printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}

return 0;
}

Output
Enter a positive integer: 100
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21,
34, 55, 89,

© Parewa Labs Pvt. Ltd. All rights reserved.

C program to print Fibonacci series program


using recursive methods
#include<stdio.h>
#include<conio.h>
int fibonacci(int);
int main(){
int n, i;
printf("Enter the number of element you want
in series :\n");
scanf("%d",&n);
printf("fibonacci series is : \n");
for(i=0;i<n;i++) {
printf("%d ",fibonacci(i));
}
getch();
}

int fibonacci(int i){


if(i==0) return 0;
else if(i==1) return 1;
else return (fibonacci(i-1)+fibonacci(i-2));
}

Output:

GCD of Two Numbers using Recursion

#include <stdio.h>
int hcf(int n1, int n2);
int main() {
int n1, n2;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
printf("G.C.D of %d and %d is %d.", n1,
n2, hcf(n1, n2));
return 0;
}

int hcf(int n1, int n2) {


if (n2 != 0)
return hcf(n2, n1 % n2);
else
return n1;
}

Output
Enter two positive integers: 366
60
G.C.D of 366 and 60 is 6.

10.program to calculate standard deviation

C Program To Calculate Standard Deviation


// C Program To Calculate Standard Deviation
#include <stdio.h>
#include <math.h>
int main(){
int i, n;
float num[25];
float sum = 0.0, mean, SD;
// Asking for input
printf("Enter total number of elements: ");
scanf("%d", &n);
printf("Enter the value of elements: \n");
for (i = 0; i < n; i++ )
scanf("%f", &num[i]);
// Calculating Mean
for (i = 0; i < n; i++)
sum += num[i];
mean = sum / n;
// Calculating Standard Deviation
sum = 0.0;
for (i = 0; i < n; i++)
sum += (num[i] - mean) * (num[i] - mean);
SD = sqrt(sum / n);
printf("Mean: %6.3f \n", mean);
printf("Standard Deviation: %.6f", SD);
return 0;
}
Output
Enter total number of elements: 10
Enter the value of elements:
1
2
3
4
5
6
7
8
9
10
Mean: 5.500
Standard Deviation: 2.872281
1 #include <stdio.h>
2 #include <conio.h>
3
4
5 int main()
6 {
7 int a[1000],i,n,min,max;
8
9 printf("Enter size of the array : ");
10 scanf("%d",&n);
11
12 printf("Enter elements in array : ");
13 for(i=0; i<n; i++)
14 {
15 scanf("%d",&a[i]);
16 }
17
18 min=max=a[0];
19 for(i=1; i<n; i++)
20 {
21 if(min>a[i])
22 min=a[i];
23 if(max<a[i])
24 max=a[i];
25 }
26 printf("minimum of array is : %d",min);
27 printf("\nmaximum of array is : %d",max);
28
29
30 return 0;
31 }
Output:
1 Enter size of the array: 5
2 Enter elements in array: 1
32
43
54
65
7 minimum of an array is: 1
8 maximum of an array is: 5

9.program for find max and min of an array

1 #include <stdio.h>
2 #include <conio.h>
3
4 int sumofarray(int a[],int n)
5 {
6 int min,max,i;
7 min=max=a[0];
8 for(i=1; i<n; i++)
9 {
10 if(min>a[i])
11 min=a[i];
12 if(max<a[i])
13 max=a[i];
14 }
15
16 printf("minimum of array is : %d",min);
17 printf("\nmaximum of array is : %d",max);
18 }
19 int main()
20 {
21 int a[1000],i,n,sum;
22
23 printf("Enter size of the array : ");
24 scanf("%d", &n);
25
26 printf("Enter elements in array : ");
27 for(i=0; i<n; i++)
28 {
29 scanf("%d",&a[i]);
30 }
31 sumofarray(a,n);
32
33
34
35 }
Output:
Enter size of the array: 5
1
Enter elements in array: 1
2
2
3
35
4
0
5
-1
6
minimum of an array is: -1
7
maximum of an array is: 35
8
10.program to find minimum of given array

1 #include <stdio.h>
2 #include <conio.h>
3
4 int minimum(int a[],int n,int i)
5 {
6 static int min=0;;
7 if(i<n)
8 {
9 if(a[min]>a[i])
10 {
11 min=i;
12 minimum(a,n,++i);
13
14 }
15 }
16
17 return min;
18
19 }
20 int maximum(int a[],int n,int i)
21 {
22 static int max=0;;
23 if(i<n)
24 {
25 if(a[max]<a[i])
26 {
27 max=i;
28 maximum(a,n,++i);
29
30 }
31 }
32
33 return max;
34
35 }
36 int main()
37 {
38 int a[1000],i,n,sum;
39
40 printf("Enter size of the array : ");
41 scanf("%d", &n);
42
43 printf("Enter elements in array : ");
44 for(i=0; i<n; i++)
45 {
46 scanf("%d", &a[i]);
47 }
48
49
50
51 printf("minimum of array is : %d",a[(minimum(a,n,1))]);
52
53 printf("\nmaximum of array is : %d",a[(maximum(a,n,1))]);
54
55
56
57 }
Output:
1 Enter size of the array: 5
2 Enter elements in array: 9
38
47
56
60
7 minimum of an array is : 0
8 maximum of an array is : 9

10.program to print array in reverse order

* C program to print array in reverse order


*/

#include <stdio.h>
#define MAX_SIZE 100 // Defines maximum size
of array

int main()
{
int arr[MAX_SIZE];
int size, i;

/* Input size of array */


printf("Enter size of the array: ");
scanf("%d", &size);

/* Input array elements */


printf("Enter elements in array: ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}

/*
* Print array in reversed order
*/
printf("\nArray in reverse order: ");
for(i = size-1; i>=0; i--)
{
printf("%d\t", arr[i]);
}

return 0;
}

Program to find reverse of array


/**
* C program to find reverse of array
*/

#include <stdio.h>
#define MAX_SIZE 100 // Maximum array size

int main()
{
int arr[MAX_SIZE], reverse[MAX_SIZE];
int size, i, arrIndex, revIndex;

/* Input size of the array */


printf("Enter size of the array: ");
scanf("%d", &size);

/* Input array elements */


printf("Enter elements in array: ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}

revIndex = 0;
arrIndex = size - 1;
while(arrIndex >= 0)
{
/* Copy value from original array to
reverse array */
reverse[revIndex] = arr[arrIndex];

revIndex++;
arrIndex--;
}

/*
* Print the reversed array
*/
printf("\nReversed array : ");
for(i=0; i<size; i++)
{
printf("%d\t", reverse[i]);
}

return 0;
}
.

Program to reverse array without using another


array
/**
* C program to reverse an array without using
second array
*/

#include <stdio.h>
#define MAX_SIZE 100 // Maximum array size
int main()
{
int arr[MAX_SIZE];
int size, i, arrIndex, revIndex;
int temp; // Used for swapping

/* Input size of the array */


printf("Enter size of the array: ");
scanf("%d", &size);

/* Input array elements */


printf("Enter elements in array: ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}

revIndex = 0;
arrIndex = size - 1;
while(revIndex < arrIndex)
{
/* Copy value from original array to
reverse array */
temp = arr[revIndex];
arr[revIndex] = arr[arrIndex];
arr[arrIndex] = temp;

revIndex++;
arrIndex--;
}

/*
* Print reversed array
*/
printf("\nReversed array : ");
for(i=0; i<size; i++)
{
printf("%d\t", arr[i]);
}

return 0;
}
Learn more about program to find reverse of number.
Output
Enter size of the array: 5
Enter elements in array: 10 5 16 35 500

Reversed array : 500 35 16 5 10

Example 1: Program to remove the duplicate elements from an


array

1. #include <stdio.h>
2. #include <conio.h>
3. int main ()
4. {
5. // declare local variables
6. int arr[20], i, j, k, size;
7.
8. printf (" Define the number of elements in an array: ");
9. scanf (" %d", &size);
10.
11. printf (" \n Enter %d elements of an array: \n ", size);
12. // use for loop to enter the elements one by one in an array
13. for ( i = 0; i < size; i++)
14. {
15. scanf (" %d", &arr[i]);
16. }
17.
18.
19. // use nested for loop to find the duplicate elements in array

20. for ( i = 0; i < size; i ++)


21. {
22. for ( j = i + 1; j < size; j++)
23. {
24. // use if statement to check duplicate element
25. if ( arr[i] == arr[j])
26. {
27. // delete the current position of the duplicate element

28. for ( k = j; k < size - 1; k++)


29. {
30. arr[k] = arr [k + 1];
31. }
32. // decrease the size of array after removing duplicate
element
33. size--;
34.
35. // if the position of the elements is changes, don't incre
ase the index j
36. j--;
37. }
38. }
39. }
40.
41.
42. /* display an array after deletion or removing of the duplicat
e elements */
43. printf (" \n Array elements after deletion of the duplicate ele
ments: ");
44.
45. // for loop to print the array
46. for ( i = 0; i < size; i++)
47. {
48. printf (" %d \t", arr[i]);
49. }
50. return 0;
51. }

11.program to add two matrices

Program to Add Two Matrices

#include <stdio.h>
int main() {
int r, c, a[100][100], b[100][100],
sum[100][100], i, j;
printf("Enter the number of rows (between 1
and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between
1 and 100): ");
scanf("%d", &c);

printf("\nEnter elements of 1st matrix:\n");


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j
+ 1);
scanf("%d", &a[i][j]);
}

printf("Enter elements of 2nd matrix:\n");


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element b%d%d: ", i + 1, j
+ 1);
scanf("%d", &b[i][j]);
}

// adding two matrices


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
sum[i][j] = a[i][j] + b[i][j];
}

// printing the result


printf("\nSum of two matrices: \n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("%d ", sum[i][j]);
if (j == c - 1) {
printf("\n\n");
}
}

return 0;
}

Output
Enter the number of rows (between 1 and 100):
2
Enter the number of columns (between 1 and
100): 3

Enter elements of 1st matrix:


Enter element a11: 2
Enter element a12: 3
Enter element a13: 4
Enter element a21: 5
Enter element a22: 2
Enter element a23: 3
Enter elements of 2nd matrix:
Enter element b11: -4
Enter element b12: 5
Enter element b13: 3
Enter element b21: 5
Enter element b22: 6
Enter element b23: 3

Sum of two matrices:


-2 8 7

10 8 6

Tutorials
Matrices by Passing it to a Function

#include <stdio.h>

void enterData(int firstMatrix[][10], int


secondMatrix[][10], int rowFirst, int
columnFirst, int rowSecond, int columnSecond);
void multiplyMatrices(int firstMatrix[][10],
int secondMatrix[][10], int multResult[][10],
int rowFirst, int columnFirst, int rowSecond,
int columnSecond);
void display(int mult[][10], int rowFirst, int
columnSecond);

int main()
{
int firstMatrix[10][10],
secondMatrix[10][10], mult[10][10], rowFirst,
columnFirst, rowSecond, columnSecond, i, j, k;

printf("Enter rows and column for first


matrix: ");
scanf("%d %d", &rowFirst, &columnFirst);

printf("Enter rows and column for second


matrix: ");
scanf("%d %d", &rowSecond, &columnSecond);
// If colum of first matrix in not equal
to row of second matrix, asking user to enter
the size of matrix again.
while (columnFirst != rowSecond)
{
printf("Error! column of first matrix
not equal to row of second.\n");
printf("Enter rows and column for
first matrix: ");
scanf("%d%d", &rowFirst,
&columnFirst);
printf("Enter rows and column for
second matrix: ");
scanf("%d%d", &rowSecond,
&columnSecond);
}

// Function to take matrices data


enterData(firstMatrix, secondMatrix,
rowFirst, columnFirst, rowSecond,
columnSecond);

// Function to multiply two matrices.


multiplyMatrices(firstMatrix,
secondMatrix, mult, rowFirst, columnFirst,
rowSecond, columnSecond);

// Function to display resultant


matrix after multiplication.
display(mult, rowFirst, columnSecond);

return 0;
}
void enterData(int firstMatrix[][10], int
secondMatrix[][10], int rowFirst, int
columnFirst, int rowSecond, int columnSecond)
{
int i, j;
printf("\nEnter elements of matrix 1:\n");
for(i = 0; i < rowFirst; ++i)
{
for(j = 0; j < columnFirst; ++j)
{
printf("Enter elements a%d%d: ",
i + 1, j + 1);
scanf("%d", &firstMatrix[i][j]);
}
}

printf("\nEnter elements of matrix 2:\n");


for(i = 0; i < rowSecond; ++i)
{
for(j = 0; j < columnSecond; ++j)
{
printf("Enter elements b%d%d: ",
i + 1, j + 1);
scanf("%d",
&secondMatrix[i][j]);
}
}
}

void multiplyMatrices(int firstMatrix[][10],


int secondMatrix[][10], int mult[][10], int
rowFirst, int columnFirst, int rowSecond, int
columnSecond)
{
int i, j, k;
// Initializing elements of matrix mult to
0.
for(i = 0; i < rowFirst; ++i)
{
for(j = 0; j < columnSecond; ++j)
{
mult[i][j] = 0;
}
}

// Multiplying matrix firstMatrix and


secondMatrix and storing in array mult.
for(i = 0; i < rowFirst; ++i)
{
for(j = 0; j < columnSecond; ++j)
{
for(k=0; k<columnFirst; ++k)
{
mult[i][j] +=
firstMatrix[i][k] * secondMatrix[k][j];
}
}
}
}

void display(int mult[][10], int rowFirst, int


columnSecond)
{
int i, j;
printf("\nOutput Matrix:\n");
for(i = 0; i < rowFirst; ++i)
{
for(j = 0; j < columnSecond; ++j)
{
printf("%d ", mult[i][j]);
if(j == columnSecond - 1)
printf("\n\n");
}
}
}

Output
Enter rows and column for first matrix: 3
2
Enter rows and column for second matrix: 3
2
Error! column of first matrix not equal to row
of second.

Enter rows and column for first matrix: 2


3
Enter rows and column for second matrix: 3
2

Enter elements of matrix 1:


Enter elements a11: 3
Enter elements a12: -2
Enter elements a13: 5
Enter elements a21: 3
Enter elements a22: 0
Enter elements a23: 4

Enter elements of matrix 2:


Enter elements b11: 2
Enter elements b12: 3
Enter elements b21: -9
Enter elements b22: 0
Enter elements b31: 0
Enter elements b32: 4

Output Matrix:
24 29
6 25

12.program for transpose of amatrix


Program to Find the Transpose of a Matrix

#include <stdio.h>
int main() {
int a[10][10], transpose[10][10], r, c;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);

// asssigning elements to the matrix


printf("\nEnter matrix elements:\n");
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j +
1);
scanf("%d", &a[i][j]);
}

// printing the matrix a[][]


printf("\nEntered matrix: \n");
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
printf("%d ", a[i][j]);
if (j == c - 1)
printf("\n");
}

// computing the transpose


for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
transpose[j][i] = a[i][j];
}

// printing the transpose


printf("\nTranspose of the matrix:\n");
for (int i = 0; i < c; ++i)
for (int j = 0; j < r; ++j) {
printf("%d ", transpose[i][j]);
if (j == r - 1)
printf("\n");
}
return 0;
}

Output
Enter rows and columns: 2
3

Enter matrix elements:


Enter element a11: 1
Enter element a12: 4
Enter element a13: 0
Enter element a21: -5
Enter element a22: 2
Enter element a23: 7

Entered matrix:
1 4 0
-5 2 7

Transpose of the matrix:


1 -5
4 2
0 7
td. All rights reserved.

Program to Add Two Matrices

#include <stdio.h>
int main() {
int r, c, a[100][100], b[100][100],
sum[100][100], i, j;
printf("Enter the number of rows (between 1
and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between
1 and 100): ");
scanf("%d", &c);

printf("\nEnter elements of 1st matrix:\n");


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j
+ 1);
scanf("%d", &a[i][j]);
}

printf("Enter elements of 2nd matrix:\n");


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element b%d%d: ", i + 1, j
+ 1);
scanf("%d", &b[i][j]);
}

// adding two matrices


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
sum[i][j] = a[i][j] + b[i][j];
}

// printing the result


printf("\nSum of two matrices: \n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("%d ", sum[i][j]);
if (j == c - 1) {
printf("\n\n");
}
}

return 0;
}

Output
Enter the number of rows (between 1 and 100):
2
Enter the number of columns (between 1 and
100): 3

Enter elements of 1st matrix:


Enter element a11: 2
Enter element a12: 3
Enter element a13: 4
Enter element a21: 5
Enter element a22: 2
Enter element a23: 3
Enter elements of 2nd matrix:
Enter element b11: -4
Enter element b12: 5
Enter element b13: 3
Enter element b21: 5
Enter element b22: 6
Enter element b23: 3

Sum of two matrices:


-2 8 7

10 8 6

In this program, the user is asked to enter the number of


rows r and columns c.

#include<stdio.h>

int main()
{
char name[30];
printf("Enter name: ");
gets(name); //Function to read string
from user.
printf("Name: ");
puts(name); //Function to display
string.
return 0;
}

12.programs on strings

1. #include <stdio.h>
2. #include <string.h>
3. main () {
4. char greeting[20] = "Hello ";
5. char name[] = "Sebastian";
6. strcat(greeting, name);
7. printf("%s\n", greeting);
8. }

1. #include <stdio.h>
2. #include <string.h>
3. main () {
4. char name[] = "Chris";
5. int cmp = strcmp(name, "Chris");
6. if (cmp == 0) {
7. printf("%s\n", "Welcome Superhero!");
8. }
9. else {
10. printf("%s\n", "Sorry, you lack
abilities");
11. }
12. }

13.program for copying strings without using string functions

1 #include<stdio.h>
2 void main()
3 {
4 char str1[100],str2[50];
5 int i;
6
7 printf("Enter string str1\n");
8 gets(str1);
9
10 for(i=0;str1[i]!='\0';i++)
11 {
12 str2[i] = str1[i];
13 }
14 str2[i]='\0';
15 printf("Copied String(str2) is %s",str2);
16 }
Output

Example

Source file

I love programming.
Working with files in C programming is fun.
I am learning C programming at Codeforwin.

Output

Total characters = 106


Total words = 18
Total lines = 3
/**
* C program to count number of characters, words
and lines in a text file.
*/

#include <stdio.h>
#include <stdlib.h>

int main()
{
FILE * file;
char path[100];

char ch;
int characters, words, lines;

/* Input path of files to merge to third file


*/
printf("Enter source file path: ");
scanf("%s", path);
/* Open source files in 'r' mode */
file = fopen(path, "r");

/* Check if file opened successfully */


if (file == NULL)
{
printf("\nUnable to open file.\n");
printf("Please check if file exists and you
have read privilege.\n");

exit(EXIT_FAILURE);
}

/*
* Logic to count characters, words and lines.
*/
characters = words = lines = 0;
while ((ch = fgetc(file)) != EOF)
{
characters++;

/* Check new line */


if (ch == '\n' || ch == '\0')
lines++;

/* Check words */
if (ch == ' ' || ch == '\t' || ch == '\n'
|| ch == '\0')
words++;
}

/* Increment words and lines for last word */


if (characters > 0)
{
words++;
lines++;
}

/* Print file statistics */


printf("\n");
printf("Total characters = %d\n", characters);
printf("Total words = %d\n", words);
printf("Total lines = %d\n", lines);

/* Close files to release resources */


fclose(file);

return 0;
}

Suppose if data\file3.txt contains

I love programming.
Working with files in C programming is fun.
I am learning C programming at Codeforwin.
Output
Enter source file path: data\file3.txt

Total characters = 106


Total words = 18
Total lines = 3

13.program to print student memo using files

C Program

1. #include <stdio.h>
2. int main() {
3. char name[50];
4. int marks,i,n;
5. printf("Enter number of students: ");
6. scanf("%d",&n);
7. FILE *fptr;
8. fptr=(fopen("C:\\student.txt","w"));
9. if(fptr==NULL) {
10. printf("Error!");
11. exit(1);
12. }
13. for (i=0;i<n;++i) {
14. printf("For student%d\nEnter
name: ",i+1);
15. scanf("%s",name);
16. printf("Enter marks: ");
17. scanf("%d",&marks);
18. fprintf(fptr,"\nName: %s
\nMarks=%d \n",name,marks);
19. }
20. fclose(fptr);
21. return 0;
22. }

Example 2

Write a C program to read name and marks of n number of students


from user and store them in a file. If the file previously exits, add
the information of n students.

C Program

1. #include <stdio.h>
2. int main() {
3. char name[50];
4. int marks,i,n;
5. printf("Enter number of students: ");
6. scanf("%d",&n);
7. FILE *fptr;
8. fptr=(fopen("C:\\student.txt","a"));
9. if(fptr==NULL) {
10. printf("Error!");
11. exit(1);
12. }
13. for (i=0;i<n;++i) {
14. printf("For student%d\nEnter
name: ",i+1);
15. scanf("%s",name);
16. printf("Enter marks: ");
17. scanf("%d",&marks);
18. fprintf(fptr,"\nName: %s
\nMarks=%d \n",name,marks);
19. }
20. fclose(fptr);
21. return 0;
22. }

1.

2. #include <stdio.h>
3. struct s {
4. char name[50];
5. int height;
6. };
7. int main() {
8. struct s a[5],b[5];
9. FILE *fptr;
10. int i;
11. fptr=fopen("file.txt","wb");
12. for (i=0;i<5;++i) {
13. fflush(stdin);
14. printf("Enter name: ");
15. gets(a[i].name);
16. printf("Enter height: ");
17. scanf("%d",&a[i].height);
18. }
19. fwrite(a,sizeof(a),1,fptr);
20. fclose(fptr);
21. fptr=fopen("file.txt","rb");
22. fread(b,sizeof(b),1,fptr);
23. for (i=0;i<5;++i) {
24. printf("Name: %s\nHeight:
%d",b[i].name,b[i].height);
25. }
26. fclose(fptr);
27. }

You might also like