You are on page 1of 68

Problems & Solutions

By

Noor Mohammed Anik

 Important C programming problems


 Important C programming solutions
 Some small C programming project
 Important for university students & all learners
“”””””Bismillahir Rahmanir Rahim””””””

“”””All praise is due to Allah,


Lord of the worlds””””

2
About Me……………

__ ___________________________

Noor Mohammed Anik

Student of CSE

Computer Science & Engineering

International Islamic University Chittagong

3
Table of Contents

1. Dedication ……………………………………………………………… 7

2. Introduction…………………………………………………………… 8

3. C hello world program…………………………………………….. 10

4. C program to add two numbers……………………………….. 10

5. C program print integer………………………………………… 11

6. C program to check odd or even………………………………. 11

7. C program to perform addition, subtraction

Multiplication and division ………………………………. 13

8. C program to check whether input alphabet is a vowel or not …. 14

9. Check vowel using switch statement…………………………………. 15

10. C program to check leap year ………………………………………. 15

11. Add digits of number in C ………………………………………… 16

12. Factorial program in C ……………………………………………… 17

13. Decimal to binary conversion ……………………………………17

14. C program to find ncr and npr …………………………………….19

15. C program to swap two numbers …………………………………19

4
16. C program to reverse a number ……………………………………23

17. Palindrome Numbers ……………………………………………. 24

18. C program to print patterns of numbers and stars ……….. 24

19. C program to print diamond pattern …………………………. 25

20. C program for prime number ………………………………….. ……. 26

21. Armstrong number C program …………………………….... …… 29

22. Fibonacci series in C ……………………………………………………30

23. C program to print Floyd's triangle ……………………………… 32

24. Pattern Printing of Word ………………………………………….. 33

25. C program to find maximum element in array ……………….34

26. C program to find minimum element in array…………..… 35

27. C program to insert an element in an array ……………….. 35

28. C program for matrix ………………………………………………. 37

29. String length ………………………………………………………… 42

30. C program to reverse a string using recursion ………….. 43

31. C palindrome program, C program for palindrome …….…. 44

32. Remove vowels string C ……………………………………….. 46

33. C code for all substrings of a string……………………… … 49

34. C program to sort a string in alphabetic order ……………. 49

5
35. C program remove spaces, blanks from a string ………… . .50

36. strlwr, strupr in C ……………………………………………. 51

37. Change string to upper case without strupr ………. 52

38. Change string to lower case without strlwr ………… 53

39. c program to swap two strings …………………………. 53

40. c program to find frequency of characters in a string …. 54

41. Anagram in C ………………………………………………….. 55

42. C program to print date …………………………………….. 56

43. C program to shutdown or turn off computer ………… 57

44. C programming code for Windows XP & 7 ………….. 57

45. Number Multyiply using loop(8*1) ………………….. 58

46. Summation of even & odd numbers ………………… 58

47. Exmple of nested loop ……………………………………… 60

48. Project-1: Simplee Calculator ……………………………. 60

49.Project-2: Gussing Game ………………………………….. 63

50 Project -3: FRIENDs Gussing Game …………………… 65

6
Dedication

Dennis Ritchie

The creator of C Programming Language

7
Introduction

C Programming Language is the Mother Language of all


Programming Language. There are some collection of C
programming Problems & their solutions. This code are
important for all programmers & learners. To become a good
programmer, you need to practice more & more. If you are
creative, it will also help to become a good programmer.
Practice this problems & enjoy C programming.

_____ Noor Mohammed Anik

8
Any comments or Suggestions are welcome

Facebook: www.facebook.com/Noor016

Website: www.onlinejogot.tk

Email: noor.bd92@gmail.com

Cell: 01672902634

Date of Publications :30.08.2013

9
Problems & Solutions

C hello world program


C hello world program :- code to print hello world. This program prints hello world, printf
function is used to display text on screen, '\n' places cursor on the beginning of next line.
This may be your first c code while learning programming.

C programming code
#include<stdio.h>

int main()
{
printf("Hello world\n");
return 0;
}

C program to add two numbers

10
C program to add two numbers: This c language program perform the basic arithmetic
operation of addition on two numbers and then prints the sum on the screen. For example if
the user entered two numbers as 5, 6 then 11 ( 5 + 6 ) will be printed on the screen.

C programming code
#include<stdio.h>

int main()
{
int a, b, c;

printf("Enter two numbers to add\n");


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

c = a + b;

printf("Sum of entered numbers = %d\n",c);

return 0;
}

C program print integer


This c program first inputs an integer and then prints it. Input is done using scanf function
and number is printed on screen using printf.

C programming code
#include<stdio.h>

int main()
{
int a;

printf("Enter an integer\n");
scanf("%d", &a);

printf("Integer that you have entered is %d\n", a);

return 0;
}

c program to check odd or even

11
c program to check odd or even: We will determine whether a number is odd or even by using
different methods all are provided with a code in c language. As you have study in
mathematics that in decimal number system even numbers are divisible by 2 while odd are
not so we may use modulus operator(%) which returns remainder, For example 4%3 gives 1 (
remainder when four is divided by three). Even numbers are of the form 2*p and odd are of
the form (2*p+1) where p is is an integer.

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.

In c programming language when we divide two integers we get an integer result, For
example the result of 7/3 will be 2.So we can take advantage of this and may use it to find
whether the number is odd or even. Consider an integer n we can first divide by 2 and then
multiply it by 2 if the result is the original number then the number is even otherwise the
number is odd. For example 11/2 = 5, 5*2 = 10 ( which is not equal to eleven), now consider
12/2 = 6 and 6 *2 = 12 ( same as original number). These are some logic which may help
you in finding if a number is odd or not.

C program to check odd or even using modulus operator


#include<stdio.h>

int main()
{
int n;

printf("Enter an integer\n");
scanf("%d",&n);

if ( n%2 == 0 )
printf("Even\n");
else
printf("Odd\n");

return 0;
}

C program to check odd or even using bitwise operator


#include<stdio.h>

int main()
{
int n;

printf("Enter an integer\n");

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

if ( n & 1 == 1 )
printf("Odd\n");
else
printf("Even\n");

return 0;
}

C program to check odd or even without using bitwise or


modulus operator
#include<stdio.h>

int main()
{
int n;

printf("Enter an integer\n");
scanf("%d",&n);

if ( (n/2)*2 == n )
printf("Even\n");
else
printf("Odd\n");

return 0;
}

Find odd or even using conditional operator


#include<stdio.h>

int main()
{
int n;

printf("Enter an integer\n");
scanf("%d",&n);

n%2 == 0 ? printf("Even number\n") : printf("Odd number\n");

return 0;
}

C program to perform addition, subtraction, multiplication


and division

13
C program to perform basic arithmetic operations i.e. addition , subtraction, multiplication
and division of two numbers. Numbers are assumed to be integers and will be entered by the
user.

C programming code
#include <stdio.h>

int main()
{
int first, second, add, subtract, multiply;
float divide;

printf("Enter two integers\n");


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

add = first + second;


subtract = first - second;
multiply = first * second;
divide = first / (float)second; //typecasting

printf("Sum = %d\n",add);
printf("Difference = %d\n",subtract);
printf("Multiplication = %d\n",multiply);
printf("Division = %.2f\n",divide);

return 0;
}

c program to check whether input alphabet is a vowel or not


This code checks whether an input alphabet is a vowel or not. Both lower-case and upper-case
are checked.

C programming code
#include<stdio.h>

int main()
{
char ch;

printf("Enter a character\n");
scanf("%c",&ch);

if ( ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' ||


ch == 'I' || ch =='o' || ch=='O' || ch == 'u' || ch == 'U')
printf("%c is a vowel.\n", ch);

14
else
printf("%c is not a vowel.\n", ch);

return 0;
}

Output:

Check vowel using switch statement


#include<stdio.h>

int main()
{
char ch;

printf("Enter a character\n");
scanf("%c",&ch);

switch(ch)
{
case 'a':
case 'A':
case 'e':
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;
}

15
C program to check leap year
C program to check leap year: c code to check leap year, year will be entered by the user.

C programming code
#include <stdio.h>

int main()
{
int year;

printf("Enter a year to check if it is a leap year\n");


scanf("%d", &year);

if ( year%400 == 0)
printf("%d is a leap year.\n", year);
else if ( year%100 == 0)
printf("%d is not a leap year.\n", year);
else if ( year%4 == 0 )
printf("%d is a leap year.\n", year);
else
printf("%d is not a leap year.\n", year);

return 0;
}

Add digits of number in c


C program to add digits of a number: Here we are using modulus operator(%) to extract
individual digits of number and adding them.

C programming code
#include<stdio.h>

int main()
{
int n, sum = 0, remainder;

printf("Enter an integer\n");
scanf("%d",&n);

while( n != 0 )
{
remainder = n % 10;

16
sum = sum + remainder;
n = n / 10;
}

printf("Sum of digits of entered number = %d\n",sum);

return 0;
}

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 a 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.

Factorial program in c using for loop


: Here we find factorial using for loop.

#include<stdio.h>
#include<conio.h>

int main()
{
int c, n, fact = 1;

printf("Enter a number to calculate it's factorial\n");


scanf("%d",&n);

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


fact = fact*c;

printf("Factorial of %d = %d\n",n,fact);

getch();
return 0;
}

Decimal to binary conversion


C program to convert decimal to binary: c language code to convert an integer from decimal
number system(base-10) to binary number system(base-2). Size of integer is assumed to be
32 bits. We use bitwise operators to perform the desired task. We right shift the original

17
number by 31, 30, 29, ..., 1, 0 bits using a loop and bitwise AND the number obtained with
1(one), if the result is 1 then that bit is 1 otherwise it is 0(zero).

C programming code
#include <stdio.h>

int main()
{
int n, c, k;

printf("Enter an integer in decimal number system\n");


scanf("%d",&n);

printf("%d in binary number system is:\n", n);

for ( c = 31 ; c >= 0 ; c-- )


{
k = n >> c;

if ( k & 1 )
printf("1");
else
printf("0");
}

printf("\n");

return 0;
}

Above code only prints binary of integer, but we may wish to perform operations on binary so
in the code below we are storing the binary in a string. We create a function which returns a
pointer to string which is the binary of the number passed as argument to the function.

C code to store decimal to binary conversion in a string


#include <stdio.h>
#include <stdlib.h>

char *decimal_to_binary(int);

int main()
{
int n, c, k;
char *pointer;

printf("Enter an integer in decimal number system\n");


scanf("%d",&n);

pointer = decimal_to_binary(n);
printf("Binary string of %d is: %s\n", n, t);

18
free(pointer);

return 0;
}

char *decimal_to_binary(int n)
{
int c, d, count;
char *pointer;

count = 0;
pointer = (char*)malloc(32+1);

if ( pointer == NULL )
exit(EXIT_FAILURE);

for ( c = 31 ; c >= 0 ; c-- )


{
d = n >> c;

if ( d & 1 )
*(pointer+count) = 1 + '0';
else
*(pointer+count) = 0 + '0';

count++;
}
*(pointer+count) = '\0';

return pointer;
}

C program to find ncr and npr


c program to find nCr and nPr: This code calculate nCr which is n!/(r!*(n-r)!) and nPr =
n!/(n-r)!

C program to find nCr using function


#include<stdio.h>

long factorial(int);
long find_ncr(int, int);
long find_npr(int, int);

int main()
{
int n, r;
long ncr, npr;

19
printf("Enter the value of n and r\n");
scanf("%d%d",&n,&r);

ncr = find_ncr(n, r);


npr = find_npr(n, r);

printf("%dC%d = %ld\n", n, r, ncr);


printf("%dP%d = %ld\n", n, r, npr);

return 0;
}

long find_ncr(int n, int r)


{
long result;

result = factorial(n)/(factorial(r)*factorial(n-r));

return result;
}

long find_npr(int n, int r)


{
long result;

result = factorial(n)/factorial(n-r);

return result;
}

long factorial(int n)
{
int c;
long result = 1;

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


result = result*c;

return ( result );
}

C programming code
#include <stdio.h>

int main()
{
int n, sum = 0, c, value;

printf("Enter the number of integers you want to add\n");


scanf("%d", &n);

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

20
for ( c = 1 ; c <= n ; c++ )
{
scanf("%d",&value);
sum = sum + value;
}

printf("Sum of entered integers = %d\n",sum);

return 0;
}

C program to swap two numbers


C program to swap two numbers with and without using third variable, swapping in c using
pointers and functions (Call by reference) , swapping means interchanging. For example if
in your c program you have taken two variable a and b where a = 4 and b = 5, then before
swapping a = 4, b = 5 after swapping a = 5, b = 4
In our c program to swap numbers we will use a temp variable to swap two numbers.
Swapping is used in sorting that is when we wish to arrange numbers in a particular order
either in ascending order or in descending order.

Swaping of two numbers in c


#include<stdio.h>
#include<conio.h>

int main()
{
int x, y, temp;

printf("Enter the value of x and y ");


scanf("%d%d",&x, &y);

printf("Before Swapping\nx = %d\ny = %d\n",x,y);

temp = x;
x = y;
y = temp;

printf("After Swapping\nx = %d\ny = %d\n",x,y);

getch();
return 0;
}

Swapping of two numbers without third variable

21
You can also swap two numbers without using temp or temporary or third variable. In that
case c program will be as shown :-

#include<stdio.h>

int main()
{
int a, b;

printf("Enter two numbers to swap ");


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

a = a + b;
b = a - b;
a = a - b;

printf("a = %d\nb = %d\n",a,b);


return 0;
}

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.

Swap two numbers using pointers


#include<stdio.h>

int main()
{
int x, y, *a, *b, temp;

printf("Enter the value of x and y ");


scanf("%d%d",&x,&y);

printf("Before Swapping\nx = %d\ny = %d\n", x, y);

a = &x;
b = &y;

temp = *b;
*b = *a;
*a = temp;

printf("After Swapping\nx = %d\ny = %d\n", x, y);

return 0;
}

Swapping numbers using call by reference


In this method we will make a function to swap numbers.

22
#include<stdio.h>

void swap(int*, int*);

int main()
{
int x, y;

printf("Enter the value of x and y\n");


scanf("%d%d",&x,&y);

printf("Before Swapping\nx = %d\ny = %d\n", x, y);

swap(&x, &y);

printf("After Swapping\nx = %d\ny = %d\n", x, y);

return 0;
}

void swap(int *a, int *b)


{
int temp;

temp = *b;
*b = *a;
*a = temp;
}

C program to reverse a number


C Program to reverse a number :- This program reverse the number entered by the user and
then prints the reversed number on the screen. For example if user enter 123 as input then 321
is printed as output. In our program we use modulus(%) operator to obtain the digits of a
number. To invert number look at it and write it from opposite direction or the output of code
is a number obtained by writing original number from right to left.

C programming code
#include<stdio.h>

int main()
{
int n, reverse = 0;

printf("Enter a number to reverse\n");


scanf("%d",&n);

while( n != 0 )
{
reverse = reverse * 10;
reverse = reverse + n%10;

23
n = n/10;
}

printf("Reverse of entered number is = %d\n", reverse);

return 0;
}

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.

Palindrome number algorithm


1. Get the number from user.
2. Reverse it.
3. Compare it with the number entered by the user.
4. If both are same then print palindrome number
5. Else print not a palindrome number.

Palindrome number program c


#include<stdio.h>

int main()
{
int n, reverse = 0, temp;

printf("Enter a number to check if it is a palindrome or not\n");


scanf("%d",&n);

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

24
printf("%d is not a palindrome number.\n", n);

return 0;
}

C program to print patterns of numbers and stars


These program prints various different patterns of numbers and stars. These codes illustrate
how to create various patterns using c programming. Most of these c programs involve usage
of nested loops and space. A pattern of numbers, star or characters is a way of arranging
these in some logical manner or they may form a sequence. Some of these patterns are
triangles which have special importance in mathematics. Some patterns are symmetrical
while other are not. Please see the complete page and look at comments for many different
patterns.

*
***
*****
*******
*********

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;

for ( row = 1 ; row <= n ; row++ )


{
for ( c = 1 ; c < temp ; c++ )
printf(" ");

temp--;

for ( c = 1 ; c <= 2*row - 1 ; c++ )


printf("*");

printf("\n");

25
}

return 0;
}

C program to print diamond pattern


Diamond pattern in c: This code print diamond pattern of stars. Diamond shape is as follows:

*
***
*****
***
*

C programming code
#include<stdio.h>

int main()
{
int n, c, k, space = 1;

printf("Enter number of rows\n");


scanf("%d",&n);

space = n - 1;

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


{
for ( c = 1 ; c <= space ; c++ )
printf(" ");

space--;

for ( c = 1 ; c <= 2*k-1 ; c++)


printf("*");

printf("\n");

space = 1;

for ( k = 1 ; k <= n - 1 ; k++ )


{
for ( c = 1 ; c <= space; c++)
printf(" ");

space++;

26
for ( c = 1 ; c <= 2*(n-k)-1 ; c++ )
printf("*");

printf("\n");
}

return 0;
}

C program for prime number


Prime number program in c: c program for prime number, this code prints prime numbers
using c programming language. To check whether a number is prime or not see another code
below. Prime number logic: a number is prime if it is divisible only by one and itself.
Remember two is the only even and also the smallest prime number. First few prime numbers
are 2, 3, 5, 7, 11, 13, 17....etc. Prime numbers have many applications in computer science
and mathematics. A number greater than one can be factorized into prime numbers, For
example 540 = 22*33*51

Prime number program in c language


#include<stdio.h>

int main()
{
int n, i = 3, count, c;

printf("Enter the number of prime numbers required\n");


scanf("%d",&n);

if ( n >= 1 )
{
printf("First %d prime numbers are :\n",n);
printf("2\n");
}

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++;
}

27
i++;
}

return 0;
}

There are many logic to check prime numbers, one given below is more efficient then above
method.
for ( c = 2 ; c <= (int)sqrt(n) ; c++ )
//only checking from 2 to square root of number is sufficient.

There are many more efficient logic then written above.

C program for prime number or not


#include<stdio.h>

main()
{
int n, c = 2;

printf("Enter a number to check if it is prime\n");


scanf("%d",&n);

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


{
if ( n%c == 0 )
{
printf("%d is not prime.\n", n);
break;
}
}
if ( c == n )
printf("%d is prime.\n", n);

return 0;
}

C program for prime number using function


#include<stdio.h>

int check_prime(int);

int main()
{
int n, result;

printf("Enter an integer to check whether it is prime or not.\n");


scanf("%d",&n);

result = check_prime(n);

28
if ( result == 1 )
printf("%d is prime.\n", n);
else
printf("%d is not prime.\n", n);

return 0;
}

int check_prime(int a)
{
int c;

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


{
if ( a%c == 0 )
return 0;
}
if ( c == a )
return 1;
}

Armstrong number c program


armstrong number c program: c programming code to check whether a number is armstrong
or not. A number is armstrong if the sum of cubes of individual digits of a number is equal
to the number itself. For example 371 is an armstrong number as 33 + 73 + 13 = 371. Some
other armstrong numbers are: 0, 1, 153, 370, 407.

C code
#include<stdio.h>
#include<conio.h>

int main()
{
int number, sum = 0, temp, remainder;

printf("Enter a number\n");
scanf("%d",&number);

temp = number;

while( temp != 0 )
{
remainder = temp%10;
sum = sum + remainder*remainder*remainder;
temp = temp/10;
}

29
if ( number == sum )
printf("Entered number is an armstrong number.");
else
printf("Entered number is not an armstrong number.");

getch();
return 0;
}

C program to generate and print armstrong numbers


armstrong number in c: This program prints armstrong number. In our program we ask the
user to enter a number and then we use a loop from one to the entered number and check if it
is an armstrong number and if it is then the number is printed on the screen. Remember a
number is armstrong if the sum of cubes of individual digits of a number is equal to the
number itself. For example 371 is an armstrong number as 33 + 73 + 13 = 371. Some other
armstrong numbers are 0, 1, 153, 370, 407.

C code
#include<stdio.h>
#include<conio.h>

int main()
{
int r;
long number = 0, c, sum = 0, temp;

printf("Enter the maximum range upto which you want to find armstrong
numbers ");
scanf("%ld",&number);

printf("Following armstrong numbers are found from 1 to %ld\n",number);

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


{
temp = c;
while( temp != 0 )
{
r = temp%10;
sum = sum + r*r*r;
temp = temp/10;
}
if ( c == sum )
printf("%ld\n", c);
sum = 0;
}

getch();
return 0;

30
}

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 number 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.

Fibonacci series in c using for loop


/* Fibonacci Series c language */
#include<stdio.h>

int main()
{
int n, first = 0, second = 1, next, c;

printf("Enter the number of terms\n");


scanf("%d",&n);

printf("First %d terms of Fibonacci series are :-\n",n);

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


{
if ( c <= 1 )
next = c;
else
{
next = first + second;
first = second;
second = next;
}
printf("%d\n",next);
}

return 0;
}

Fibonacci series program in c using recursion


#include<stdio.h>

31
int Fibonacci(int);

main()
{
int n, i = 0, c;

scanf("%d",&n);

printf("Fibonacci series\n");

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


{
printf("%d\n", Fibonacci(i));
i++;
}

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) );
}

C program to print Floyd's triangle


C program to print Floyd's triangle:- This program prints Floyd's triangle. Number of rows
of Floyd's triangle to print is entered by the user. First four rows of Floyd's triangle are as
follows :-
1
23
456
7 8 9 10
It's clear that in Floyd's triangle nth row contains n numbers.

C programming code
#include<stdio.h>
#include<conio.h>

int main()
{
int n, i, c, a = 1;

32
printf("Enter the number of rows of Floyd's triangle to print\n");
scanf("%d",&n);

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


{
for ( c = 1 ; c <= i ; c++ )
{
printf("%d ",a);
a++;
}
printf("\n");
}

getch();
return 0;
}

Pattern Printing of Word


EDCBA
DCBA
CBA
BA
A

#include<stdio.h>

int main()
{
int n, c, k, space = 0;
char ch, temp;

scanf("%d", &n);

ch = 'A'+n-1;
temp = ch;

for ( k = n ; k >= 1 ; k-- )


{
for ( c = 1 ; c <= space; c++)
printf(" ");

space++;

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


{
printf("%c",temp);
temp--;
}

printf("\n");
ch--;
temp = ch;
}

33
return 0;
}

Pattern Printing by Word


A
BB
CCC
DDDD
EEEEE

#include<stdio.h>

int main()
{
int c, n, k;
char ch = 'A';

printf("Enter number of rows\n");


scanf("%d",&n);

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


{
for ( k = 1 ; k <= c ; k++)
printf("%c ", ch);

printf("\n");
ch++;
}

return 0;
}

c program to find maximum element in array


This code find maximum or largest element present in an array. It also prints the location or
index at which maximum element occurs in array. This can also be done by using pointers
(see both codes).

C programming code
#include <stdio.h>

int main()
{

34
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 number %d and it's value


is %d.\n", location, maximum);
return 0;
}

C program to find minimum element in array


C code to find minimum or smallest element present in an array. It also prints the location or
index at which minimum element occurs in array. This can also be done by using pointers
(see both the codes).

C programming code
#include <stdio.h>

int main()
{
int array[100], minimum, 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]);

minimum = array[0];

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

35
{
if ( array[c] < minimum )
{
minimum = array[c];
location = c+1;
}
}

printf("Minimum element is present at location number %d and it's value


is %d.\n", location, minimum);
return 0;
}

C program to insert an element in an array


This code will insert an element into an array, For example consider an array a[10] having
three elements in it initially and a[0] = 1, a[1] = 2 and a[2] = 3 and you want to insert a
number 45 at location 1 i.e. a[0] = 45, so we have to move elements one step below so after
insertion a[1] = 1 which was a[0] initially, and a[2] = 2 and a[3] = 3. Array insertion
does not mean increasing its size i.e array will not be containing 11 elements.

C programming code
#include <stdio.h>

int main()
{
int array[100], position, c, n, value;

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


scanf("%d", &n);

printf("Enter %d elements\n", n);

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


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

printf("Enter the location where you wish to insert an element\n");


scanf("%d", &position);

printf("Enter the value to insert\n");


scanf("%d", &value);

for ( c = n - 1 ; c >= position - 1 ; c-- )


array[c+1] = array[c];

array[position-1] = value;

printf("Resultant array is\n");

36
for( c = 0 ; c <= n ; c++ )
printf("%d\n", array[c]);

return 0;
}

C program to delete an element from an array


This program delete an element from an array. Deleting an element does not affect the size of
array. It is also checked whether deletion is possible or not, For example if array is containing
five elements and you want to delete element at position six which is not possible.

c programming code
#include <stdio.h>

int main()
{
int array[100], position, c, n;

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


scanf("%d", &n);

printf("Enter %d elements\n", n);

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


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

printf("Enter the location where you wish to delete element\n");


scanf("%d", &position);

if ( position >= n+1 )


printf("Deletion not possible.\n");
else
{
for ( c = position - 1 ; c < n - 1 ; c++ )
array[c] = array[c+1];

printf("Resultant array is\n");

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


printf("%d\n", array[c]);
}

return 0;
}

37
c program to add two matrix
This c program add two matrices i.e. compute the sum of two matrices and then print it.
Firstly user will be asked to enter the order of matrix ( number of rows and columns ) and
then two matrices. For example if the user entered order as 2, 2 i.e. two rows and two columns
and matrices as
First Matrix :-
12
34
Second matrix :-
45
-1 5
then output of the program ( sum of First and Second matrix ) will be
57
29

C programming code
#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 ");


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]);

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


for ( d = 0 ; d < n ; d++ )
sum[c][d] = first[c][d] + second[c][d];

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

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


{
for ( d = 0 ; d < n ; d++ )
printf("%d\t", sum[c][d]);

38
printf("\n");
}

return 0;
}

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 code
#include<stdio.h>
#include<conio.h>

int main()
{
int m, n, c, d, first[10][10], second[10][10], difference[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]);

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


for ( d = 0 ; d < n ; d++ )
difference[c][d] = first[c][d] - second[c][d];

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

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


{
for ( d = 0 ; d < n ; d++ )
printf("%d\t",difference[c][d]);

printf("\n");
}

getch();
return 0;

39
}

c program to transpose a matrix


This c program prints transpose of a matrix. It is obtained by interchanging rows and
columns of a matrix. For example if a matrix is
12
34
56
then transpose of above matrix will be
135
246
When we transpose a matrix then the order of matrix changes, but for a square matrix order
remains same.

C programming code
#include<stdio.h>

int main()
{
int m, n, c, d, matrix[10][10], transpose[10][10];

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


scanf("%d%d",&m,&n);
printf("Enter the elements of matrix \n");

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


{
for( d = 0 ; d < n ; d++ )
{
scanf("%d",&matrix[c][d]);
}
}

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


{
for( d = 0 ; d < n ; d++ )
{
transpose[d][c] = matrix[c][d];
}
}

printf("Transpose of entered matrix :-\n");

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


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

40
{
printf("%d\t",transpose[c][d]);
}
printf("\n");
}

return 0;
}

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. Matrices are frequently used while doing
programming and are used to represent graph data structure, in solving system of linear
equations and many more.

Matrix multiplication in c language


#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 the number of rows and columns of first 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 number of rows and columns of second matrix\n");


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

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");

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


for ( d = 0 ; d < q ; d++ )
scanf("%d", &second[c][d]);

41
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 entered matrices:-\n");

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


{
for ( d = 0 ; d < q ; d++ )
printf("%d\t", multiply[c][d]);

printf("\n");
}
}

return 0;
}

c program print string


This program print a string. String can be printed by using various functions such as
printf, puts.

C programming code
#include <stdio.h>

int main()
{
char array[20] = "Hello World";

printf("%s\n",array);

return 0;
}

String length

42
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.

C program to find string length


#include<stdio.h>
#include<string.h>

int main()
{
char a[100];
int length;

printf("Enter a string to calculate it's length\n");


gets(a);

length = strlen(a);

printf("Length of entered string is = %d\n",length);

return 0;
}

C program to reverse a string using recursion


#include<stdio.h>
#include<string.h>

void reverse(char*,int,int);

int main()
{
char a[100];

gets(a);

reverse(a, 0, strlen(a)-1);

printf("%s\n",a);

return 0;
}

void reverse(char *x, int beg, int end)


{
char a, b, c;

if ( beg >= end )


return;

43
c = *(x+beg);
*(x+beg) = *(x+end);
*(x+end) = c;

reverse(x, ++beg, --end);


}

C program to reverse a string using pointers


: Now we will invert string using pointers or without using library function strrev.

#include<stdio.h>

int string_length(char*);
void reverse(char*);

int main()
{
char string[100];

printf("Enter a string\n");
gets(string);

reverse(string);

printf("Reverse of entered string is \"%s\".\n", string);

return 0;
}

void reverse(char *string)


{
int length, c;
char *begin, *end, temp;

length = string_length(string);

begin = string;
end = string;

for ( c = 0 ; c < ( length - 1 ) ; c++ )


end++;

for ( c = 0 ; c < length/2 ; c++ )


{
temp = *end;
*end = *begin;
*begin = temp;

begin++;
end--;
}

44
}

int string_length(char *pointer)


{
int c = 0;

while( *(pointer+c) != '\0' )


c++;

return c;
}

C palindrome program, c program for palindrome


C program for palindrome or palindrome in c programming: palindrome program in c
language, c code to check if a string is a palindrome or not and for palindrome number. This
program works as follows :- at first we copy the entered string into a new string, and then we
reverse the new string and then compares it with original string. If both of them have same
sequence of characters i.e. they are identical then the entered string is a palindrome otherwise
not. To perform copy, reverse and compare operations we use strcpy, strrev and strcmp
functions of string.h respectively, if you do not wish to use these functions see c
programming code for palindrome without using string functions. Some palindrome strings
examples are "dad", "radar", "madam" etc.

C program for palindrome


#include <stdio.h>
#include <string.h>

int main()
{
char a[100], b[100];

printf("Enter the string to check if it is a palindrome\n");


gets(a);

strcpy(b,a);
strrev(b);

if( strcmp(a,b) == 0 )
printf("Entered string is a palindrome.\n");
else
printf("Entered string is not a palindrome.\n");

return 0;
}

45
Palindrome number in c
#include <stdio.h>

int main()
{
int n, reverse = 0, temp;

printf("Enter a number to check if it is a palindrome or not\n");


scanf("%d",&n);

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;
}

C program for palindrome without using string functions


#include <stdio.h>
#include <string.h>

int main()
{
char text[100];
int begin, middle, end, length = 0;

gets(text);

while ( text[length] != '\0' )


length++;

end = length - 1;
middle = length/2;

for( begin = 0 ; begin < middle ; begin++ )


{
if ( text[begin] != text[end] )

46
{
printf("Not a palindrome.\n");
break;
}
end--;
}
if( begin == middle )
printf("Palindrome.\n");

return 0;
}

Remove vowels string c


Remove vowels string c: c program to remove or delete vowels from a string, if the input
string is "c programming" then output will be "c prgrmmng". In the program we create a new
string and process entered string character by character, and if a vowel is found it is not
added to new string otherwise the character is added to new string, after the string ends we
copy the new string into original string. Finally we obtain a string without any vowels.

C programming code
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define TRUE 1
#define FALSE 0

int check_vowel(char);

int main()
{
char string[100], *temp, *pointer, ch, *start;

printf("Enter a string\n");
gets(string);

temp = string;
pointer = (char*)malloc(100);

if( pointer == NULL )


{
printf("Unable to allocate memory.\n");
exit(EXIT_FAILURE);
}

start = pointer;

while(*temp)
{

47
ch = *temp;

if ( !check_vowel(ch) )
{
*pointer = ch;
pointer++;
}
temp++;
}
*pointer = '\0';

pointer = start;
strcpy(string, pointer); /* If you wish to convert original string */
free(pointer);

printf("String after removing vowel is \"%s\"\n", string);

return 0;
}

int check_vowel(char a)
{
if ( a >= 'A' && a <= 'Z' )
a = a + 'a' - 'A';

if ( a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')


return TRUE;

return FALSE;
}

C substring code
#include<stdio.h>
#include<malloc.h>

char* substring(char*, int, int);

int main()
{
char string[100], *pointer;
int position, length;

printf("Enter a string\n");
gets(string);

printf("Enter the position and length of substring\n");


scanf("%d%d",&position, &length);

pointer = substring( string, position, length);

printf("Required substring is \"%s\"\n", pointer);

48
free(pointer);

return 0;
}

/*C substring function: It returns a pointer to the substring */

char *substring(char *string, int position, int length)


{
char *pointer;
int c;

pointer = malloc(length+1);

if( pointer == NULL )


{
printf("Unable to allocate memory.\n");
exit(EXIT_FAILURE);
}

for( c = 0 ; c < position -1 ; c++ )


string++;

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


{
*(pointer+c) = *string;
string++;
}

*(pointer+c) = '\0';

return pointer;
}

C code for all substrings of a string


#include<stdio.h>
#include<string.h>
#include<malloc.h>

char* substring(char*, int, int);

int main()
{
char string[100], *pointer;
int position = 1, length = 1, temp, string_length;

printf("Enter a string\n");
gets(string);

temp = string_length = strlen(string);

49
printf("Substring of \"%s\" are\n", string);

while ( position <= string_length )


{
while ( length <= temp )
{
pointer = substring(string, position, length);
printf("%s\n", pointer);
free(pointer);
length++;
}
temp--;
position++;
length = 1;
}

return 0;
}

C program to sort a string in alphabetic order


C program to sort a string in alphabetic order: For example if user will enter a string
"programming" then output will be "aggimmnoprr" or output string will contain characters
in alphabetical order.

C programming code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void sort_string(char*);

int main()
{
char string[100];

printf("Enter some text\n");


gets(string);

sort_string(string);
printf("%s\n", string);

return 0;
}

void sort_string(char *s)


{
int c, d = 0, length;
char *pointer, *result, ch;

50
length = strlen(s);

result = (char*)malloc(length+1);

pointer = s;

for ( ch = 'a' ; ch <= 'z' ; ch++ )


{
for ( c = 0 ; c < length ; c++ )
{
if ( *pointer == ch )
{
*(result+d) = *pointer;
d++;
}
pointer++;
}
pointer = s;
}
*(result+d) = '\0';

strcpy(s, result);
free(result);
}

C program remove spaces, blanks from a string


C code to remove spaces or excess blanks from a string, For example consider the string

"c programming"

there are two spaces in this string, so our program will print a string "c programming". It will
remove spaces when they occur more than one time consecutively in string anywhere.

C programming code
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define SPACE ' '

int main()
{
char string[100], *blank, *start;
int length, c = 0, d = 0;

printf("Enter a string\n");
gets(string);

51
length = strlen(string);

blank = string;

start = (char*)malloc(length+1);

if ( start == NULL )
exit(EXIT_FAILURE);

while(*(blank+c))
{
if ( *(blank+c) == SPACE && *(blank+c+1) == SPACE )
{}
else
{
*(start+d) = *(blank+c);
d++;
}
c++;
}
*(start+d)='\0';

printf("%s\n", start);

free(start);

return 0;
}

strlwr, strupr in c
Here we will change string case with and without strlwr, strupr functions.

strlwr in c
#include<stdio.h>
#include<string.h>

int main()
{
char string[] = "Strlwr in C";

printf("%s\n",strlwr(string));

return 0;
}

strupr in c
#include<stdio.h>

52
#include<string.h>

int main()
{
char string[] = "strupr in c";

printf("%s\n",strupr(string));

return 0;
}

Change string to upper case without strupr


#include<stdio.h>

void upper_string(char*);

int main()
{
char string[100];

printf("Enter a string to convert it into upper case\n");


gets(string);

upper_string(string);

printf("Entered string in upper case is \"%s\"\n", string);

return 0;
}

void upper_string(char *string)


{
while(*string)
{
if ( *string >= 'a' && *string <= 'z' )
{
*string = *string - 32;
}
string++;
}
}

Change string to lower case without strlwr


#include<stdio.h>

void lower_string(char*);

int main()
{
char string[100];

printf("Enter a string to convert it into lower case\n");

53
gets(string);

lower_string(string);

printf("Entered string in lower case is \"%s\"\n", string);

return 0;
}

void lower_string(char *string)


{
while(*string)
{
if ( *string >= 'A' && *string <= 'Z' )
{
*string = *string + 32;
}
string++;
}
}

c program to swap two strings


C program to swap strings.

C programming code
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<conio.h>

int main()
{
char first[100], second[100], *temp;

printf("Enter the first string ");


gets(first);

printf("Enter the second string ");


gets(second);

printf("\nBefore Swapping\n");
printf("First string: %s\n",first);
printf("Second string: %s\n\n",second);

temp = (char*)malloc(100);

strcpy(temp,first);
strcpy(first,second);
strcpy(second,temp);

54
printf("After Swapping\n");
printf("First string: %s\n",first);
printf("Second string: %s\n",second);

getch();
return 0;
}

C program to find frequency of characters in a string


This program computes frequency of characters in a string i.e. which character is present how
many times in a string. For example in the string "code" each of the character 'c', 'o', 'd', and
'e' has occurred one time. Only lower case alphabets are considered, other characters (
uppercase and special characters ) are ignored. You can easily modify this program to handle
uppercase and special symbols.

C programming code
#include<stdio.h>
#include<string.h>

int main()
{
char string[100], ch;
int c = 0, count[26] = {0};

printf("Enter a string\n");
gets(string);

while ( string[c] != '\0' )


{
/* Considering characters from 'a' to 'z' only */

if ( string[c] >= 'a' && string[c] <= 'z' )


count[string[c]-'a']++;

c++;
}

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


{
if( count[c] != 0 )
printf("%c occurs %d times in the entered
string.\n",c+'a',count[c]);
}

return 0;
}

55
Anagram in c
c program to check whether two strings are anagrams or not, string is assumed to consist of
alphabets only. Two words are said to be anagrams of each other if the letters from one word
can be rearranged to form the other word. From the above definition it is clear that two strings
are anagrams if all characters in both strings occur same number of times. For example "abc"
and "cab" are anagram strings, here every character 'a', 'b' and 'c' occur only one time in both
strings. Our algorithm tries to find how many times characters appears in the strings and
then comparing their corresponding counts.

C anagram programming code


#include<stdio.h>

int check(char [], char []);

int main()
{
char a[100], b[100];
int flag;

printf("Enter first string\n");


gets(a);

printf("Enter second string\n");


gets(b);

flag = check(a, b);

if ( flag == 1 )
printf("\"%s\" and \"%s\" are anagrams.\n", a, b);
else
printf("\"%s\" and \"%s\" are not anagrams.\n", a, b);

return 0;
}

int check(char a[], char b[])


{
int first[26] = {0}, second[26] = {0}, c = 0;

while ( a[c] != '\0' )


{
first[a[c]-'a']++;
c++;
}

c = 0;

while ( b[c] != '\0' )

56
{
second[b[c]-'a']++;
c++;
}

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


{
if( first[c] != second[c] )
return 0;
}

return 1;
}

C program to print date


This c program prints current system date. To print date we will use getdate function.

C programming code
#include<stdio.h>
#include<conio.h>
#include<dos.h>

int main()
{
struct date d;

getdate(&d);

printf("Current system date is %d/%d/%d",d.da_day,d.da_mon,d.da_year);


getch();
return 0;
}

C program to shutdown or turn off computer


C Program to shutdown your computer: This program turn off i.e shutdown your computer
system. Firstly it will asks you to shutdown your computer if you press 'y' the your
computer will shutdown in 30 seconds, system function of "stdlib.h" is used to run an
executable file shutdown.exe which is present in C:\WINDOWS\system32 in Windows XP.
You can use various options while executing shutdown.exe for example -s option shutdown the
computer after 30 seconds, if you wish to shutdown immediately then you can write
"shutdown -s -t 0" as an argument to system function. If you wish to restart your computer
then you can write "shutdown -r".

57
If you are using Turbo C Compiler then execute your file from folder. Press F9 to build your
executable file from source program. When you run from within the compiler by pressing
Ctrl+F9 it may not work.

C programming code for Windows XP


#include<stdio.h>
#include<stdlib.h>

int main()
{
char ch;

printf("Do you want to shutdown your computer now (y/n) ");


scanf("%c",&ch);

if( ch == 'y' || ch == 'Y' )


system("C:\\WINDOWS\\System32\\shutdown -s");

return 0;
}

C programming code for Windows 7


#include<stdio.h>
#include<stdlib.h>

int main()
{
char ch;

printf("Do you want to shutdown your computer now (y/n)\n");


scanf("%c",&ch);

if( ch == 'y' || ch == 'Y' )


system("C:\\WINDOWS\\System32\\shutdown /s");

return 0;
}

To shutdown immediately use "C:\\WINDOWS\\System32\\ shutdown /s /t 0". To restart use /r


instead of /s.

Number Mulyiply using loop(8*1)

source code
#include<stdio.h>
#include<conio.h>

58
int main()
{
int a=8;
int b=1;
while(b<=10){
printf("%d X %d = %d\n\n",a,b,a*b);
b=b+1;
}
getch();
return 0;

Summation of odd & even numbers


Even numbers :1+2+3+4+…………+ n
Odd Numbers : 1+3+5+7+9+………+ n

C program code

Even Numbers:

#include<stdio.h>
int main()
{
int n;
int sum=0;
int i=1;
printf("Please input your number\n");
scanf("%d",&n);
while(i<=n){
if(i%2==0){
printf("\t\t%d\n",i);
sum=sum+i;
}
i++;
}
printf("_________________________\n");
printf("sumation is = %d\n",sum);

return 0;
}

59
Odd Numbers:

#include<stdio.h>
int main()
{
int n;
int sum=0;
int i=1;
printf("Please input your number\n");
scanf("%d",&n);
while(i<=n){
if(i%2!=0){
printf("\t\t%d\n",i);
sum=sum+i;
}
i++;
}
printf("___________________________\n");
printf("sumation is = %d\n",sum);

getch();
return 0;
}

Exmple of Nested Loop


#include<stdio.h>
#include<conio.h>
int main(){
int n,i;
for(n=1;n<=10;n=n+1){
for(i=1;i<=20;i=i+1){
printf("%d X %d = %d\n\n",n,i,n*i);
}
getch();
return 0;
}
}

Project-1:Simple Calculator
This is a simple calculator project by me. It is use for two numbers
type this code & run in code blocks or other IDE

60
Code

#include<stdio.h>
#include<stdlib.h>
int clearScreen()
{
int s;
printf("If you want to Clear This screen plese input 5 or any number\n");
scanf("%d",&s);
if(s==5)
system("cls");
return 0;
}
int adding()
{
float x,y;
printf("Please Input Two Numbers\n");
printf("Your First Number :\n");
scanf("%f",&x);
printf("Your Second Number :\n");
scanf("%f",&y);
printf("Your Result is :%0.3f + %0.3f = %0.3f\n",x,y,x+y);
return 0;

}
int substruct()
{
float x,y;
printf("Please Input Two Numbers\n");
printf("Your First Number :\n");
scanf("%f",&x);
printf("Your Second Number :\n");
scanf("%f",&y);
printf("Your Result is :%0.3f - %0.3f = %0.3f\n",x,y,x-y);
return 0;

}
int multiply()
{

61
float x,y;
printf("Please Input Two Numbers\n");
printf("Your First Number :\n");
scanf("%f",&x);
printf("Your Second Number :\n");
scanf("%f",&y);
printf("Your Result is :%0.3f * %0.3f = %0.3f\n",x,y,x*y);
return 0;

}
int divide()
{
float x,y;
printf("Please Input Two Numbers\n");
printf("Your First Number :\n");
scanf("%f",&x);
printf("Your Second Number :\n");
scanf("%f",&y);
printf("Your Result is :%0.3f / %0.3f = %0.3f\n",x,y,x/y);
return 0;

}
int main()
{

double x,y;
int choice;
while(1)

printf("Simple Calculator Project By Noor Md Anik\n");


printf("\nEnter 1 for Adding(+)\nEnter 2 for Substruct(-)\nEnter 3 for
Multiply(*)\nEnter 4 for Divide(/)\nEnter 5 for clear screen\nEnter 0 for Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
adding();
break;

62
case 2:
substruct();
break;
case 3:
multiply();
break;
case 4:
divide();
break;
case 5:
system("cls");
break;
case 0:
return 0;
break;

}
return 0;

Project-2: Gussing Game


This is a 2nd project …Copy this code into code blocks & run..

Source Code

#include<stdio.h>
#include<conio.h>

int main()
{
int a,b,c,d,e,f,result;
puts("\n\t\t\tWELCOME TO THE GUSSING GAME\n");
puts("\t\t\t!!!!Play Game & Surprise !!!!");
puts("\t\t|===============================================");

63
puts("\t\t===============================================|");

puts("**************************************************************
*********\n");
puts("\t\t\t****INSTRUCTION****\n**1.Je sonka golo type korte bola hobe ta
obossoi type korte hobe.\n**2.onno number type korle game ti wrong answer
dibe.\n");

puts("**************************************************************
*********\n");
puts("\n\t\t\t*****Start Game Enter 1 *****\n\n\t\t\t*****Exit Game Enter 2
*****");
scanf("%d",&f);
if(f==2){
return puts("\n\t\t\t\t**GOOD BYE**\n\n \t\t This Game is Created by
Noor Md. Anik\n");

else{
puts("\t\t****** YOUR GAME IS START NOW ********");

puts("\======================================================
=====");
printf("\n\n\tFriend, Tomi mone mone 1ti sonka doro & 10 type koro\n\n");
scanf("%d",&a);
printf("\n\tTomi je sonkati mone mone dorco take 2 dara gon koro & 20 type
koro\n\n");
scanf("%d",&b);

printf("\n\tAkon gonfoler sate 30 jog koro & 30 type koro\n\n");


scanf("%d",&c);
puts("\n\tJog kore ja pale take 2 dara vag koro & 40 type koro\n\n");
scanf("%d",&d);
puts("\n\tAkon vagfol teke mone mone dora sonkati biyog koro & 50 type
koro\n\n\n");

64
scanf("%d",&e);
result=(a+b+c+d+e)/10;

puts("=======================================================
========================");
printf("\n\n\t **** Tomar mone mone dora sonkati = %d ****\n\n ",result);
puts("\n\n\t ***** THANKS FOR PLAY THE GAME ******\n\n \tThis Game
is Created by Noor Md. Anik\n\n");

}
getch();
return 0;

Project -3: FRIENDs Gussing Game

This game project written by me. In this game I am use graphics or colour in code
blocks

Source Code:

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

int main()
{
int a,b,c,result1,result2;
system("color 1a");
puts("\n\t\t\tWELCOME TO THE FRIENDsGUSSING GAME\n");
puts("\t\t\t!!!!Play Game & Surprise !!!!");
puts("\t\t|===============================================");
puts("\t\t===============================================|");

puts("**************************************************************
*********\n");

65
puts("\t\t\t****INSTRUCTION****\n**1.Mone mone tomake 2 ti sonka dorte
hobe.\n**2.Tarpor sonka 2 tir Jogfol & Bioyogfol input dite hobe.\n");

puts("**************************************************************
*********\n");
puts("\n\t\t\t*****Start Game Enter 1 *****\n\n\t\t\t*****Exit Game Enter 2
*****");
scanf("%d",&c);
if(c==2){
puts("\n\t\t\t\t**GOOD BYE**\n\n \t\t This Game is Created by Noor Md.
Anik\n");
getch();
return 0;

else{
puts("\t\t****** YOUR GAME IS START NOW ********");

puts("\======================================================
=====");
printf("\n\n\tFriend, Tomi mone mone 2 ti sonka doro & sonka 2 tir jogfol type
koro\n\n");
scanf("%d",&a);
printf("\n\tAkon sonka 2 tir bioyogfol type koro & Enter dao\n\n");
scanf("%d",&b);

result1=(a+b)/2;
result2=(a-b)/2;

puts("=======================================================
========================");
printf("\n\n\t **** Tomar mone mone dora 1st sonkati = %d****\n\n ",result1);
printf("\n\n\t **** Tomar mone mone dora 2nd sonkati = %d****\n\n
",result2);

66
puts("\n\n\t ***** THANKS FOR PLAY THE GAME ******\n\n \tThis Game
is Created by Noor Md. Anik\n\n");

}
getch();
return 0;

Some Important Symbol :

Noor Mohammed Anik


student of CSE
International Islamic University Chittagong

67
68

You might also like