Академический Документы
Профессиональный Документы
Культура Документы
int main()
{
printf("Hello world\n");
return 0;
}
Purpose of Hello world program may be to say hello to people or the users of your software or
application.
Output of program:
#include <stdio.h>
int main()
{
char string[] = "Hello World";
printf("%s\n", string);
return 0;
}
Don't worry if you didn't understand above code as you may not be familiar with arrays yet.
Using loop we can print "Hello World" a desired number of time or indefinitely.
#include <stdio.h>
#define TRUE 1
By Tancho Divine
int main()
{
while (TRUE)
{
printf("Hello World\n");
}
return 0;
}
While loop will execute forever until it is terminated, to terminate press (Ctrl + C) in windows
operating system.
C programming code
#include <stdio.h>
int main()
{
int a;
printf("Enter an integer\n");
scanf("%d", &a);
return 0;
}
Output of program:
By Tancho Divine
In c language we have data type for different types of data, for integer data it is int, for character
date char, for floating point data it's float and so on.
C programming code
#include<stdio.h>
int main()
{
int a, b, c;
c = a + b;
return 0;
}
Output of program:
main()
{
int a = 1, b = 2;
return 0;
}
main()
{
int a, b, c;
char ch;
while(1)
{
printf("Enter values of a and b\n");
scanf("%d%d",&a,&b);
c = a + b;
if ( ch == 'y' || ch == 'Y' )
continue;
else
break;
}
return 0;
}
main()
{
long first, second, sum;
By Tancho Divine
scanf("%ld%ld", &first, &second);
printf("%ld\n", sum);
return 0;
}
result = a + b;
return result;
}
We have used long data type as it can handle large numbers, if you want to add still larger
numbers which doesn't fit in long range then use array, string or other data structure.
main()
{
int n;
printf("Enter an integer\n");
scanf("%d",&n);
if ( n%2 == 0 )
printf("Even\n");
else
printf("Odd\n");
return 0;
}
By Tancho Divine
We can use bitwise AND (&) operator to check odd or even, as an example consider binary of 7
(0111) when we perform 7 & 1 the result will be one and you may observe that the least
significant bit of every odd number is 1, so ( odd_number & 1 ) will be one always and also (
even_number & 1 ) is zero.
main()
{
int n;
printf("Enter an integer\n");
scanf("%d",&n);
if ( n & 1 == 1 )
printf("Odd\n");
else
printf("Even\n");
return 0;
}
main()
{
int n;
printf("Input an integer\n");
scanf("%d",&n);
return 0;
}
int main()
{
int first, second, add, subtract, multiply;
float divide;
printf("Sum = %d\n",add);
printf("Difference = %d\n",subtract);
printf("Multiplication = %d\n",multiply);
printf("Division = %.2f\n",divide);
return 0;
}
Output of program:
In c language when we divide two integers we get integer result for example 5/2 evaluates to 2.
As a general rule integer/integer = integer and float/integer = float or integer/float = float. So we
convert denominator to float in our program, you may also write float in numerator. This explicit
conversion is known as typecasting.
C programming code
#include <stdio.h>
int main()
{
char ch;
printf("Enter a character\n");
scanf("%c", &ch);
return 0;
}
Output of program:
int main()
{
char ch;
printf("Input a character\n");
scanf("%c", &ch);
switch(ch)
{
case 'a':
case 'A':
case 'e':
By Tancho Divine
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
printf("%c is a vowel.\n", ch);
break;
default:
printf("%c is not a vowel.\n", ch);
}
return 0;
}
return 0;
}
This function can also be used to check if a character is a consonant or not, if it's not a vowel
then it will be a consonant, but make sure that the character is an alphabet not a special character.
Factorial program in c
Factorial program in c: c code to find and print factorial of a number, three methods are given,
first one uses for loop, second uses a function to find factorial and third using recursion. Factorial
is represented using '!', so five factorial will be written as (5!), n factorial as (n!). Also
n! = n*(n-1)*(n-2)*(n-3)...3.2.1 and zero factorial is defined as one i.e. 0! = 1.
#include <stdio.h>
int main()
{
int c, n, fact = 1;
return 0;
}
Output of code:
long factorial(int);
int main()
{
int number;
long fact = 1;
return 0;
}
long factorial(int n)
{
int c;
long result = 1;
return result;
}
long factorial(int);
int main()
{
int n;
long f;
if (n < 0)
printf("Negative integers are not allowed.\n");
else
{
f = factorial(n);
printf("%d! = %ld\n", n, f);
}
return 0;
}
long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
Recursion is a technique in which a function calls itself, for example in above code factorial
function is calling itself. To solve a problem using recursion you must first express its solution in
recursive form.
C programming code
#include <stdio.h>
int main() {
int a, b, x, y, t, gcd, lcm;
while (b != 0) {
t = b;
b = a % b;
a = t;
}
gcd = a;
lcm = (x*y)/gcd;
return 0;
}
Output of program:
int main() {
long x, y, hcf, lcm;
return 0;
}
int main() {
long x, y, hcf, lcm;
return 0;
}
while (y != 0) {
if (x > y) {
x = x - y;
}
else {
y = y - x;
}
}
return x;
}
int main()
{
int n, c, k;
if (k & 1)
printf("1");
else
printf("0");
}
printf("\n");
return 0;
}
Output of program:
int main()
{
int x, y, temp;
temp = x;
x = y;
y = temp;
return 0;
}
Output of program:
#include <stdio.h>
int main()
{
int a, b;
a = a + b;
b = a - b;
By Tancho Divine
a = a - b;
To understand above logic simply choose a as 7 and b as 9 and then do what is written in
program. You can choose any other combination of numbers as well. Sometimes it's a good way
to understand a program.
int main()
{
int x, y, *a, *b, temp;
a = &x;
b = &y;
temp = *b;
*b = *a;
*a = temp;
return 0;
}
C programming code
#include <stdio.h>
By Tancho Divine
int main()
{
int n, reverse = 0;
while (n != 0)
{
reverse = reverse * 10;
reverse = reverse + n%10;
n = n/10;
}
return 0;
}
Output of program:
Palindrome Numbers
Palindrome number in c: A palindrome number is a number such that if we reverse it, it will not
change. For example some palindrome numbers examples are 121, 212, 12321, -454. To check
whether a number is palindrome or not first we reverse it and then compare the number obtained
with the original, if both are same then number is palindrome otherwise not. C program for
palindrome number is given below.
temp = n;
while( temp != 0 )
{
reverse = reverse * 10;
reverse = reverse + temp%10;
temp = temp/10;
}
if ( n == reverse )
printf("%d is a palindrome number.\n", n);
else
printf("%d is not a palindrome number.\n", n);
return 0;
}
Output of program:
*
***
*****
*******
*********
By Tancho Divine
We have shown five rows above, in the program you will be asked to enter the numbers of rows
you want to print in the pyramid of stars.
C programming code
#include <stdio.h>
int main()
{
int row, c, n, temp;
printf("Enter the number of rows in pyramid of stars you wish to see ");
scanf("%d",&n);
temp = n;
temp--;
printf("\n");
}
return 0;
}
Output of program:
#include <stdio.h>
int main()
{
int n, c, k;
printf("\n");
}
return 0;
}
Using these examples you are in a better position to create your desired pattern for yourself.
Creating a pattern involves how to use nested loops properly, some pattern may involve
alphabets or other special characters. Key aspect is knowing how the characters in pattern
changes.
*
***
*****
***
*
C programming code
#include <stdio.h>
int main()
{
int n, c, k, space = 1;
space--;
printf("\n");
}
space = 1;
space++;
printf("\n");
}
return 0;
}
By Tancho Divine
Output of program:
int main()
{
int n, i = 3, count, c;
if ( n >= 1 )
{
printf("First %d prime numbers are :\n",n);
printf("2\n");
}
By Tancho Divine
for ( count = 2 ; count <= n ; )
{
for ( c = 2 ; c <= i - 1 ; c++ )
{
if ( i%c == 0 )
break;
}
if ( c == i )
{
printf("%d\n",i);
count++;
}
i++;
}
return 0;
}
Output of program:
Fibonacci series in c
Fibonacci series in c programming: c program for Fibonacci series without and with recursion.
Using the code below you can print as many numbers of terms of series as desired. Numbers of
Fibonacci sequence are known as Fibonacci numbers. First few numbers of series are 0, 1, 1, 2,
3, 5, 8 etc, Except first two terms in sequence every other term is the sum of two previous terms,
For example 8 = 3 + 5 (addition of 3, 5). This sequence has many applications in mathematics
and Computer Science.
return 0;
}
Output of program:
int Fibonacci(int);
main()
{
int n, i = 0, c;
scanf("%d",&n);
By Tancho Divine
printf("Fibonacci series\n");
return 0;
}
int Fibonacci(int n)
{
if ( n == 0 )
return 0;
else if ( n == 1 )
return 1;
else
return ( Fibonacci(n-1) + Fibonacci(n-2) );
}
Recursion method is less efficient as it involves function calls which uses stack, also there are
chances of stack overflow if function is called frequently for calculating larger Fibonacci
numbers.
1
1 1
1 2 1
1 3 3 1
Pascal triangle in c
#include <stdio.h>
long factorial(int);
int main()
{
int i, n, c;
printf("\n");
}
return 0;
}
long factorial(int n)
{
int c;
long result = 1;
return result;
}
Output of program:
C programming code
#include <stdio.h>
int main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];
printf("\n");
}
return 0;
}
By Tancho Divine
Output of program:
Subtract matrices
C code to subtract matrices of any order. This program finds difference between corresponding
elements of two matrices and then print the resultant matrix.
C programming code
#include <stdio.h>
int main()
{
int m, n, c, d, first[10][10], second[10][10], difference[10][10];
printf("\n");
}
return 0;
}
Output of program:
Matrix multiplication in c
Matrix multiplication in c language: c program to multiply matrices (two dimensional array), this
program multiplies two matrices which will be entered by the user. Firstly user will enter the
order of a matrix. If the entered orders of two matrix is such that they can't be multiplied then an
error message is displayed on the screen. You have already studied the logic to multiply them in
Mathematics.
int main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
if ( n != p )
printf("Matrices with entered orders can't be multiplied with each
other.\n");
else
{
printf("Enter the elements of second matrix\n");
multiply[c][d] = sum;
sum = 0;
}
}
printf("\n");
}
}
return 0;
}
By Tancho Divine
A 3 X 3 matrix multiply in c is shown as example below.
Matrices are frequently used while doing programming and are used to represent graph data
structure, in solving system of linear equations and many more. Lot of research is being done on
how to multiply matrices using minimum number of operations. You can also implement it using
pointers.
C programming code
#include <stdio.h>
int main()
{
char array[20] = "Hello World";
printf("%s\n",array);
return 0;
}
C programming code
By Tancho Divine
#include <stdio.h>
int main()
{
char array[100];
printf("Enter a string\n");
scanf("%s", array);
int main()
{
char a[80];
gets(a);
printf("%s\n", a);
return 0;
}
Note that scanf can only input single word strings, to receive strings containing spaces use gets
function.
void print(char*);
int main() {
char s[100];
gets(s);
print(s);
return 0;
}
#include <stdio.h>
#include <string.h>
int main() {
char s[100];
int c, l;
gets(s);
l = strlen(s);
return 0;
}
String length
This program prints length of string, for example consider the string "c programming" it's length
is 13. Null character is not counted when calculating string length. To find string length we use
strlen function of string.h.
int main()
{
char a[100];
int length;
length = strlen(a);
return 0;
}
By Tancho Divine
Output of program:
You can also find string length using pointer or without strlen function. Following program
shows how to achieve this.
#include <stdio.h>
int main()
{
char array[100], *pointer;
int length = 0;
printf("Enter a string\n");
gets(array);
pointer = array;
while(*(pointer+length))
length++;
return 0;
}
while(*(s+c))
c++;
return c;
}
int main()
{
char a[100], b[100];
if( strcmp(a,b) == 0 )
printf("Entered strings are equal.\n");
else
printf("Entered strings are not equal.\n");
return 0;
}
Output of program:
main()
{
char source[] = "C program";
By Tancho Divine
char destination[50];
strcpy(destination, source);
return 0;
}
C programming code
#include<stdio.h>
main()
{
int status;
char file_name[25];
status = remove(file_name);
if( status == 0 )
printf("%s file deleted successfully.\n",file_name);
else
{
printf("Unable to delete the file\n");
perror("Error");
}
return 0;
}
By Tancho Divine
Output of program:
Deleted file doesn't go to trash or recycle bin so you may not be able to recover it. Deleted files
can be recovered using special recovery software if the files are not overwritten on the storage
medium.
int main()
{
struct date d;
getdate(&d);
This code works in Turbo C only because it supports dos.h header file.
By default shutdown occur after 30 seconds. To shutdown immediately you can write "shutdown
-s -t 0". If you wish to restart your computer then you can write "shutdown -r".
If you are using Turbo C Compiler then execute your program from command prompt or by
opening the executable file from the folder. Press F9 to build your executable file from source
program. When you run program from within the compiler by pressing Ctrl+F9 it may not work.
int main()
{
system("C:\\WINDOWS\\System32\\shutdown /s");
return 0;
}