100 C Programs
100 C Programs
100 C Programs
#include<stdio.h>
int main()
{
int n;
int i;
int m=0;
int flag=0;
m = n/2;
for(i = 2; i <= m;i++)
{
if(n%i == 0)
{
printf("Number is NOT prime\n");
return 0;
}
}
if(flag==0)
printf("Number is prime\n");
return 0;
}
3. Check if number is palindrome or not
#include<stdio.h>
int main()
{
int n;
int r;
int sum=0;
int temp;
temp = n;
while(n>0)
{
r = n%10;
sum = (sum*10)+r;
n = n/10;
}
if(temp == sum)
printf("Number is palindrome \n");
else
printf("Number is NOT palindrome\n");
return 0;
}
4. Find factorial of the number
#include<stdio.h>
int main()
{
int i;
int fact=1;
int number;
printf("Enter a number: \n");
scanf("%d",&number);
for(i = 1; i <= number; i++)
{
fact=fact*i;
}
printf("Factorial of %d is: %d\n",number,fact);
return 0;
}
5. Check if a number is Armstrong number
#include<stdio.h>
int main()
{
int n;
int r;
int sum=0;
int temp;
printf("Enter a number \n");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp == sum)
printf("Number is a armstrong number\n");
else
printf("Number is NOT a armstrong number\n");
return 0;
}
6. Program to find sum of digits
#include<stdio.h>
int main()
{
int n;
int sum=0;
int m;
printf("Enter number:\n");
scanf("%d",&n);
while(n>0)
{
m = n%10;
sum = sum+m;
n = n/10;
}
printf("Sum is = %d\n",sum);
return 0;
}
7. Reverse a number
#include<stdio.h>
int main()
{
int n;
int reverse = 0;
int rem;
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a[10];
int n;
int i;
printf("Enter decimal number \n");
scanf("%d",&n);
for(i=0;n>0;i++)
{
a[i]=n%2;
n=n/2;
}
printf("\nBinary number is ");
for(i=i-1;i>=0;i--)
{
printf("%d",a[i]);
}
return 0;
}
12. Check if number is positive or negative
#include <stdio.h>
void main()
{
int num;
if((num1>num2)&&(num1>num3))
printf("\n Number1 is greatest\n");
else if((num2>num3)&&(num2>num1))
printf("\n Number2 is greatest\n");
else
printf("\n Number3 is greatest\n");
return 0;
}
14. Program to get ascii value
#include <stdio.h>
int main()
{
char ch;
printf("Enter character:\n");
scanf("%c", &ch);
printf("ASCII value of character %c is: %d\n", ch, ch);
return 0;
}
15. Program to demonstrate sizeof function
#include<stdio.h>
int main()
{
printf("Size of char: %ld byte\n",sizeof(char));
printf("Size of int: %ld bytes\n",sizeof(int));
printf("Size of float: %ld bytes\n",sizeof(float));
printf("Size of double: %ld bytes\n", sizeof(double));
return 0;
}
16. Program to check for leap year
#include <stdio.h>
int main()
{
int y;
if(y % 4 == 0)
{
if( y % 100 == 0)
{
if ( y % 400 == 0)
printf("%d is a Leap Year\n", y);
else
printf("%d is not a Leap Year\n", y);
}
else
printf("%d is a Leap Year\n", y );
}
else
printf("%d is not a Leap Year\n", y);
return 0;
}
17. Program to convert upper case to lower case
#include<stdio.h>
#include<string.h>
int main()
{
char str[25];
int i;
printf("Enter the string: \n");
scanf("%s",str);
for(i=0;i<=strlen(str);i++)
{
if(str[i]>=65&&str[i]<=90)
str[i]=str[i]+32;
}
printf("\nLower Case String is: %s\n",str);
return 0;
}
18. Program to convert lower case to upper case
#include<stdio.h>
#include<string.h>
int main()
{
char str[25];
int i;
for(i=0;i<=strlen(str);i++)
{
if(str[i]>=97&&str[i]<=122)
str[i]=str[i]-32;
}
printf("\nUpper Case String is: %s\n",str);
return 0;
}
19. Program to get string length
#include <stdio.h>
int main()
{
char str[100],i;
printf("Enter a string: \n");
scanf("%s",str);
for(i=0; str[i]!='\0'; ++i);
printf("\nLength of input string: %d",i);
return 0;
}
20. Program to concatenate 2 strings
#include <stdio.h>
int main()
{
char str1[50], str2[50], i, j;
printf("\nEnter first string: ");
scanf("%s",str1);
printf("\nEnter second string: ");
scanf("%s",str2);
return 0;
}
21. Program to get largest element in an array
#include <stdio.h>
int largest_element(int arr[], int num)
{
int i, max_element;
max_element = arr[0];
for (i = 1; i < num; i++)
if (arr[i] > max_element)
max_element = arr[i];
return max_element;
}
int main()
{
int arr[] = {1, 24, 15, 20, 8, -11, 30};
int n = sizeof(arr)/sizeof(arr[0]);
printf("Largest element of array is %d", largest_element(arr, n));
return 0;
}
22. Program to get size of the array
#include <stdio.h>
int main()
{
double arr[] = {1, 2, 3, 4, 5, 6};
int n;
n = sizeof(arr) / sizeof(arr[0]);
printf("Size of the array is: %d\n", n);
return 0;
}
23. Program to get GCD
#include <stdio.h>
int main()
{
int n1, n2, i, gcd;
printf("Enter two integers: \n");
scanf("%d %d", &n1, &n2);
for(i=1; i <= n1 && i <= n2; ++i)
{
if(n1%i==0 && n2%i==0)
gcd = i;
}
printf("G.C.D of %d and %d is %d\n", n1, n2, gcd);
return 0;
}
24. Program to get LCM
#include <stdio.h>
int main()
{
int n1, n2, minMultiple;
printf("Enter two positive integers: \n");
scanf("%d %d", &n1, &n2);
#include<stdio.h>
#include<string.h>
int main()
{
char name[30] = "Hello";
#include<stdio.h>
int main()
int num,i,fact,r,sum=0,temp;
if(sum==temp)
#include<stdio.h>
int main()
{
int n;
printf("Enter a number");
scanf("%d",&n);
printf("\nSquare of the number %d is %d",n,n*n);
printf("\nCube of the number %d is %d",n,n*n*n);
return 0;
}
32. Program to generate random number
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, n;
printf(“%d\n”, n);
return 0;
}
33. Program to check if strings are anagram
#include <stdio.h>
#include <string.h>
#include <stdio.h>
int main()
{
char s1[] = "prodevelopertutorial";
char s2[8];
int length = 0;
while(s1[length] != '\0')
{
s2[length] = s1[length];
length++;
}
s2[length] = '\0';
return 0;
}
35. Program to toggle case
#include <stdio.h>
#define MAX_SIZE 100
while(str[i] != '\0')
{
if(str[i]>='a' && str[i]<='z')
{
str[i] = str[i] - 32;
}
else if(str[i]>='A' && str[i]<='Z')
{
str[i] = str[i] + 32;
}
i++;
}
}
int main()
{
char str[MAX_SIZE];
return 0;
}
36. Program to get number of vowels and consonant
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100 // Maximum string size
int main()
{
char str[MAX_SIZE];
int i, len, vowel, consonant;
vowel = 0;
consonant = 0;
len = strlen(str);
return 0;
}
37. Program to get nth bit of a number
#include <stdio.h>
int main()
{
int num, n, bitStatus;
#include <stdio.h>
int main()
{
int num, n, newNum;
return 0;
}
39. Program to toggle bits of a number
#include <stdio.h>
int main()
{
int num, n, newNum;
return 0;
}
40. Program to get even or odd
#include <stdio.h>
int main()
{
int num;
printf("Enter any number: ");
scanf("%d", &num);
if(num & 1)
{
printf("%d is odd.", num);
}
else
{
printf("%d is even.", num);
}
return 0;
}
41. Program for sigalarm
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
t0 = time (NULL);
printf ("%s", asctime(localtime(&t0)));
return 0;
}
43. Program for fork
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>
int main()
{
pid_t cid;
int a = 0;
if ( (cid=fork())>0 ) {
a++;
printf("Parent : a=%d\n",a);
printf("Parent : My Child PID = %d\n",cid);
printf("Parent : My PID = %d\n",getpid());
wait(NULL);
printf("Parent : child done\n");
puts("Parent : DONE");
}else{
printf("Child : a=%d\n",a);
printf("Child : My PID = %d\n",getpid());
printf("Child : My Parent PID = %d\n",getppid());
sleep(1);
}
return 0;
}
44. Process to get parent id
#include <stdio.h>
#include <unistd.h>
int main()
{
pid_t pid = getpid();
printf("My PID = %d\n",pid);
printf("My Parent PID = %d\n",getppid());
return 0;
}
45. Program to demonstrate pipe
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main(void)
{
pid_t childpid;
int fd[2];
char buf[10] = "Bye";
childpid=fork();
if (childpid > 0){
//parent
write(fd[1],"Hello",sizeof("Hello"));
}else if (childpid == 0){
//child
read(fd[0],buf,sizeof(buf));
}
printf("CHILD PID = %d , buf = %s \n", childpid, buf);
return 0;
}
46. Program for sigaction
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
t0 = time(NULL);
t0 = time (NULL);
printf("%lu sec since epoch\n", t0);
printf("%s\n", ctime(&t0));
return 0;
}
50. Program for atoi
#include <stdio.h>
#include <stdlib.h>
int main()
{
char s[12] = "";
int i = 0;
scanf("%s",s);
i = atoi(s);
printf("%s = %i \n",s,i);
return 0;
}
51. Program for fgets
#include <stdio.h>
int main()
{
char a[8];
fgets(a,sizeof(a),stdin);
if( !a[sizeof(a)-1] ){
printf("Greater than %lu. Thus, null terminated\n",sizeof(a));
}
puts(a);
return 0;
}
52. Program for file write
#include <stdio.h>
int main()
{
char* filename = "myfile.txt";
FILE* fp = fopen(filename, "w");
char c = 'A';
if(fp){
while( c<='Z' ){
fputc(c,fp);
c++;
}
fputc('\n',fp);
}else{ fclose(fp);
#include <stdio.h>
int main()
{
char* filename = "myFile.txt";
FILE* fp = fopen(filename, "r");
char c = 0;
if(fp){
while( (c=fgetc(fp)) != EOF ){
printf("%c", c);
}
}else{ fclose(fp);
return 0;
}
54. Program for fscanf
#include <stdio.h>
int main()
{
char* filename = "numbers.txt";
FILE* fp = fopen(filename, "r");
int a = 0;
if(fp){
while( fscanf(fp,"%d",&a) != EOF ){
printf("%d\n", a);
}
}else{ fclose(fp);
return 0;
}
55. Program for fseek
#include <stdio.h>
int main()
{
char* filename = "myFile.txt";
FILE* fp = fopen(filename, "r");
char c = 0;
if(fp){
fseek(fp,5,SEEK_SET); while(
(c=fgetc(fp)) != EOF ){
printf("%c", c);
}
}else{ fclose(fp);
return 0;
}
56. Program for break
#include <stdio.h>
int main()
{
char a = 'A';
while(1){
printf("(Y)es, (N)o or (Q)uit\n");
printf("Enter choice: ");
scanf("%c",&a);
if(a=='q' || a=='Q'){
break;
}
}
return 0;
}
57. Program to demonstrate continue
#include <stdio.h>
int main()
{
int i = 0;
for(;i<100;i++){
if(i%2){
continue;
}
printf("%d*%d=%d\n",i,i,i*i);
}
return 0;
}
58. Program to show do_while
#include <stdio.h>
int main()
{
char a = 'A';
do{
printf("(Y)es, (N)o or (Q)uit\n");
printf("Enter choice: ");
scanf("%c",&a);
}while(a!='q' && a!='Q');
return 0;
}
59. Program for for loop
#include <stdio.h>
#include <math.h>
int main()
{
int a = 'A';
int b = 'a';
for(a='A',b='a'; a<='Z' && b<='z'; a++,b++ ){
printf("%c-%c\n",a,b);
}
return 0;
}
60. Program for pass by value and pass byreference
#include <stdio.h>
void inc1(int byval)
{
byval++;
}
void inc2(int *byref)
{
(*byref)++;
}
int main()
{
int a = 0;
int b = 0;
inc1(a);
inc2(&b);
printf("a= %d\n",a);
printf("b= %d\n",b);
return 0;
}
61. Program to show pointers in C
#include<stdio.h>
int main(){
float celsius, fahrenheit;
return 0;
}
62. Program to print heart pattern
#include<stdio.h>
int main(){
int i,j;
//loop for the upper part of the heart
for(i=0;i<3;i++){
for(j=i;j<2;j++){
printf(" ");
}
for(j=(i*2)+5;j>0;j--){
printf("*");
}
for(j=5-(i*2);j>0;j--){
printf(" ");
}
for(j=(i*2)+5;j>0;j--){
printf("*");
}
printf("\n");
}
//loop for the lower part of the heart
for(i=0;i<10;i++){
for(j=0;j<i;j++){
printf(" ");
}
for(j=19-(2*i);j>0;j--){
printf("*");
}
printf("\n");
}
return 0;
}
63: Program for pyramid pattern
#include <stdio.h>
int main()
{
int i, space, rows, k=0, count = 0, count1 = 0;
printf("\n");
}
return 0;
}
64: Program for Celsius to Fahrenheit
#include<stdio.h>
int main(){
float celsius, fahrenheit;
return 0;
}
65. Program to calculate equilateral triangle
#include<stdio.h>
#include<math.h>
int main(){
int side;
float area;
printf("Side: ");
scanf("%d", &side);
printf("\n");
return 0;
}
66. Program to find area of dead body
#include<stdio.h>
int main(){
float length, breadth, area;
#include<stdio.h>
int main(){
int base, height, area;
printf("Base: ");
scanf("%d", &base);
printf("Height: ");
scanf("%d", &height);
printf("\n");
return 0;
}
68. Program to get day-month-year
#include<stdio.h>
int main(){
int year, month, week, days;
return 0;
}
69 . Program to get diameter and circumference from radius
#include<stdio.h>
int main(){
float radius, diameter, circumference, area, pi;
pi = 3.1415926535;
printf("Enter Radius: ");
scanf("%f", &radius);
diameter = 2 * radius;
circumference = pi * diameter;
area = pi * (radius * radius);
#include<stdio.h>
int main(){
float celsius, fahrenheit;
return 0;
}
71. Program to check if file exist
#include <stdio.h>
int main()
{
FILE *file = fopen("Test.txt", "r");
if(file == NULL)
{
printf("ERROR : File Does Not Exist\n");
return -1;
}
else
{
printf("File Exists\n");
}
return 0;
}
72: Get file size
#include <stdio.h>
int main(int argc, char *argv[])
{
if(argc != 2)
{
printf("usage %s file.txt\n",argv[0]);
return -1;
}
FILE * file = fopen(argv[1], "r");
if(file == NULL)
{
printf("[ERROR] Unable to open %s\n",argv[1]);
return -1;
}
fseek(file, 0, SEEK_END);
printf("%s is %d bytes long\n",argv[1],ftell(file));
return 0;
}
73. Program for dynamic memory allocation
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char name[100];
char *description;
if (description == NULL) {
fprintf(stderr, "Error - unable to allocate required memory");
} else {
strcpy(description, "Imam Sutono is student at Binus University");
}
return 0;
}
74. Program for resizing and releasing of memory
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char name[100];
char *description;
if (description == NULL) {
fprintf(stderr, "Error - unable to allocate required memory");
} else {
strcpy(description, "He is student at Binus University");
}
#include <stdio.h>
// function declaration
void func(void);
static int count = 5; // global variable
int main()
{
while(count--)
{
func();
}
return 0;
}
// function definition
void func(void)
{
static int i = 5; // local static variable
i++;
#include <stdio.h>
int main() {
int sum = 17, count = 5;
double mean;
mean = (double) sum / count; // convert data type of sum (int) to double
printf("Value of sum : %f\n", mean);
return 0;
}
77. Program for integer promotion
#include <stdio.h>
int main() {
int i = 17;
char c = 'c'; // ASCII value is 99
int sum;
return 0;
}
78. Program for typedef
#include <stdio.h>
#include <string.h>
int main() {
Book book;
return 0;
}
79. Program for bitwise left shift operator
#include <stdio.h>
int main()
{
int a=7, b=2,c;
c = a<<b;
printf("Value of c = %d\n",c);
return 0;
}
80. Program for float
#include <stdio.h>
int main(void)
{
const double RENT = 3852.99; // const-style constant
printf("*%f*\n", RENT);
printf("*%e*\n", RENT);
printf("*%4.2f*\n", RENT);
printf("*%3.1f*\n", RENT);
printf("*%10.3f*\n", RENT);
printf("*%10.3E*\n", RENT);
printf("*%+4.2f*\n", RENT);
printf("*%010.2f*\n", RENT);
return 0;
}
81. Program for bitwise complement
#include <stdio.h>
int main()
{
int a=14, b;
b = ~a;
printf("Value of c = %d\n",b);
return 0;
}
82. Program for bitwise and
#include <stdio.h>
int main()
{
int a=14, b= 7, c;
c =a&b;
printf("Value of c = %d\n",c);
return 0;
}
83. Program for getting remainder
#include<stdio.h>
int main ()
{
int t, A, B;
int rem = 0;
scanf ("%d", &t);
while (t--)
{
scanf ("%d%d",&A,&B);
rem = A % B;
printf("%d\n", rem);
}
return 0;
}
84. Program for ternary operator
#include<stdio.h>
int main()
{
float a,b,c,large;
printf("Enter any 3 numbers\n");
scanf("%f%f%f",&a,&b,&c);
large = a>b? (a>c?a:c): (b>c?b:c);
printf("The larger no is :%f\n", large);
return 0;
}
85. Program for structure in c
#include <stdio.h>
/*structure declaration*/
struct employee{
char name[30];
int empId;
float salary;
};
int main()
{
/*declare and initialization of structure variable*/
struct employee emp={"Mike",1120,76909.00f};
#include <stdio.h>
struct student{
char name[30];
int rollNo;
struct dateOfBirth{
int dd;
int mm;
int yy;
}DOB; /*created structure varoable DOB*/
};
int main()
{
struct student std;
return 0;
}
87. Program for structure pointer
#include <stdio.h>
struct item
{
char itemName[30];
int qty;
float price;
float amount;
};
int main()
{
struct item itm; /*declare variable of structure item*/
struct item *pItem; /*declare pointer of structure item*/
return 0;
}
88. Program for passing structure to function
#include<stdio.h>
struct address
{
char city[20];
int pin;
char phone[14];
};
struct employee
{
char name[20];
struct address add;
};
void display(struct employee);
void main ()
{
struct employee emp;
printf("Enter employee information?\n");
scanf("%s %s %d %s",emp.name,emp.add.city, &emp.add.pin, emp.add.phone);
display(emp);
}
void display(struct employee emp)
{
printf("Printing the details....\n");
printf("%s %s %d %s",emp.name,emp.add.city,emp.add.pin,emp.add.phone);
}
89. Program for union
#include <stdio.h>
union test1
{
int x;
int y;
} Test1;
union test2
{
int x;
char y;
} Test2;
union test3
{
int arr[10];
char y;
} Test3;
int main()
{
printf("sizeof(test1) = %lu, sizeof(test2) = %lu, "
"sizeof(test3) = %lu",
sizeof(Test1),
sizeof(Test2), sizeof(Test3));
return 0;
}
90. Program for union pointer
#include <stdio.h>
union test
{
int x;
char y;
};
int main()
{
union test p1;
p1.x = 65;
// p2 is a pointer to union p1
union test* p2 = &p1;
#include <stdio.h>
#include <string.h>
int main( ) {
printf( "Memory size occupied by status1 : %d\n", sizeof(status1));
printf( "Memory size occupied by status2 : %d\n", sizeof(status2));
return 0;
}