C Programs

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 45

Program to Display "Hello, World!

"

#include <stdio.h>

int main()

// printf() displays the string inside quotation

printf("Hello, World!");

return 0;

}
Output
Program to Print an Integer

#include <stdio.h>

int main()

int number;

// printf() dislpays the formatted output

printf("Enter an integer: ");

// scanf() reads the formatted input and stores them

scanf("%d", &number);

// printf() displays the formatted output

printf("You entered: %d", number);

return 0;

Output
Program to Add Two Integers

#include <stdio.h>

int main()

int first Number, second Number, sum Of Two Numbers;

printf("Enter two integers: ");

// Two integers entered by user is stored using scanf() function

scanf("%d %d", &first Number, &second Number);

// sum of two numbers in stored in variable sum Of Two Numbers

Sum Of Two Numbers = first Number + second Number;

// Displays sum

printf("%d + %d = %d", first Number, second Number, sum Of Two Numbers);

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

for (c = 2; c <= n - 1; c++)

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;

printf("Enter an integer: ");

scanf("%d", &number);

// True if te number is perfectly divisible by 2

if(number % 2 == 0)

printf("%d is even.", number);

else

printf("%d is odd.", number);

return 0;

}
Output
Program to Check Odd or Even Using Conditional Operator

#include <stdio.h>

int main()

int number;

printf("Enter an integer: ");

scanf("%d", &number);

(number % 2 == 0) ? printf("%d is even.", number) : printf("%d is odd.", number);

return 0;

}
Output
Pattern program in C

#include <stdio.h>

int main()

int row, c, n, s;

printf("Enter the number of rows in pyramid of stars you wish to see\n");

scanf("%d", &n);

s = n;

for (row = 1; row <= n; row++) // Loop to print rows

for (c = 1; c < s; c++) // Loop to print spaces in a row

printf(" ");

s--;

for (c = 1; c <= 2*row - 1; c++) // Loop to print stars in a row

printf("*");

printf("\n");

return 0;

}
Output
C program to print following Pyramid:

**

***

****

*****

*/

#include<stdio.h>

#define MAX 5

int main()

int i,j;

for(i=0; i< MAX; i++)

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(i=MAX; i>=0; i--)

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;

for(i=1; i<=5; i++)

for(j=4; j>=i; j--)

printf(" ");

for(k=1; k<=l; k++)

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()

/* variable definition and initialization */ char string Array[100];

Print f("Please write something: \n");

scanf("%s", string Array);

print f("You enter the string %s\n", string Array);

return 0;

}
Output
C program generates numbers randomly using random function.

#include <stdio.h>

#include <stdlib.h>

int main()

int c, n;

printf("Ten random numbers in [1,100]\n");

for (c = 1; c <= 10; c++)

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;

printf("Enter any number n:");

scanf("%d", &n);

/*logic*/ for (i = 1; i <= n; i++) {

if (n % i == 0) {

c++;

if (c == 2) {

printf("n is a Prime number");

else {

printf("n is not a Prime number");

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;

printf("Enter a,b,c: ");

scanf("%d %d %d", &a, &b, &c);

if (a > b && a > c) {

printf("a is Greater than b and c");

else if (b > a && b > c) {

printf("b is Greater than a and c");

else if (c > a && c > b) {

printf("c is Greater than a and b");

else {

printf("all are equal or any two values are equal");

return 0;

}
Output
program to find maximum or largest element present in an array.

#include <stdio.h>

int main()

int array[100], maximum, size, c, location = 1;

printf("Enter the number of elements in array\n");

scanf("%d", &size);

printf("Enter %d integers\n", size);

for (c = 0; c < size; c++)

scanf("%d", &array[c]);

maximum = array[0];

for (c = 1; c < size; c++)

if (array[c] > maximum)

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()

//array fib stores numbers of fibonacci series

int i, fib[25];

//initialized first element to 0

fib[0] = 0;

//initialized second element to 1

fib[1] = 1;

//loop to generate ten elements

for (i = 2; i < 10; i++) {

//i'th element of series is equal to the sum of i-1'th element and i-2'th element.

fib[i] = fib[i - 1] + fib[i - 2];

printf("The fibonacci series is as follows ");

//print all numbers in the series

for (i = 0; i < 10; i++) {

printf("%d ", fib[i]);

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) {

printf("The given number is EVEN");

else {

printf("The given number is ODD");

return 0;

}
Output
C program for matrix addition:

#include <stdio.h>

int main()

int m, n, c, d, first[10][10], second[10][10], sum[10][10];

printf("Enter the number of rows and columns of matrix\n");

scanf("%d%d", &m, &n);

printf("Enter the elements of first matrix\n");

for (c = 0; c < m; c++)

for (d = 0; d < n; d++)

scanf("%d", &first[c][d]);

printf("Enter the elements of second matrix\n");

for (c = 0; c < m; c++)

for (d = 0 ; d < n; d++)

scanf("%d", &second[c][d]);

printf("Sum of entered matrices:-\n");

for (c = 0; c < m; c++) {

for (d = 0 ; d < n; d++) {

sum[c][d] = first[c][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;

int first[10][10], second[10][10], multiply[10][10];

printf("Enter number of rows and columns of first matrix\n");

scanf("%d%d", &m, &n);

printf("Enter elements of first matrix\n");

for (c = 0; c < m; c++)

for (d = 0; d < n; d++)

scanf("%d", &first[c][d]);

printf("Enter number of rows and columns of second matrix\n");

scanf("%d%d", &p, &q);

if (n != p)

printf("The matrices can't be multiplied with each other.\n");

else

printf("Enter elements of second matrix\n");

for (c = 0; c < p; c++)

for (d = 0; d < q; d++)

scanf("%d", &second[c][d]);

for (c = 0; c < m; c++) {

for (d = 0; d < q; d++) {

for (k = 0; k < p; k++) {


sum = sum + first[c][k]*second[k][d];

multiply[c][d] = sum;

sum = 0;

printf("Product of the matrices:\n");

for (c = 0; c < m; c++) {

for (d = 0; d < q; d++)

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;

int ch, character = 0, line = 0, space = 0, tab = 0;

printf("Enter file name:\n");

scanf("%s", in_name);

in_file = fopen(in_name, "r");

if (in_file == NULL)

printf("Can't open %s for reading.\n", in_name);

else

while ((ch = fgetc(in_file)) != EOF)

character++;

if (ch == ' ')

space++;

if (ch == '\n')

line++;

if (ch == '\t')

tab++;
}

fclose(in_file);

printf("\nNumber of characters = %d", character);

printf("\nNumber of spaces = %d", space);

printf("\nNumber of tabs = %d", tab);

printf("\nNumber of lines = %d", line);

return 0;

}
Output

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy