C Programs
C Programs
C Programs
"
#include <stdio.h>
int main()
printf("Hello, World!");
return 0;
}
Output
Program to Print an Integer
#include <stdio.h>
int main()
int number;
scanf("%d", &number);
return 0;
Output
Program to Add Two Integers
#include <stdio.h>
int main()
// Displays sum
return 0;
}
Output
C program check if an integer is prime or not
#include <stdio.h>
int main()
int n, c;
printf("Enter a number\n");
scanf("%d", &n);
if (n == 2)
printf("Prime number.\n");
else
if (n % c == 0)
break;
if (c != n)
printf("Not prime.\n");
else
printf("Prime number.\n");
return 0;
}
Output
Program to Check Even or Odd
#include <stdio.h>
int main()
int number;
scanf("%d", &number);
if(number % 2 == 0)
else
return 0;
}
Output
Program to Check Odd or Even Using Conditional Operator
#include <stdio.h>
int main()
int number;
scanf("%d", &number);
return 0;
}
Output
Pattern program in C
#include <stdio.h>
int main()
int row, c, n, s;
scanf("%d", &n);
s = n;
printf(" ");
s--;
printf("*");
printf("\n");
return 0;
}
Output
C program to print following Pyramid:
**
***
****
*****
*/
#include<stdio.h>
#define MAX 5
int main()
int i,j;
for(j=0;j<=i;j++)
printf("*");
printf("\n");
return 0;
}
Output
C program to print following Pyramid:
*****
****
***
**
*/
#include<stdio.h>
#define MAX 5
int main()
int i,j;
for(j=0;j<=i;j++)
printf("*");
printf("\n");
return 0;
}
Output
C program to print following Pyramid:
123
12345
1234567
123456789
#include<stdio.h>
int main()
int i,j,k,l=1;
printf(" ");
printf("%d",k);
l = l+2;
printf("\n");
return 0;
}
Output
C program to print a string on the screen input by the user.
#include<stdio.h>
main()
return 0;
}
Output
C program generates numbers randomly using random function.
#include <stdio.h>
#include <stdlib.h>
int main()
int c, n;
n = rand()%100 + 1;
printf("%d\n", n);
return 0;
}
Output
A prime number is a natural number that has only one and itself as factors.
#include <stdio.h>
main() {
int n, i, c = 0;
scanf("%d", &n);
if (n % i == 0) {
c++;
if (c == 2) {
else {
return 0;
}
Output
C Program Calculate Factorial of a Number using Recursion
#include<stdio.h>
int fact(int);
void main()
{
int n,t;
clrscr();
printf("Enter any Number : ");
scanf("%d",& n);
t=fact(n);
printf("Factorial of %d is : %d",n,t);
getch();
}
int fact(int a)
{
int p;
if(a==1)
return(1);
else
{
p=a*fact(a-1);
return(p);
}
}
Output
C Program to find the greatest number of three numbers.
#include <stdio.h>
int main()
int a, b, c;
else {
return 0;
}
Output
program to find maximum or largest element present in an array.
#include <stdio.h>
int main()
scanf("%d", &size);
scanf("%d", &array[c]);
maximum = array[0];
maximum = array[c];
location = c+1;
printf("Maximum element is present at location %d and it's value is %d.\n", location, maximum);
return 0;
}
Output
C program to copy a file.
#include<stdio.h>
#include<stdlib.h>
int main()
{
char ch, source_file[25], target_file[25];
FILE *source, *target;
printf("Enter name of file to copy\n");
gets(source_file);
source = fopen(source_file, "r");
if( source == NULL )
{
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
}
printf("Enter name of target file\n");
gets(target_file);
target = fopen(target_file, "w");
if( target == NULL )
{
fclose(source);
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
}
while( ( ch = fgetc(source) ) != EOF )
fputc(ch, target);
printf("File copied successfully.\n");
fclose(source);
fclose(target);
return 0;
}
Output
C program to Fibonacci Series.
#include<stdio.h>
int main()
int i, fib[25];
fib[0] = 0;
fib[1] = 1;
//i'th element of series is equal to the sum of i-1'th element and i-2'th element.
return 0;
}
Output
Write a C program to check whether the given number is even or odd.
#include <stdio.h>
int main()
int a;
printf("Enter a: ");
scanf("%d", &a);
/* logic */ if (a % 2 == 0) {
else {
return 0;
}
Output
C program for matrix addition:
#include <stdio.h>
int main()
scanf("%d", &first[c][d]);
scanf("%d", &second[c][d]);
printf("%d\t", sum[c][d]);
printf("\n");
return 0;
}
Output
C program to Matrix multiplication
#include <stdio.h>
int main()
int m, n, p, q, c, d, k, sum = 0;
scanf("%d", &first[c][d]);
if (n != p)
else
scanf("%d", &second[c][d]);
multiply[c][d] = sum;
sum = 0;
printf("%d\t", multiply[c][d]);
printf("\n");
return 0;
}
Output
Write a program which prints number of characters, spaces, tabs and lines in a text file.
(count.txt)
#include <stdio.h>
int main()
char in_name[80];
FILE *in_file;
scanf("%s", in_name);
if (in_file == NULL)
else
character++;
space++;
if (ch == '\n')
line++;
if (ch == '\t')
tab++;
}
fclose(in_file);
return 0;
}
Output