Code1:
Program for
printing hello message
#include<conio.h>
#include<stdio.h>
main()
{
Printf(“hello”);
}
Code2:
program for constant’s
#include<stdio.h>
main()
{
const int
num=12;
num++; //cannot modify the constant object
}
Code3:
various datatypes
#include<stdio.h>
Main()
{
int ht,wt;
char ch=’a’;
float=3.14;
}
Code4:
program to declare a variable and initialize it
#include<stdio.h>
Main()
{
int a=0; // a
is declared here
a is
initialized with 0
printf(“%d”,a);
}
Code5:
Addition of two number’s
#include<stdio.h>
main()
{
int a,b,c;
printf(“enter
two number’s”);
scanf(“%d%d”,&a,&b);
c=a+b;
printf(“%d”,c);
}
Code6: code
for addition,subtraction,multiplication,division
#include<stdio.h>
main()
{
int
a,b,sum,add,sub,mul,div;
printf(“enter
a,b values”);
scanf(“%d%d,&a,&b);
sum=a+b;
sub=a-b;
mul=a*b;
div=a/b;
printf(“%d%d%d%d”,sum,sub,mul,div);
}
Code7:program
for simple interest (PTR/100)
#include<stdio.h>
main()
{
int p,t,r,si;
printf(“enter
the values p,t,r values”);
scanf(“%d%d%d”,&p,&t,&r);
si=(p*t*r)/100;
printf(“%d”,si);
}
Code8:
reciprocal of a number
#include<stdio.h>
main()
{
float f,z;
printf(“Enter
a value”);
scanf(“%f”,&f);
z=1/f;
printf(“%f”,z);
}
Code9:
Arithmetic Operator
#include
<stdio.h>
int
main(){
int a=9,b=4,c;
c=a+b;
printf("a+b=%d\n",c);
c=a-b;
printf("a-b=%d\n",c);
c=a*b;
printf("a*b=%d\n",c);
c=a/b;
printf("a/b=%d\n",c);
c=a%b;
printf("Remainder when a divided by
b=%d\n",c);
return 0;
}
}
Code10:Relational
Operator
main()
{
int x = 3;
int y = 4;
printf("Value of %d > %d is %d\n",x,y,x>y);
printf("Value of %d >= %d is %d\n",x,y,x>=y);
printf("Value of %d <= %d is %d\n",x,y,x<=y);
printf("Value of %d < %d is %d\n",x,y,x<y);
}
Code11:Assignment
Operator
main()
{
int a = 21;
int c ;
c = a;
printf("Line 1 - = Operator Example, Value of c = %d\n", c );
c += a; printf("Line 2 - += Operator Example, Value of c = %d\n", c );
c -= a; printf("Line 3 - -= Operator Example, Value of c = %d\n", c );
c *= a; printf("Line 4 - *= Operator Example, Value of c = %d\n", c );
c /= a; printf("Line 5 - /= Operator Example, Value of c = %d\n", c );
}
Code12:
Logical Operator
main()
{
int a = 21;
int b = 10;
int c ;
if ( a && b ) {
printf("Line 6 - Condition is true\n" );
}
if ( a || b )
{
printf("Line 7 - Condition is true\n" );
}
/* Again lets change the value of a and b */
a = 0;
b = 10;
if ( a && b )
{
printf("Line 8 - Condition is true\n" );
}
else
{
printf("Line 8 - Condition is not true\n" );
}
if ( !(a && b) )
{
printf("Line 9 - Condition is true\n" );
}
}
Code13:Bitwise
Operator
main()
{
unsigned int a = 60; /* 60 = 0011 1100 */
unsigned int b = 13; /* 13 = 0000 1101 */
int c = 0;
c = a & b; /* 12 = 0000 1100 */
printf("Line 1 - Value of c is %d\n", c );
c = a | b; /* 61 = 0011 1101 */
printf("Line 2 - Value of c is %d\n", c );
c = a ^ b; /* 49 = 0011 0001 */
printf("Line 3 - Value of c is %d\n", c );
c = ~a; /*-61 = 1100 0011 */
printf("Line 4 - Value of c is %d\n", c );
c = a << 2; /* 240 = 1111 0000 */
printf("Line 5 - Value of c is %d\n", c );
c = a >> 2; /* 15 = 0000 1111 */
printf("Line 6 - Value of c is %d\n", c );
}
Code14:sizeof
#include <stdio.h>
int main(){
int a;
float b;
double c;
char d;
printf("Size of int=%d bytes\n",sizeof(a));
printf("Size of float=%d bytes\n",sizeof(b));
printf("Size of double=%d bytes\n",sizeof(c));
printf("Size of char=%d byte\n",sizeof(d));
}
Code15: Conditional operator
int a=10, b=20, c; c = a > b ? a : b; // if a>b "execute" a, else b
Code16: ASCII value
#include<stdio.h>
Main()
{
Char ch=’a’;
Printf(“%d%c”,ch,ch);
}
Code17: Escape Character’s
#include<stdio.h>
Main()
{
Printf(“a\tb”);
Printf(“a\nb”);
Printf(“a\\b”);
Printf(“a\vb”);
Printf(“a\ab”);
Printf(“a\bb”);
Printf(“a\?b”);
Printf(“a\rb”);
}
Code18: if statement
#include<stdio.h>
main()
{
Int i=14;
If(i>18)
Printf(“eligible
for voting”);
}
Another
example
#include<stdio.h>
main()
{
int a=5,b=5;
if((a-b)==0)
{
printf(“ a
and b are equal”);
}
Sleep(1000);
}
Code 19:
if-else statement
#include<stdio.h>
main()
{
int
a=20,b=10;
if(a>b)
{
Printf(“a is
big”);
}
else
{
printf(“ b is
big”);
}
}
Code 20:
if-elseif ladder
#include<stdio.h>
main()
{
int n=4;
if(n==1)
{
printf(“n is
one”);
elseif(n==2)
{
printf(“n is
two”);
}
elseif(n==3)
{
printf(“n is
three”);
}
elseif(n==4)
{
printf(“n is
four”);
}
else
{
printf(“No
match”);
}
}
Code21:
switch statement
#include<stdio.h>
main()
{
int n =4;
switch(n)
{
case1:
printf(“red”);
break;
case2:
printf(“blue”);
break;
case3:
printf(“orange”);
break;
case4:
printf(“black”);
break;
default:
printf(“No
match”);
}
}
Example
without break:
#include<stdio.h>
main()
{
int n =1;
switch(n)
{
case1:
printf(“red”);
case2:
printf(“blue”);
case3:
printf(“orange”);
case4:
printf(“black”);
default:
printf(“No
match”);
}
}
Code22:while
loop
#include<stdio.h>
main()
{
int i;
while(i<10)
{
Printf(“%d”,i);
i++;
}
}
Code23: do
while loop
#include<stdio.h>
main()
{
int i;
do
{
printf(“%d”,i);
i++;
}while(i<10);
}
Code 24: for loop
#include<stdio.h>
main()
{
for(int
i=0;i<10;i++)
{
printf(“%d”,i);
}
}
Code25: break
#include<stdio.h>
main()
{
for(int
i=0;i<10;i++)
{
printf(“%d”,i);
if(i==5)
break;
}
}
Code26:continue
#include<stdio.h>
main()
{
for(int
i=0;i<10;i++)
{
printf(“%d”,i);
if(i==5)
continue;
}
}
Code27:
(ax+b)/(ax-b)
#include<stdio.h>
Void main()
{
Int a ,x,b,z;
Printf(“Enter
a,x,b values”);
Z=(a*x+b)/(a*x-b);
Printf(“%d”,z);
}
Code 28:
Absolute value of a number
#include<stdio.h>
#include<conio.h>
void main()
{
int num;
printf("\n Enter a Number : ");
scanf("%d",&num);
if(num<0)
{
num= (-1)*num;
}
printf("\n The Abosolute Value is %d",num);
#include<conio.h>
void main()
{
int num;
printf("\n Enter a Number : ");
scanf("%d",&num);
if(num<0)
{
num= (-1)*num;
}
printf("\n The Abosolute Value is %d",num);
}
Code 29: Program for reverse of a number
#include<stdio.h>
int
main(){
int num,r,reverse=0;
printf("Enter any number: ");
scanf("%d",&num);
while(num){
r=num%10;
reverse=reverse*10+r;
num=num/10;
}
printf("Reversed of number: %d",reverse);
return 0;
}
Code
30: swap two numbers
#include<stdio.h>
int main(){
int a=5,b=10;
//process one
a=b+a;
b=a-b;
a=a-b;
printf("a= %d b= %d",a,b);
}
Code:31
Conversion from
uppercase to lower case using c program
#include<stdio.h>
#include<string.h>
#include<string.h>
int main(){
char str[20];
int i;
printf("Enter any string->");
scanf("%s",&str);
printf("The string is->%s",str);
for(i=0;i<=strlen(str);i++){
if(str[i]>=65&&str[i]<=90)
str[i]=str[i]+32;
}
printf("\nThe string in uppercase is->%s",str);
return 0;
}
char str[20];
int i;
printf("Enter any string->");
scanf("%s",&str);
printf("The string is->%s",str);
for(i=0;i<=strlen(str);i++){
if(str[i]>=65&&str[i]<=90)
str[i]=str[i]+32;
}
printf("\nThe string in uppercase is->%s",str);
return 0;
}
ASCII value of 'A' is 65 while 'a' is 97.
Difference between them is 97 – 65 = 32
So if we will add 32 in the ASCII value of 'A' then
it will be 'a' and if will we subtract 32 in ASCII value of 'a' it will be 'A'.
It is true for all alphabets.
In general rule:
Upper case character = Lower case character – 32
Lower case character = Upper case character + 32
Code 32: Even/Odd number
#include<stdio.h>
Main()
{
Int n;
Printf(“Enter n value”);
scanf(“%d”,n)
if(n%2==0)
{
Printf(“even”);
}
Else
{
Printf(“Odd”);
}
}
Code 33:Multiplication table
Main()
{
Int I,n,c=0;
Printf(“enter any number”);
Scanf(“%d”,&n);
I=1;
while(i<=10)
{
Printf(“%d * %d= %d\n”,n,I,n*i);
I++;
}
}
Code 34:Program to print even number till range
Main()
{
Int I,n;
Printf(“Enter nay number”);
Scanf(“%d”,&n);
For(i=2;i<=n;i+=2)
Printf(“%d”,i);
}
Code 35:leap year or not
Main()
{
Int year;
Printf(“Enter year”);
Scanf(“%d”,&year);
If(year%==0)
Printf(“Leap year”);
Else
Printf(“Not a Leap year”);
}
Code 36:
Program to check vowel or not
#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);
else
printf("%c is not a vowel.\n", ch);
}
Code 37:
program for sum.avg of two number’s
#include<stdio.h>
main()
{
int a,b,sum,diff,avg;
printf(“\nEnter two integers: “);
scanf(“%d%d”,&a,&b);
sum=a+b;
diff=a-b;
avg=(a+b)/2;
printf(“\nSum= %d\nDifference= %d\nAverage= %d\n”,sum,diff,avg);
}
main()
{
int a,b,sum,diff,avg;
printf(“\nEnter two integers: “);
scanf(“%d%d”,&a,&b);
sum=a+b;
diff=a-b;
avg=(a+b)/2;
printf(“\nSum= %d\nDifference= %d\nAverage= %d\n”,sum,diff,avg);
}
Code 38: sum
of digits of a number
#include<stdio.h>main()
{
int num,i,remainder,sum=0;
printf(“\nEnter the number: “);
scanf(“%d”,&num);
while(num>0)
{
remainder=num%10;
sum+=remainder;
num/=10;
}
printf(“\nSum of digits: %d\n\n”,sum);
}
Code 39: To
find area and circumference of circle
#include<stdio.h>
int main()
{
int rad;
float PI=3.14,area,ci;
printf("nEnter radius of circle: ");
scanf("%d",&rad);
area = PI * rad * rad;
printf("nArea of circle : %f ",area);
ci = 2 * PI * rad;
printf("nCircumference : %f ",ci);
}
Code40 : strlen()
#include
<stdio.h>
#include
<conio.h>
int
main()
{
char str[30]="C
PROGRAMMING";
clrscr();
printf("\n\nstr : %s\n\n",str);
printf("\nLength of the string,
strlen(str) is %d\n",strlen(str));
getch();
return 0;
}
Code41 : strlwr()
#include
<stdio.h>
#include
<conio.h>
int
main()
{
char str[30]="C
PROGRAMMING";
clrscr();
printf("\n\nstr : %s\n\n",str);
printf("strlwr(str) :
%s\n",strlwr(str));
getch();
return 0;
}
Code42 : strupr()
#include
<stdio.h>
#include
<conio.h>
int
main()
{
char str[30]="C
Programming";
clrscr();
printf("\n\nstr : %s\n\n",str);
printf("strupr(str) :
%s\n",strupr(str));
getch();
return 0;
}
Code42: strcat()
#include
<stdio.h>
#include
<conio.h>
int
main()
{
char str1[25]="I LIKE ";
char str2[15]="C
PROGRAMMIG";
clrscr();
printf("\n\nstr1 : %s\t\t\tstr2 :
%s\n\n",str1,str2);
printf("\n\nstrcat(str1,str2) :
%s\n\n",strcat(str1,str2));
printf("\n\nstr1 : %s\t\tstr2 :
%s\n\n",str1,str2);
getch();
return 0;
}
Code44 : strncat()
#include
<stdio.h>
#include
<conio.h>
int
main()
{
char str1[25]="I LIKE ";
char str2[13]="C
PROGRAMMIG";
clrscr();
printf("\n\nBEFORE:\n\nstr1 :
%s\t\t\tstr2 : %s\n\n",str1,str2);
printf("\n\nstrncat(str1,str2,5) :
%s\n\n",strncat(str1,str2,5));
printf("\n\nAFTER:\n\nstr1 :
%s\t\tstr2 : %s\n\n",str1,str2);
getch();
return 0;
}
Code45: strcpy()
#include
<stdio.h>
#include
<conio.h>
int main()
{
char str1[15]="I LIKE ";
char str2[15]="C PROGRAMMIG";
clrscr();
printf("\n\nBEFORE:\n\nstr1 :
%s\t\t\tstr2 : %s\n\n",str1,str2);
strcpy(str1,str2);
printf("\n\nAFTER:\n\nstr1 :
%s\t\tstr2 : %s\n\n",str1,str2);
getch();
return 0;
}
Code46: strncpy()
#include
<stdio.h>
#include
<conio.h>
int main()
{
char str1[25]="I LIKE ";
char str2[15]="C PROGRAMMIG";
clrscr();
printf("\n\nBEFORE:\n\nstr1 :
%s\t\t\tstr2 : %s\n\n",str1,str2);
strncpy(str1,str2,4);
printf("\n\nAFTER:\n\nstr1 :
%s\t\tstr2 : %s\n\n",str1,str2);
getch();
return 0;
}
Code47: strcmp()
#include
<stdio.h>
#include
<conio.h>
int
main()
{
char str1[]="I LIKE";
int i;
clrscr();
i=strcmp(str1,str2);
if(i==0)
printf("\n\nstr1 and str2 are
identical");
else if(i<0)
printf("\n\nstr1< str2");
else
printf("\n\nstr1< str2");
getch();
return 0;
}
0 comments:
Post a Comment