Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

Simple C program codes.

'#include' preprocessor directive is used to paste code of given file into


current file. It is used to include system-defined and user-defined header
files. If included file is not found, compiler renders error.

'<stdio.h>' is a header file which has the necessary information to


include the input/output related functions in our program. Example printf,
scanf etc. If we want to use printf or scanf function in our program, we
should include the stdio.h header file in our source code.

'int main()' means that our function needs to return some integer at the
end of the execution and we do so by returning 0 at the end of the program.
0 is the standard for the “successful execution of the program”.

'printf(); ' function is used to print the “character, string, float, integer,
octal, and hexadecimal " values onto the output screen.

'return 0; ' means that the program will execute successfully and did
what it was intended to do.

1) Your first simple C program for showing the simple message.

Code:

#include<stdio.h>

int main()

printf(" Welcome to C program. ");

return 0;

Output: Welcome to C program.

2) A program of C programming language for display multiple message.

Code:

#include<stdio.h>

I
int main()

printf(" Hello!! world. \n"); // '\n' means new line character (it is a comment)

printf(" Let's learn C programming language.\n");

printf(" Come with me in the programming world!!!");

return 0;

3) A program for display multiple message without use of '\n' in source


code.

Code:

#include<stdio.h>

int main()

printf("A programming language ");

printf(" is a language ");

printf(" which help you to develop various application for computer. ");

return 0;

4) A simple c program for addition.

Here, 'int' means integer which stores and display all numbers (except
decimal numbers). We use printf() function with a %d format specifier to
display the value of an integer variable. '+' sign is used an operator for add
two integer value in your program.

code:

#include<stdio.h>

int main()
II
{

int a=5;

int b=35;

int x=a+b;

printf("The addition is %d",x);

return 0;

5) A simple C program for subtraction.

Code:

#include<stdio.h>

int main()

int x=90;

int y=58;

int sub_a=x-y;

printf("The Subtraction is %d",sub_a);

int sub_b=y-x;

printf("\nThe subtraction is %d,sub_b);

return 0;

6) A simple C program for multiplication.

code:

#include<stdio.h>

int main()

III
{

int a=47;

int v=23;

int c=a*v;

printf("The multiplication is %d",c);

return 0;

7) A simple C program for division.

code:

#include<stdio.h>

int main()

int a=15;

int b=6;

int c=a/b;

printf("The division is %d",c);

return 0;

8)A simple C program for find out the remainder.

Code:

#include<stdio.h>

int main()

int a=15;

IV
int b=6;

int c=a%b;

printf("The remainder is %d",c);

return 0;

9) A program for adding any two numbers during run time.

Here, 'scanf(); ' is used for collect data from user.

Code:

#include<stdio.h>

int main()

int a,b,sum=0;

printf("Enter 1st Number : ");

scanf("%d",&a);

printf("Enter 2nd Number : ");

scanf("%d",&b);

sum=a+b;

printf("\n Addition is %d",sum);

return 0;

10) A program for subtraction(during run time).

Code:

#include<stdio.h>

int main()

V
{

int a,b;

int sub=0;

printf("Enter any two number : ");

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

sub=a-b;

printf("\n\n The subtraction is %d",sub);

return 0;

11) A program for multiplication of two number during run time.

Code:

#include<stdio.h>

int main()

int a;

int b;

int mult=0;

printf("Enter any two numbers : ");

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

mult=a*b;

printf("\n\t The multiplication is %d",mult);

return 0;

VI
12) A program to add four numbers.

Code:

#include<stdio.h>

int main()

int a,b,c,d,sum;

printf("Enter any four numbers : ");

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

sum=a+b+c+d;

printf("\n\t The result is %d",sum);

return 0;

13) A program to find out the area of triangle.

Here, 'float' variable is used to store and display whole numbers including
decimal place number also.

%c is used to display character, %f for float variable, %s for a string


variable, %f for double and %x hexadecimal variable.

Code:

#include<stdio.h>

int main()

float h,b,area=0;

printf("It is program for find out the area of triangle.\n");

printf("\t--------------------------------------------\n");

printf("Enter the height of a triangle : ");

VII
scanf("%f",&h);

printf("Enter the breadth of a triangle : ");

scanf("%f",&b);

printf("\n\t----------------------------------------");

area=0.5*b*h;

printf("\n\t The area of the triangle is : %f",area);

return 0;

14) A program to find out the area of circle.

Code:

#include<stdio.h>

int main()

float r;

float area=0;

printf("Enter the radius of circle : ");

scanf("%f",&r);

area=3.14*r*r;

printf("----------------------------------------------\n");

printf("\n\t The area of circle is : %f",area);

return 0;

VIII
15) A program to find out the greater number using if...else condition.

Code:

#include<stdio.h>

int main()

int a,b;

printf("Enter any two numbers : ");

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

if(a>b)

printf("\tThe greater number is: %d",a);

else

printf("\tThe greater number is: %d",b);

return 0;

16) A program to find out the smaller number.

Code:

#include<stdio.h>

int main()

int a,b;

IX
printf("Enter any two number : ");

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

if(a<b)

printf("The smaller number is: %d",a);

else

printf("The smaller number is: %d",b);

return 0;

17) A program to check that the given number is odd or even.

Code:

#include<stdio.h>

int main()

int n;

printf("Enter any non-decimal number : ");

scanf("%d",&n);

if(n%2 == 0)

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

X
else

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

return 0;

18) A program to find out the smallest number among three numbers.

Code:

#include<stdio.h>

int main()

int a,b,c;

printf("Enter any three numbers : ");

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

if((a<b)&&(a<c))

printf("\n The smallest number is %d",a);

else

if((b<a)&&(b<c))

printf("\n The smallest number is %d",b);

XI
else

printf("\n The smallest number is %d",c);

return 0;

19) A program to find out the greatest number among three numbers.

Code:

#include<stdio.h>

int main()

int a,b,c;

printf("Enter any three number : ");

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

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

printf("\t The greatest number is %d",a);

else

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

printf("\t The greatest number is %d",b);

XII
}

else

printf("\t The greatest number is %d",c);

return 0;

20) A program for find out factorial of a user entered number using 'for'
loop

Hint: Loop is a continuous process which was stopped by condition. e.g.


for(i=1;i<=10;i++) means that 'for' loop is start from 1 and grows as 2,3,4,
....... but it is stop when it reached 10. The factorial of 3 is 3*2*1=6 and 4 is
4*3*2*1=24 and 10 is 10*9*8*7*6*5*4*3*2*1=3628800

Code:

#include <stdio.h>

int main()

int n,fact=1,i;

printf("enter a number ");

scanf("%d",&n);

for(i=1;i<=n;i++) // i++ is also equal with i=i+1

fact=fact*i;

printf("The factorial of %d is %d",n,fact);

return 0;

}
XIII
21) A program for find out factors of a number.

Code:

#include <stdio.h>

int main()

int num, i;

printf("Enter a positive integer: ");

scanf("%d", &num);

printf("Factors of %d are: ", num);

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

if (num % i == 0)

printf("%d ", i);

return 0;

22) A program is for find out LCM of two number.

Code:

#include <stdio.h>

int main()

int n1, n2, i, gcd, lcm;

XIV
printf("Enter two positive integers: ");

scanf("%d %d", &n1, &n2);

for (i = 1; i <= n1 && i <= n2; ++i)

// check if i is a factor of both integers

if (n1 % i == 0 && n2 % i == 0)

gcd = i;

lcm = (n1 * n2) / gcd;

printf("The LCM of two numbers %d and %d is %d.", n1, n2, lcm);

return 0;

23)A program to find out the simple interest.

Code:

#include<stdio.h>

int main()

int p,n,r;

float si,t;

printf("======== Simple interest calculation app =========");

printf("\n\n\n Enter the loan / principal amount : Rs.");

scanf("%d",&p);

printf("Enter the loan period in year : ");

scanf("%d",&n);

XV
printf("Enter the interest rate per year : @ ");

scanf("%d",&r);

si=p*n*r/100;

printf("\n\t The simple interest of Rs. %d is : %f",p,si);

t=p+si;

printf("\n Your payable amount after %d years with interest is : Rs. %f",n,t);

return 0;

24) A program to display the series of first 20 natural number using "while"
or "do.....while" loop.

Using "while" loop->>

Code:

#include<stdio.h>

int main()

int i=1;

while(i<=20)

printf("The series is %d\n ",i);

i=i+1;

return 0;

XVI
Or, using "do......while"

Code:

#include<stdio.h>

int main()

int i=1;

do

printf("The series is %d\n",i);

i=i+1;

}while(i<=20);

return 0;

25) A program to display the odd series from 1 to 50.

Code:

#include<stdio.h>

int main()

int i;

printf("The odd series from 1-50 is : ");

for(i=1;i<=50;i=i+2)

printf("%d , ",i);

return 0;

XVII
26) A program to display the even series upto 30.

Code:

#include<stdio.h>

int main()

int n;

printf("The even series from 1-30 is \n");

for(n=2;n<=30;n=n+2)

printf(", %d",n);

return 0;

27) A program to display the series 4,8,12, ....... ,60

Code:

#include<stdio.h>

int main()

int b;

for(b=4;b<=60;b=b+4)

printf("The series is %d\n",b);

return 0;

28) A program to display the first 20 odd numbers in a raw.

Code:

#include<stdio.h>

XVIII
int main()

int c;

printf("The first 20 odd numbers in a number system is given below ----\n");

for(c=1;c<=40;c=c+2)

printf("\t %d \n",c);

return 0;

29) A program to display the sum of first 10 natural number.

Hint: 1+2+3+4+5+6+7+8+9+10=__. This program find out this result.

Code:

#include<stdio.h>

int main()

int y,sum=0;

printf("The sum of first 10 natural number is : \n");

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

printf("%d+",y);

sum=sum+y;

printf("= %d",sum);

return 0;

XIX
30) A program to print the series decremented by 2 each time starting from
20.

Code:

#include<stdio.h>

int main()

int i;

printf("The series is ");

for(i=20;i>=2;i=i-2)

printf("%d, ",i);

return 0;

31) A program to demonstrate the use of getchar() and putchar()

Here, we import "string.h" header file because it have the declaration


about getchar() and putchar().

"char" is a type of variable which used to store single character and


numbers(do not all numbers)

Code:

#include<stdio.h>

#include<string.h>

int main()

char wds;

printf("Enter a single alphabet/ character : ");

wds=getchar(); //getchar() is used for collect input from user as value of wds

XX
printf("\t Your entered Character is : ");

putchar(wds); /* it is used for display the value of wds in a program. Here,


wds is a user defined char type variable.*/

return 0;

32) A program to introduce with gets() and puts().

Code:

#include<stdio.h>

#include<string.h> // it is also contains declaration about gets() and puts()

int main()

char name[10]; // it can store upto 9 characters which include spaces.

puts("Enter your name : "); // puts() is used as printf()

gets(name); // gets() is used as scanf()

puts("My name is ");

puts(name);

return 0;

33) A program to display your name and address.

Code:

#include<stdio.h>

#include<string.h>

int main()

char name[10];
XXI
char address[20];

puts("Enter your name : ");

gets(name);

puts("Enter your address : ");

gets(address);

puts("\n\tMy name is : ");

puts(name);

puts("\n\tMy address is : ");

puts(address);

return 0;

34) A program to print out the message "C program is easy to learn." for
five times.

Code:

#include<stdio.h>

int main()

int i;

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

printf("C program is easy to learn.\n");

return 0;

35) A program to display data in different variable.

Code:

#include<stdio.h>
XXII
int main()

int a=205;

float b=205;

printf(" The value of a is : %d",a);

printf("\n The value of b is : %f",b);

return 0;

36) A program to find out ASCII code.

ASCII= American Standard Code for Information Technology. When


character is type in the below program, the character itself is recorded in a
numeric value( ASCII code ) and when we displayed that value by using
"%c" the computer automatically displayed that character as per ASCII
code.

The ASCII value of 'A' is 65 , 'B' is 66 and so on 'Z' is 90. The ASCII code of
'a' is 97 , 'b' is 98 and so on 'z' is 122.

Code:

#include<stdio.h>

int main()

char alpha='a';

int i=90;

printf("The entered value of alpha is : %c",alpha);

printf("\n The ASCII code of the value of alpha is : %d",alpha);

printf("\n i= %d",i);

printf("\n i=%c",i);
XXIII
return 0;

37) A program for find out ASCII code of a user entered character.

Code:

#include<stdio.h>

int main()

char var1;

printf(" Enter any single character : ");

scanf("%c",&var1);

printf(" Your entered character is %c",var1);

printf("\n\t ASCII code is %d",var1);

return 0;

38) A program to find out the length of your name.

Code:

#include<stdio.h>

#include<string.h>

int main()

char name[20];

int a;

printf(" Enter your name : ");

gets(name);

XXIV
a=strlen(name); //strlen(); function calculate character.

printf("The length of your name is %d",a);

return 0;

39) A program with using strcat() to joint two string.

Code:

#include<stdio.h>

#include<string.h>

int main()

char s1[25], s2[25];

printf("Enter first string : ");

gets(s1);

printf("Enter second string : ");

gets(s2);

strcat(s1,s2);

puts(s1);

return 0;

40) A program which using strcpy() for copying one string to another.

Code:

#include<stdio.h>

#include<string.h>

int main()

XXV
{

char s1[20],s2[20];

printf("Enter first string : ");

gets(s1);

printf("Enter second string : ");

gets(s2);

strcpy(s2,s1);

printf("The copied string is ");

puts(s2);

return 0;

41) A program for printing multiplication table of 6.

Code:

#include<stdio.h>

int main()

int i=6;

int j;

printf(" The multiplication table of 6 is : \n");

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

printf(" %d * %d= %d\n",i,j,i*j);

return 0;

XXVI
42) A program to display the day name depending on user input using
"if.....else"

Code:

#include<stdio.h>

int main()

int day;

printf("Enter any number between 1 to 7 : ");

scanf("%d",&day);

if(day == 1)

printf("Monday\n");

else if(day == 2)

printf("Tuesday\n");

else if(day == 3)

printf("Wednesday\n");

else if(day == 4)

printf("Thursday\n");

else if(day == 5)

printf("Friday\n");

else if(day == 6)

printf("Saturday\n");

else if(day == 7)

printf("Sunday\n");

else

printf("Invalid Number!!! Enter correct number.");


XXVII
return 0;

43) A program to display the name of day depending on user input using "
switch case ".

Code:

#include<stdio.h>

int main()

int day;

printf("Enter any number between 1 to 7 : ");

scanf("%d",&day);

switch(day)

case 1:

printf("Monday\n");

break;

case 2:

printf("Tuesday\n");

break;

case 3:

printf("Wednesday\n");

break;

case 4:

printf("Thursday\n");

break;
XXVIII
case 5:

printf("Friday\n");

break;

case 6:

printf("Saturday\n");

break;

case 7:

printf("Sunday\n");

break;

default:

printf("Invalid!!! Enter a correct number");

break;

return 0;

44) A program for printing multiplication table of 2-5

Code:

#include<stdio.h>

int main()

int i,j;

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

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

XXIX
printf("%d * %d = %d \n",i,j,i*j);

printf("\n");

return 0;

45) A program to find out the value of (a+b)²

Code:

#include<stdio.h>

int main()

int a,b,ans;

printf("Enter the value of a and b : ");

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

ans=(a+b)*(a+b);

printf("\n\t The result is %d ",ans);

return 0;

46) A program to find out that the entered character is vowel or not.

Code:

#include<stdio.h>

int main()

char vowel;

printf("Enter any single character : ");

XXX
scanf("%c",&vowel);

switch(vowel)

case 'a':

printf("It is a vowel\n");

break;

case 'e':

printf("It is a vowel\n");

break;

case 'i':

printf("It is a vowel\n");

break;

case 'o':

printf("It is a vowel\n");

break;

case 'u':

printf("It is a vowel\n");

break;

default:

printf("It is not a vowel");

break;

return 0;

XXXI
47) A program to check whether a given year is leap year or not.

Code:

#include<stdio.h>

int main()

int year;

printf("Enter any year : ");

scanf("%d",&year);

if(year%4 == 0)

printf("%d, is leap year",year);

else

printf("%d, is not a leap year",year);

return 0;

XXXII

You might also like