Introduction
In this post, I am going to write basic 50 programs in c language which are helpful to all. Here, you can find 50 basic programs on every topic in c programming. So, let’s begin.
C Program to display a simple message
In this program we are displaying a simple message using printf() function.
#include <stdio.h>
#include <stdlib.h>
main( )
{
printf("Welcome to C Programming");
return 0;
}
Output
Welcome to C Programming
C program to read and display a single character using getchar() and putchar()
In this program, we are two unformatted input-output functions i.e. getchar() and putchar(). We can read a single character using getchar() function and display a single character using putchar() function.
Also Read: Reverse a Number using getchar and putchar function in c
#include <stdio.h>
#include <stdlib.h>
main( )
{
char ch;
printf("Enter a character: ");
ch=getchar();
printf("You have entered: ");
putchar(ch);
return 0;
}
Output
Enter a character: A
You have entered: A
C program to read and display a string using gets() and puts() function
In this program, we read a string using gets() function and display the same string using puts() function. Both are unformatted input-output functions.
#include <stdio.h>
#include <stdlib.h>
main( )
{
char str[20];
printf("Enter a string\n");
gets(str);
printf("You have entered: ");
puts(str);
return 0;
}
Output
Enter a string
Vijay
You have entered: Vijay
C Program for addition of two numbers
In this program, we will read two from the user and perform the addition and display the result.
#include <stdio.h>
#include <stdlib.h>
main( )
{
int a,b,c;
printf("Enter two numbers\n");
scanf("%d%d",&a,&b);
c=a+b;
printf("Addition of %d and %d is %d",a,b,c);
return 0;
}
Output
Enter two numbers
5 10
Addition of 5 and 10 is 15
C Program to display message without using semicolon
This is a very interesting program in the basic 50 programs in c. We know semicolon terminates the statement. But, here we are going write a printf() statement in the if statement. See the following program.
#include <stdio.h>
#include <stdlib.h>
main( )
{
if(printf("Welcome to C Programming"))
{
}
return 0;
}
Output
Welcome to C Programming
C Program to calculate simple interest
In this program, we are reading principal amount, rate of interest and number of years and displaying the result.
#include <stdio.h>
#include <stdlib.h>
main( )
{
int p, n ;
float r, si ;
printf("Enter the value of p, n and r\n");
scanf("%d%d%f",&p,&n,&r);
si = p * n * r / 100 ;
printf("Simple Interest = %0.2f",si) ;
return 0;
}
Output
Enter the value of p, n and r
1000 10 7.5
Simple Interest = 750.00
C program to display square of a number
In this program, we are going to find the square of a number.
#include <stdio.h>
#include <stdlib.h>
main( )
{
int a,b;
printf("Enter a number\n");
scanf("%d",&a);
b=a*a;
printf("Square of %d is %d",a,b) ;
return 0;
}
Output
Enter a number
5
Square of 5 is 25
C program to do exponentiation
In this program, we are using a function pow() to find the raised to the power of a number. For this, we have to include math.h header file.
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
main( )
{
int a,b,c;
printf("Enter two numbers\n");
scanf("%d%d",&a,&b);
c=pow(a,b);
printf("%d raised to power %d is %d",a,b,c) ;
return 0;
}
Output
Enter two numbers
2 5
2 raised to power 5 is 32
C program calculate the gross salary
Here, gross salary means the total salary. To calculate the gross salary, we should know the basic salary, dearness allowance, house rent allowance. So, we will read these value from the user and display the result.
#include <stdio.h>
#include <stdlib.h>
main( )
{
float bs,da,hra,gs;
printf("Enter the basic salary, DA and HRA in percentage\n");
scanf("%f%f%f",&bs,&da,&hra);
gs=bs+bs*(da/100)+bs*(hra/100);
printf("Gross Salary is %0.2f",gs) ;
return 0;
}
Suppose, the value of basic salary is 1000, DA is 20% of basic salary and HRA is 30% of basic salary, then the gross salary will be
Enter the basic salary, DA and HRA in percentage
1000
20
30
Gross Salary is 1500.00
C program to print the largest of the two given numbers using if statement
In this program, we are going to find the greatest number between two numbers. But, here we are using only if statement, not an if-else statement.
#include <stdio.h>
#include <stdlib.h>
main( )
{
int a,b,max;
printf("Enter any two numbers\n");
scanf("%d%d",&a,&b);
max=b;
if(a>b)
{
max=a;
}
printf("Largest number between %d and %d is %d",a,b,max);
return 0;
}
As you can observe the above program, we have not used else statement. See the following output.
Enter any two numbers
5 2
Largest number between 5 and 2 is 5
C program to print the largest of the two given numbers using if-else statement
This is the same program as the previous one. But, here I am using if-else statement. Output will remain the same.
#include <stdio.h>
#include <stdlib.h>
main( )
{
int a,b;
printf("Enter any two numbers\n");
scanf("%d%d",&a,&b);
if(a>b)
{
printf("%d is larger than %d",a,b);
}
else
{
printf("%d is larger than %d",b,a);
}
return 0;
}
Now, in the next section, I am writing the same program using conditional operator.
C program to print the largest of the two given numbers using conditional operator
The conditional operator is a ternary operator. It works on three operands. For detail, you can read my post Operators in C with Detailed Explanation.
#include <stdio.h>
#include <stdlib.h>
main( )
{
int a,b,max;
printf("Enter any two numbers\n");
scanf("%d%d",&a,&b);
max=(a>b)?a:b;
printf("Largest number between %d and %d is %d",a,b,max);
return 0;
}
C program to print the largest of three given numbers using logical operators
Instead of two number, we are finding the greatest number amongst three numbers using logical operators. Here, I am using the logical AND (&&) operator.
#include <stdio.h>
#include <stdlib.h>
main( )
{
int a,b,c;
printf("Enter any three numbers\n");
scanf("%d%d%d",&a,&b,&c);
if((a>b)&&(a>c))
{
printf("%d is greatest",a);
}
else if((b>a)&&(b>c))
{
printf("%d is greatest",b);
}
else
{
printf("%d is greatest",c);
}
return 0;
}
See the following output
Enter any three numbers
2
6
4
6 is greatest
C program to print the largest of three given numbers without using logical operators
It is also the same program as above. But, we are not using logical AND operator here. See, the following program.
#include <stdio.h>
#include <stdlib.h>
main( )
{
int a,b,c;
printf("Enter any three numbers\n");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
if(a>c)
{
printf("%d is greatest",a);
}
else
{
printf("%d is greatest",c);
}
}
else
{
if(b>c)
{
printf("%d is greatest",b);
}
else
{
printf("%d is greatest",c);
}
}
return 0;
}
Here, we have used the concept of nested if-else statement.
C program to perfrom basic arithmetic operations
Here, basic arithmetic operations are nothing but addition, subtraction, multiplication and division operations. This is a very interesting program in basic 50 c programs.
#include <stdio.h>
#include <stdlib.h>
main( )
{
float n1,n2,add,sub,mul,div;
printf("Enter any two numbers\n");
scanf("%f%f",&n1,&n2);
add=n1+n2;
sub=n1-n2;
mul=n1*n2;
div=n1/n2; // n2 must not be zero
printf("Addition of %.2f and %.2f is %.2f\n",n1,n2,add);
printf("Subtraction of %.2f and %.2f is %.2f\n",n1,n2,sub);
printf("Multiplication of %.2f and %.2f is %.2f\n",n1,n2,mul);
printf("Division of %.2f and %.2f is %.2f\n",n1,n2,div);
return 0;
}
Output of the above program is given below.
Enter any two numbers
50 2
Addition of 50.00 and 2.00 is 52.00
Subtraction of 50.00 and 2.00 is 48.00
Multiplication of 50.00 and 2.00 is 100.00
Division of 50.00 and 2.00 is 25.00
C program to show the use of increment and decrement operators
This is a very simple program which is showing the use of increment and decrement operators in c programming.
#include<stdio.h>
int main()
{
int a=5,b=10;
printf("a++ = %d and --b = %d",a++,--b);
return 0;
}
In the above program, we have declared two variables, a and b. The initial value of a is 5 and b is 10. In the printf() statement, we are incrementing the value of a and decrementing the value of b and then displaying the value of a and b. See the following output.
a++ = 6 and --b = 9
Leap Year Program in C
In this program, we are just checking whether the entered year is leap year or not.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int year;
printf("Enter any year\n");
scanf("%d",&year);
if(year%400==0)
{
printf("%d is a Leap Year",year);
}
else if(year%100==0)
{
printf("%d is Not a Leap Year",year);
}
else if(year%4==0)
{
printf("%d is a Leap Year",year);
}
else
{
printf("%d is a Leap Year",year);
}
return 0;
}
See the following output
For detailed explanation, please read: Leap Year Program in C
Enter any year
2020
2020 is a Leap Year
Enter any year
1900
1900 is Not a Leap Year
C program to reverse the digits of a number using while loop
In this program, we are reading one number and after reading, we are just reversing the digits of that number.
#include <stdio.h>
int main()
{
int rem,rev=0,n,an;
printf("Enter any Number\n");
scanf("%d",&n);
an=n;
while(n>0)
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
}
printf("Reverse of %d is %d",an,rev);
return 0;
}
See the following output
Enter a Number
12345
The reverse of 12345 is 54321
For detailed explanation, please read: Reverse a Number in C
C program to print sum of squares of first 20 even numbers using for loop
In this c program, we have to find sum of 22+42+62+….+402 using for loop. See the following program.
#include <stdio.h>
#include <stdlib.h>
main( )
{
int sum=0,i=2;
for(i=2;i<=40;i++)
{
if(i%2==0)
{
sum=sum+i*i;
}
}
printf("The sum of first 20 even numbers is %d",sum);
return 0;
}
See the following output.
The sum of first 20 even numbers is 11480
C program to print sum of squares of first 20 even numbers using while loop
This is the same program as the previous one. The only difference is here we have used the while loop in c programming.
#include <stdio.h>
#include <stdlib.h>
main( )
{
int sum=0,i=2;
while(i<=40)
{
if(i%2==0)
{
sum=sum+i*i;
}
i++;
}
printf("The sum of first 20 even numbers is %d",sum);
return 0;
}
The output will remain the same.
C program to print sum of squares of first 20 even numbers using do-while loop
This is also the same program as the previous two programs. Here, we have used do-while loop.
#include <stdio.h>
#include <stdlib.h>
main( )
{
int sum=0,i=2;
do
{
if(i%2==0)
{
sum=sum+i*i;
}
i++;
}
while(i<=40);
printf("The sum of first 20 even numbers is %d",sum);
return 0;
}
Output of this program will also remain the same.
C program to print sum of cubes of first 10 odd numbers using do-while loop
In this c program, we have to perform 13+33+53+73+…+193. That means we have to find the sum of cubes of the first 10 odd numbers.
#include <stdio.h>
#include <stdlib.h>
main( )
{
int sum=0,i=1;
do
{
if(i%2==1)
{
sum=sum+i*i*i;
}
i++;
}
while(i<=20);
printf("The sum of first 10 odd numbers is %d",sum);
return 0;
}
The output of this program is
The sum of first 10 odd numbers is 19900
Also Read: C Program to Find the Sum of Cubes of Elements in an Array
C program to print sum of cubes of first 10 odd numbers using while loop
Here, we are using the while loop.
#include <stdio.h>
#include <stdlib.h>
main( )
{
int sum=0,i=1;
while(i<=20)
{
if(i%2==1)
{
sum=sum+i*i*i;
}
i++;
}
printf("The sum of first 10 odd numbers is %d",sum);
return 0;
}
This c program will print the same output as the previous one.
C program to print sum of cubes of first 10 odd numbers using for loop
This is also the same program but here we are using for loop.
#include <stdio.h>
#include <stdlib.h>
main( )
{
int sum=0,i;
for(i=1;i<=20;i++)
{
if(i%2==1)
{
sum=sum+i*i*i;
}
}
printf("The sum of first 10 odd numbers is %d",sum);
return 0;
}
C program to check whether a number is Armstrong number or not
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,n1,rem,sum=0;
printf("Enter a number\n");
scanf("%d",&n);
n1=n;
while(n>0)
{
rem=n%10;
sum=sum+rem*rem*rem;
n=n/10;
}
if(n1==sum)
{
printf("%d is an armstrong number",n1);
}
else
printf("%d is not an armstrong number",n1);
return 0;
}
Output of the program is
Enter a number
153
153 is an armstrong number
C program to count number of digits in an integer
In this program, we are counting the total number of digits in a number.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num,n,counter=0;
printf("Enter a number\n");
scanf("%d",&num);
n=num;
while(num>0)
{
num=num/10;
counter++;
}
printf("Total digits in %d are %d",n,counter);
return 0;
}
Output of this program is given below.
Enter a number
231456
Total digits in 231456 are 6
Fibonacci series program in c
Before writing this program, please see the following expected output.
Enter value for n
10
1 1 2 3 5 8 13 21 34 55
Here, we are asking for the value of n. So n is nothing but a number of elements to be printed in a fibonacci series.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n1=1,n2=1,n3,n,i;
printf("Enter value for n");
scanf("%d",&n);
printf("%d %d",n1,n2);
for(i=3;i<=n;i++)
{
n3=n1+n2;
printf(" %d",n3);
n1=n2;
n2=n3;
}
return 0;
}
I hope you are enjoying the very basic 50 programs in c programming. Only 23 programs are left. Now, let’s go the next c program.
LCM of two numbers in c
In this program, I am going to find lcm of two numbers using c program.
#include<stdio.h>
int main()
{
int n1,n2,greater,smaller,i=1,lcm;
printf("Enter two numbers\n");
scanf("%d%d",&n1,&n2);
if(n1>n2)
{
greater=n1;
smaller=n2;
}
else
{
greater=n2;
smaller=n1;
}
while(1)
{
lcm = greater * i;
if((lcm%smaller)==0)
{
printf("LCM of %d and %d is %d", n1,n2,lcm);
break;
}
i++;
}
return 0;
}
LCM means Least Common Multiple. See the following output.
Enter two numbers
5
45
LCM of 5 and 45 is 45
C Program to Find GCD of two Numbers
Here, we are finding GCD means HCF of two numbers using c program.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n1,n2,greater,smaller,n3;
printf("Enter two numbers\n");
scanf("%d%d",&n1,&n2);
if(n1>n2)
{
greater=n1;
smaller=n2;
}
else
{
greater=n2;
smaller=n1;
}
while(1)
{
n3=greater%smaller;
if(n3==0)
{
printf("HCF of %d and %d = %d",n1,n2,smaller);
break;
}
greater=smaller;
smaller=n3;
}
return 0;
}
The GCD means Greatest Common Divisor and HCF means Highest Common Factor. Both are the same thing. See the following output.
Enter two numbers
9
45
HCF of 9 and 45 = 9
Palindrome program in c
In this c program, we are going to check whether a number is palindrome or not.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n1,n2,rem,num=0;
printf("Enter a number\n");
scanf("%d",&n1);
n2=n1;
while(n2>0)
{
rem=n2%10;
num=num*10+rem;
n2=n2/10;
}
if(n1==num)
{
printf("%d is a palindrome",n1);
}
else
{
printf("%d is a not palindrome",n1);
}
return 0;
}
See the following output.
Enter a number
12321
12321 is a palindrome
Palindrome program in c using pointer
Here, we are checking whether a string is palindrome or not but using pointer.
#include <stdio.h>
#include <string.h>
int main()
{
char str[30],*p1,*p2;
printf("Enter any string\n");
gets(str);
p1=str+strlen(str);
p2=str;
p1--;
while(p1>=p2)
{
if(*p1==*p2)
{
p1--;
p2++;
}
else
{
break;
}
}
if(p2>p1)
{
printf("The string %s is a Palindrome",str);
}
else
{
printf("The string %s is not a Palindrome",str);
}
return 0;
}
See the following output.
Enter any string
aabaa
The string aabaa is a Palindrome
Perfect number in c
In this program, we are checking whether a number is perfect number or not.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num1,num2=0,i;
printf("Enter any number\n");
scanf("%d",&num1);
for(i=1;i<num1;i++)
{
if((num1%i)==0)
{
num2=num2+i;
}
}
if(num1==num2)
{
printf("%d is the perfect number",num1);
}
else
{
printf("%d is not the perfect number",num1);
}
return 0;
}
Output of this program is given below
Enter any number
6
6 is the perfect number
C program to eliminate repeated letters in string
Before writing this program, let us see its output first so you can get the idea.
Enter your string:
Hello, welcome to c programming
Your output is Helo,wcmtprgain
As you can see the above output, we have eliminated all repeated characters. Now, see the following program.
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
char s1[100];
char s2[100];
char *r1,*r2,*r3;
int i=0,a=0;
printf("Enter your string:\n");
gets(s1);
while(s1[i]!='\0')
{
r1=strchr(s2,s1[i]+32);
r2=strchr(s2,s1[i]-32);
r3=strchr(s2,s1[i]);
if((r1==NULL)&&(r2==NULL)&&(r3==NULL))
{
s2[a]=s1[i];
a++;
}
i++;
}
s2[a]='\0';
printf("Your output is %s",s2);
return 0;
}
Program in C to find the Average Marks obtained by a class of ‘n’ Students
Here, we are using the arrays in c programming. See the following program.
int main()
{
int sum = 0,i,n;
float avg; ;
int marks[100];
printf("Enter total number of students\n");
scanf("%d",&n);
printf ("Enter marks for %d students\n",n ) ;
for(i=0;i<n;i++)
{
scanf("%d",&marks[i]);
sum=sum+marks[i];
}
avg = sum/(float)n ;
printf("Average marks = %0.2f", avg);
return 0;
}
Output of the program is given below.
Enter total number of students
5
Enter marks for 5 students
45
67
43
78
55
Average marks = 57.60
C Program to Find Sum of Even and Odd Numbers Separately in an Array
Here, we are finding sum of even numbers and sum of odd numbers in an array separately. See the following program.
int main()
{
int sume = 0, sumo = 0, i,n;
int num[100];
printf("Enter the size of an array\n");
scanf("%d",&n);
printf ("Enter total %d elements\n",n ) ;
for(i=0;i<n;i++)
{
scanf("%d",&num[i]);
if((num[i]%2)==0) // checking whether num[i] is even or not.
{
sume=sume+num[i]; // sum of even numbers
}
else
{
sumo=sumo+num[i]; // sum of odd numbers
}
}
printf("Sum of Even Numbers = %d\n", sume);
printf("Sum of Odd Numbers = %d\n", sumo);
return 0;
}
See the following output.
Enter the size of an array
10
Enter total 10 elements
1 2 3 4 5 6 7 8 9 10
Sum of Even numbers = 30
Sum of Odd numbers = 25
How do you find the smallest number in an unsorted array?
In this program, we will read an array of size n. Then we will have to enter n elements and find the smallest element in that array.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[100],n;
int i,small;
printf("Enter size of an array\n");
scanf("%d",&n);
printf("Enter %d elements\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
small=a[0];
for(i=1;i<n;i++)
{
if(small>a[i])
{
small=a[i];
}
}
printf("Original Array Elements: ");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
printf("\nSmallest Element = %d",small);
return 0;
}
Output of this program.
Enter size of an array
5
Enter 5 elements
5 6 3 8 4
Original Array Elements: 5 6 3 8 4
Smallest Element = 3
How do you find the largest number in an unsorted array?
In this program, we will read an array of size n. Then we will have to enter n elements and find the largest element in that array.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[100],n;
int i,large;
printf("Enter size of an array\n");
scanf("%d",&n);
printf("Enter %d elements\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
large=a[0];
for(i=1;i<n;i++)
{
if(large<a[i])
{
large=a[i];
}
}
printf("Original Array Elements: ");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
printf("\nLargest Element = %d",large);
return 0;
}
Output of this program is given below.
Enter size of an array
5
Enter 5 elements
7 4 8 3 5
Original Array Elements: 7 4 8 3 5
Largest Element = 8
Factorial Program in C
In this program, we have find the factorial of a number using loop.
#include<stdio.h>
int main()
{
int fact=1,i,n;
printf("Enter any number\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact = fact * i;
}
printf("\nFactorial of %d is %d", n,fact);
return 0;
}
Output of this program is given below.
Enter any number
5
Factorial of 5 is 120
Factorial Program in C using Recursion
Recursion is a process where function calls itself. This program is giving the same output as the previous one but, here we have used the concept of recursion.
#include<stdio.h>
int fact(int);
int main()
{
int f, n;
printf("Enter any number\n");
scanf("%d",&n);
f = fact(n);
printf("\nFactorial of %d is %d", n,f);
return 0;
}
int fact(int num)
{
if(num==1)
{
return(1);
}
else
{
return(num*fact(num-1));
}
}
Output of this program remains the same as the previous one.
Swapping of two numbers in c using third variable
In this program, we are swapping two numbers using the temporary variable. See the following program.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a,b,temp=0;
printf("Enter any two numbers\n");
scanf("%d%d",&a,&b);
printf("\nBefore swapping, a=% and b=%d",a,b);
temp=a;
a=b;
b=temp;
printf("\nAfter swapping, a=% and b=%d",a,b);
return 0;
}
Output of this program.
Enter any two numbers
5
10
Before swapping, a=5 and b=10
After swapping, a=10 and b=5
Swapping of two numbers in c without using third variable
In this program, I have not used a third variable.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a,b;
printf("Enter any two numbers\n");
scanf("%d%d",&a,&b);
printf("\nBefore swapping, a=% and b=%d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("\nAfter swapping, a=% and b=%d",a,b);
return 0;
}
Output of this program will remain the same.
Swapping of two numbers in c using call by value
Here, we are also exchanging the value of two variables. Call by value means passing the values as argument to the function.
#include <stdio.h>
#include <stdlib.h>
int main( )
{
void swap(int, int);
int a = 5, b=10;
printf("Before Swapping :");
printf("a = %d b = %d ", a, b);
swap(a, b); //function call
printf("\nAfter Swapping :");
printf("a = %d b = %d ", a, b);
return 0;
}
void swap(int x, int y)
{
int temp=0;
temp = x;
x = y;
y = temp;
printf("\nx = %d y = %d ", x, y);
}
Output of this program is given below.
Before Swapping :a = 5 b = 10
x = 10 y = 5
After Swapping :a = 5 b = 10
Swapping of two numbers in c using call by reference
Here, we are passing the addresses of the arguments to the function. See the following program.
#include <stdio.h>
#include <stdlib.h>
int main( )
{
void swap(int*, int*);
int a = 5, b=10;
printf("Before Swapping :");
printf("a = %d b = %d ", a, b);
swap(&a, &b); //function call
printf("\nAfter Swapping :");
printf("a = %d b = %d ", a, b);
return 0;
}
void swap(int *x, int *y)
{
int temp=0;
temp = *x;
*x = *y;
*y = temp;
}
Here, we have used the concept of pointers in c programming. See the following output.
Before Swapping :a = 5 b = 10
After Swapping :a = 10 b = 5
C program for student details using array of structures
Here, we are reading student details and display the same information using array of structures.
#include <stdio.h>
#include <stdlib.h>
int main()
{
struct student
{
int rollno;
char name[50];
};
struct student s[3];
int i;
for(i=0;i<=2;i++)
{
printf("Enter roll number and name of a student%d",i+1);
scanf("%d%s",&s[i].rollno,&s[i].name);
}
for(i=0;i<=2;i++)
{
printf("\nStudent%d",i+1);
printf("\nRoll Number : %d",s[i].rollno);
printf("\nName : %s",s[i].name);
}
return 0;
}
Output of this program is given below.
Enter roll number and name of a student1
1
Vijay
Enter roll number and name of a student2
2
Vishal
Enter roll number and name of a student3
3
Ajay
Student1
Roll Number : 1
Name : Vijay
Student2
Roll Number : 2
Name : Vishal
Student3
Roll Number : 3
Name : Ajay
Addition of two matrix in c using 2d array
Here, I am using two-dimensional array for addition of two matrices. See the following program.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[3][3],b[3][3],c[3][3];
int i,j;
printf("Enter Values of Matrix A\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter Values of Matrix B\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("Matrix C is \n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",c[i][j]);
}
printf("\n");
}
return 0;
}
Output of this program is given as follows.
Enter Values of Matrix A
1 2 3 4 5 6 7 8 9
Enter Values of Matrix B
9 8 7 6 5 4 3 2 1
Matrix C is
10 10 10
10 10 10
10 10 10
Count number of characters in a file in c
This is the program of file handling in c programming. In this program, we are counting the total number of characters in a given file.
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp;
char ch;
int count=0;
fp=fopen("abc.txt","r");
if(fp==NULL)
{
printf("Error while opening");
return -1;
}
while((ch=fgetc(fp))!=EOF)
{
count++;
}
printf("Number of characters in a file are %d",count);
fclose(fp);
return 0;
}
Count number of lowercase letters in a file in c
This program is also same as the previous one. But, here we are counting only lowercase letters in a file.
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp;
char ch;
int count=0;
fp=fopen("abc.txt","r");
if(fp==NULL)
{
printf("Error while opening for reading");
return 0;
}
printf("Contents of the file are \n",count);
while((ch=fgetc(fp))!=EOF)
{
printf("%c",ch);
if(ch>=97)
{
if(ch<=122)
count++;
}
}
fclose(fp);
printf("\nTotal number of Lowercase letters are %d",count);
return 0;
}
Convert the contents of a file in uppercase letters.
In this program, we are reading a file and convert all the characters in a file in uppercase letters.
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
FILE *fp1,*fp2;
char ch;
fp1=fopen("abc.txt","r");
fp2=fopen("temp.txt","w");
if((fp1==NULL)||(fp2==NULL))
{
printf("ERROR");
return 0;
}
while((ch=fgetc(fp1))!=EOF)
{
if(islower(ch))
{
ch=ch-32;
}
fputc(ch,fp2);
}
fclose(fp1);
fclose(fp2);
remove("abc.txt");
rename("temp.txt","abc.txt");
return 0;
}
Reverse the contents of a file in c programming
In this program, we are going to print the contents of a file in a reverse order.
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp;
int ft,i=0;
fp=fopen("abc.txt","r");
if(fp==NULL)
{
printf("ERROR");
return 0;
}
fseek(fp,0,SEEK_END);
ft=ftell(fp);
while(i<ft)
{
i++;
fseek(fp,-i,SEEK_END);
printf("%c",fgetc(fp));
}
fclose(fp);
return 0;
}
Random number generator in c
#include <stdio.h>
#include <stdlib.h>
#include<time.h>
int main(void)
{
int min,max,i,num;
printf("Enter a given range: minimum value and maximum value\n");
scanf("%d%d",&min,&max);
printf("Enter total number to generate random numbers\n");
scanf("%d",&num);
srand(time(0));
for(i=1;i<=num;i++)
{
printf("%d ",(rand()%(max-min+1))+min);
}
return 0;
}
Output of this program is given below.
Enter a given range: minimum value and maximum value
1 20
Enter total number to generate random numbers
5
7 18 19 17 5
I hope you have liked and understood this basic 50 programs in c. There may be possibility that while writing this program, you may get some errors. All these programs are error free. But, still you find some errors, then please feel free to contact me. I will definitely help you.
Thank you.