Bca C Lab
Bca C Lab
Bca C Lab
#include<stdio.h>
int main()
NOTE: When we are using c division (/) operator the result will completely
#include<stdio.h>
int main()
int a = 7, b = 3;
float floatdiv;
integerdiv = a / b; // dividing 7 by 3
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
return 0;
}
Output
++a = 11
--b = 99
++c = 11.500000
--d = 99.500000
Example 3: Assignment Operators
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
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
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, 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
#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
discriminant = b * b - 4 * a * c;
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
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
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;
}
printf("Enter a number\n");
return 0;
}
int main()
{
int c, sum, t;
char n[1000];
printf("Input an integer\n");
scanf("%s", n);
sum = c = 0;
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;
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:
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
#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;
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.
int main() {
int num, originalNum, remainder, n = 0;
float result = 0.0;
originalNum = num;
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
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.
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
Javatpoint
3
void main()
4
5 {
6 int i, n;
8 clrscr();
11 scanf("%f",&x);
12
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();
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
#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
#include <stdio.h>
#include <math.h>
return 0;
}
while (decimalNumber != 0)
{
octalNumber += (decimalNumber % 8) *
i;
decimalNumber /= 8;
i *= 10;
}
return octalNumber;
}
Enter a decimal number: 78
78 in decimal = 116 in octal
#include <stdio.h>
#include <math.h>
long Output
long convertOctalToDecimal(int octalNumber);
int main()
{
int octalNumber;
return 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
#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:
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
*
* *
* * *
* * * *
* * * * *
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;
}
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;
}
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;
}
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;
}
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
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;
}
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;
}
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
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;
}
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;
}
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?
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
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
#include <stdio.h>
int main() {
int i, n;
Output
Enter the number of terms: 10
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21,
34,
#include <stdio.h>
int main() {
int t1 = 0, t2 = 1, nextTerm = 0, n;
printf("Enter a positive number: ");
scanf("%d", &n);
return 0;
}
Output
Enter a positive integer: 100
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21,
34, 55, 89,
Output:
#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;
}
Output
Enter two positive integers: 366
60
G.C.D of 366 and 60 is 6.
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
#include <stdio.h>
#define MAX_SIZE 100 // Defines maximum size
of array
int main()
{
int arr[MAX_SIZE];
int size, 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;
}
#include <stdio.h>
#define MAX_SIZE 100 // Maximum array size
int main()
{
int arr[MAX_SIZE], reverse[MAX_SIZE];
int size, i, arrIndex, revIndex;
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;
}
.
#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
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
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
#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);
return 0;
}
Output
Enter the number of rows (between 1 and 100):
2
Enter the number of columns (between 1 and
100): 3
10 8 6
Tutorials
Matrices by Passing it to a Function
#include <stdio.h>
int main()
{
int firstMatrix[10][10],
secondMatrix[10][10], mult[10][10], rowFirst,
columnFirst, rowSecond, columnSecond, i, j, k;
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]);
}
}
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.
Output Matrix:
24 29
6 25
#include <stdio.h>
int main() {
int a[10][10], transpose[10][10], r, c;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
Output
Enter rows and columns: 2
3
Entered matrix:
1 4 0
-5 2 7
#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);
return 0;
}
Output
Enter the number of rows (between 1 and 100):
2
Enter the number of columns (between 1 and
100): 3
10 8 6
#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. }
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
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE * file;
char path[100];
char ch;
int characters, words, lines;
exit(EXIT_FAILURE);
}
/*
* Logic to count characters, words and lines.
*/
characters = words = lines = 0;
while ((ch = fgetc(file)) != EOF)
{
characters++;
/* Check words */
if (ch == ' ' || ch == '\t' || ch == '\n'
|| ch == '\0')
words++;
}
return 0;
}
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
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
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. }