You are on page 1of 49

Manipulator

#include<conio.h>
#include<stdio.h>
main()
{
int a1= 97,a;
float b= 2311.126;
char s[40]="I am going to attend lecture";
char c='x';
a=(int)b;
printf("a= %d",a);

printf("\na1= %c",a1);

printf("\n *************formatting of int***********");


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

//display as such

printf("\n\na= %5d",a); // _2311


printf("\n\na= %.5d",a); //02311
printf("\n\na= %05d",a); //02311
printf("\n\na= %-5d",a); // display as such
printf("\n\na= %-05d",a); //display as such
printf("\n\na= %-.5d",a); // 02311

printf("\n *************formatting of float***********");


printf("\n\nb= %f",b); // as such

printf("\n\nb= %3f",b); // as such


printf("\n\nb= %.2f",b); //after decimal 2 digit - roundup
printf("\n\nb= %.4f",a); //after decimal 4 digit-ru
printf("\n\nb= %10.4f",a); //total 10 but after digit 4 and
//if no. of digits are less then 10 then give space

printf("\n\nbg= %g",b); // as such it display max 6 digits


printf("\n\nbg= %.2g",b);// total 2 and shift decimal too after 1st digit
printf("\n\nbg= %.4g",a);// total 4 and will not shift decimal after 1st digit-round
up

printf("\n\nbe= %e",b); //shift dot/decimal after 1st


printf("\n\nbe= %.2e",b);// shift dot after 1st and after decml 2 digits will be
there
printf("\n\nbe= %.4e",a); //shift dot after 1st and after decml 4 digits will be
there

printf("\n *************formatting of char***********");


printf("\n\nc= %c",c);//x
printf("\n\nc= %5c",c);//_ _ _ _x
printf("\n\nc= %.4c",c);// display as such
printf("\n\nc= %05c",c);//0000x
printf("\n\nc= %-5c",c);// display as such

printf("\n *************formatting of string***********");


printf("\n\ncs= %s",s);
printf("\n\ncs= %5s",s);//min 5 char

printf("\n\ncs= %.5s",s);//max 5
printf("\n\ncs= %.50s",s);//as such
printf("\n\ncs= %-.50s",s);//as such
printf("\n\ncs= %50s",s);// space_ _ _ - - _ i am going to attend lecture

getch();

Basic Programs
Print Hello on screen

#include<conio.h>
#include<stdio.h>
main()
{

printf("hello\n");
printf("hello");

getche();
}

///////////////////////////////////////////
Sum of two no

#include<stdio.h>
#include<conio.h>
main()
{
int a=10,b=2,sum;
sum=a+b;
printf("sum is %d",sum);
printf("\nthe two numbers are A =%d and B=%d",a,b);

getch();
}
///////////////////////////////
Temp conversion

#include<stdio.h>
#include<conio.h>
int main()
{
float c,f;
printf("Enter the temperature value in celcius\n");
scanf("%g",&c);
f=(1.8*c)+32;

printf("converted value into fahrenheit is=%g",f);

//getch();
}
//////////////////////////////////////////

Sizeof()

#include<stdio.h>
#include<conio.h>
int main()
{
double x[ ] = {1,2,3,4,5,6,7};
printf("%d",sizeof(x));

getch();
}

/////////////////////////////////////

logical operators

#include<stdio.h>

#include<conio.h>
int main()
{
printf("0 && 1 = %d\n", 0&&1);
printf("0 || 1 = %d\n", 0||1);
printf("!1 = %d\n", !1);

getch();
}

//////////////////////////////////////

ASCII value

#include<stdio.h>
#include<conio.h>
//using namespace std;
int main()
{
int a;
char c;
printf("Enter any character value:");
scanf("%c",&c);
a=c;
printf("ASCII code is %d",a);

getch();
}
///////////////////////////////////////////////
Escape sequences

#include<stdio.h>
#include<conio.h>
int main()
{
int a=1,b=a+1,c=b+1,d=c+1;
printf("\tA=%d\nB=%d\tC=%d",a,b,c);
printf("\n\a\a\a\a\a*** \b D=%d**",d);
printf("\n*********");
printf("\tA=%d B=%d",a,b);
getch();
}

////////////////////////////////

Program to Swap Numbers Using Temporary Variable

#include <stdio.h>

int main()
{
double firstNumber, secondNumber, temporaryVariable;

printf("Enter first number: ");


scanf("%lf", &firstNumber);

printf("Enter second number: ");


scanf("%lf",&secondNumber);

temporaryVariable = firstNumber;

firstNumber = secondNumber;

secondNumber = temporaryVariable;

printf("\nAfter swapping, firstNumber = %.2lf\n", firstNumber);


printf("After swapping, secondNumber = %.2lf", secondNumber);

return 0;
}
//////////////////////////////////////////
BAsic If statement

//BASIC PROGRAM ON IF STATEMENT


#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,c;
printf("enter a number=");
scanf("%d",&a);
if(a<10)
//{
printf("a is less than 10");
// printf("\n number entered is:%d",a);
// }
//printf("i am outside IF");
getch();
}

///////////////////////////////////////////////

//PROGRAM TO FIND WHETER A IS EQAUL TO B


#include<stdio.h>
#include<conio.h>
int main()
{
int a,b;

printf("enter 2 numbers=");
scanf("%d %d",&a,&b);
if(a==b)
{
printf("a is equal to b");
}
getch();
}

//////////////////////////////////////////

vote casting

#include<stdio.h>
#include<conio.h>
int main()
{
int a;
printf("enter the age of candidate in years:");
scanf("%d",&a);
if(a>=18)
{
printf("*****Eligible for voting*****");
}

else
{
printf("NOT ELIGIBLE");
}

getch();
}

//////////////////////////////////////////
even or odd
//TO FIND NUMBER IS ODD OR EVEN-USING IF ELSE STATEMENT
#include<conio.h>
#include<iostream.h>
int main()
{
int n,c;
printf("\n Enter a number ");
scanf("%d",&n);
c=n%2;
if(c==0)
printf("\n Number is even");
else
printf("\n Number is odd");
getch();
}

/////////////////////////////////////////////////////////

Fins largest of three no

#include <stdio.h>
int main()
{
double n1, n2, n3;

printf("Enter three numbers: ");


scanf("%lf %lf %lf", &n1, &n2, &n3);

if( n1>=n2 && n1>=n3 )


printf("%.2f is the largest number.", n1);

if( n2>=n1 && n2>=n3 )


printf("%.2f is the largest number.", n2);

if( n3>=n1 && n3>=n2 )


printf("%.2f is the largest number.", n3);

return 0;
}
////////////////////////////////

switch without break

//PROGRAM TO SHOW IMPORTANCE OF BREAK STATEMENT


#include<conio.h>
#include<stdio.h>
main()
{
int i=2;
switch(i)
{
case 1:printf("Its CASE 1...\n");
case 2:
printf("Its CASE 2...\n");
case 3:printf("its CASE 3...");
default :
printf("/n I am Default");
}
getch();
}

/////////////////////////////////

with break

//PROGRAM TO SHOW IMPORTANCE OF BREAK STATEMENT


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

main()
{
int i=1;
switch(i)
{
case 1:printf("Its CASE 1...\n");
break;
case 2:
printf("Its CASE 2...\n");
break;
case 3:printf("its CASE 3...");
break;
default :
printf("/n I am Default");
}
getch();
}

/////////////////////////////////////////////////

Goto statement even or odd

#include<stdio.h>
#include<conio.h>
int main()
{

int a;
printf("Enter any number");
scanf("%d",&a);
if(a%2==0)
{
goto even;
// printf("number is even");
}
else
{
goto odd;
// printf("number is odd");
}
even:
printf("number is even\n");
getch();
return;
odd:
printf("number is odd");

getch();
}

////////////////////////////////////////////////

// Program to calculate the sum and average of maximum of 5 numbers


// If user enters negative number, the sum and average of previously entered
positive number is displayed

# include <stdio.h>

int main()
{

const int maxInput = 5;


int i;
double number, average, sum=0.0;

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


{
printf("%d. Enter a number: ", i);
scanf("%lf",&number);

// If user enters negative number, flow of program moves to label jump


if(number < 0.0)
goto jump;

sum += number; // sum = sum+number;


}

jump:

average=sum/(i-1);
printf("Sum = %.2f\n", sum);
printf("Average = %.2f", average);

return 0;
}
/////////////////////////////////////////////////////////////////
Switch case

//PROGRAM TO SHOW USE OF SWITCH CASE


#include<stdio.h>
#include<conio.h>
int main()
{
char m;
printf("enter a character=");
scanf("%c",&m);
switch(m)
{
case 'a': printf("case is a");
break;
case 'b': printf("case is b");
break;

case 'e':printf("case is e");


break;
// default:
//

printf("case does not exist");

}
getch();
}

////////////////////////////////////////
Switch case calcultor

#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,c,s;
printf(" Enter any of your choice out of the following:\n");
printf("To subtracrt press 1\n");
printf("To add press 2\n");
printf("To multiply press 3\n");
printf("To divide press 4\n");
scanf("%d",&s) ;
printf("Enter any two numbers:\n");
scanf("%d %d",&a,&b);
switch(s)
{

case 1:
{
c=a+b;
printf("After adding these numbers sum is:%d",c);
break;
}
case 2:
{
c=a-b;
printf("\nAfter Subtracting these numbers result is:%d",c);
break;
}
case 3:
{
c=a*b;
printf("\nAfter Multiplying result for these numbers is:%d",c);
break;
}
case 4:
{
c=a/b;
printf("\nAfter dividing these numbers result is:%d",c);
break;
}
case 5:
{

c=a%b;
printf("\nAfter Dividing these numbers remainder is:%d",c);
break;
}
}
getch();
}

///////////////////////////////////////////

//WITH PARAMETER WITH RETURN VALUE


#include<stdio.h>
#include<conio.h>
int sum(int,int); //function declaration
int main()
{
int a,b;

printf("enter two numbers");


scanf("%d%d",&a,&b);
int c=sum(a,b); //function call
printf("sum is= %d",c);
getch();

return 0;
}
int sum(int a,int b) //function defination
{
int c;
c=a+b;
return(c);
//return(z);
}

//////////////////////////////////////////
//WITH PARAMETER WITH NO RETURN VALUE
//WITH PARAMETER AND NO RETURN
#include<stdio.h>
#include<conio.h>
void sum(int,int);
int main()
{
int a,b;

printf("enter two numbers");


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

return 0;
}
void sum(int a,int b)
{
int z;
z=a+b;
printf("sum is = %d",z);
}

///////////////////////////////////////////
no paramenters with retuen type

#include<conio.h>
#include<stdio.h>
int sum();

//function declaration

int main()
{

//int a=10,b=20;
int c,z;

c=sum();

//actual arguments

printf("sum is %d",z);
getch();
}
int sum()

//formal arguments

{
int x=10,y=30;
int z;
z=x+y;
return(z);

//*return value

//////////////////////////

//NO RETURN NO PARAMETER


#include<stdio.h>
#include<conio.h>
void print(); //function declaration

//void print();
int main()
{

print(); //function call


printf("no parameter and no return type \n");
print();
getch();
return 0;
}
void print() //called function

{
for(int i=1;i<35;i++)
printf("*");
printf("\n");

////////////////////////////////////

//PASS BY VALUE EXAMPLE FOR SWAPPING


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

void swap(int,int);//FUNCTION DECLARATION


main()
{
int a,b;

printf("enter two numbers=");


scanf("%d%d",&a,&b);
printf("\n before calling(in main) a= %d and b =%d",a,b);
swap(a,b); //function calling with actual arguments
printf("\n after calling (in main) a= %d and b =%d",a,b);
getch();
return 0;
}

void swap( int x,int y) //function defination


{
int z;
z=x;
x=y;
y=z;
printf("\n after modification (in function) x= %d and y =%d",x,y);
}

////////////////////////

//PASS BY ADDRESS(BY POINTER) EXAMPLE FOR SWAPPING


#include<conio.h>
#include<stdio.h>
void swap(int *,int *);
main()
{
int a,b;

printf("enter two numbers=");


scanf("%d%d",&a,&b);
printf("\n before calling(in main) a= %d and b =%d",a,b);
swap(&a,&b); //function calling with actual arguments
printf("\n after calling (in main) a= %d and b =%d",a,b);
getch();
return 0;

}
void swap( int *x,int *y)
{
int z;
z=*x;
*x=*y;
*y=z;
printf("\n after modification (in function) x= %d and y =%d",*x,*y);
}
////////////////////////////////////
//print first n natural numbers
#include<stdio.h>
#include<conio.h>
int main()
{
int i=1;
int n;
printf("enter the value of n");
scanf("%d",&n);
while (i<=n)
{
printf("\n%d", i);
//n=n+1;//n++;
i++;
}
getch();

}
//////////////////////////////////

//TO CALCULATE SUM OF 10 NUMBERS


#include<stdio.h>
#include<conio.h>
int main()
{
int i=1,sum=0;
printf("Numbers are\n");
while(i<=10)
{
printf(" %d\n",i);
sum=sum+i;
i++;
}
printf("Sum is:%d",sum);
getch();
}

/////////////////////////////

//CALCULATE FACTORIAL OF A NUMBER


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

int main()
{
int i,fact=1;
printf("Enter any number\n");
scanf("%d",&i);

while(i>0)
{
printf("%d*",i);
fact=fact*i;
i--;

}
printf("=%d",fact);
getch();
}

/////////////////////////////////////

//PROGRAM TO SHOW WHILE LOOP IS ENTRY CONTROLED LOOP


#include<stdio.h>
#include<conio.h>
int main()
{
int x=15;

while(x!=0)
scanf("%d",&x);

getch();
}
//////////////////////////////////

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

int main ()
{
/* local variable definition */
int a = 20;

/* do loop execution */
do
{
printf("value of a: %d\n", a);
a = a + 1;
}while( a < 20 );
getch();
return 0;
}
////////////////////////////////////////////

//PROGRAM TO SHOW FUNCTIONING OF FOR LOOP


#include<stdio.h>
#include<conio.h>
int main()
{
int i;
for(i=1;i<=15;i++)
{
printf(" %d ",i);
}

getch();
}
//////////////////////////////////

//WORKING OF FOR LOOP


#include<stdio.h>
#include<conio.h>
int main()
{
int count =1;
int total = 0;
int num;
for(int i=1;i<=4;i++)
{

printf("\nEnter a number: ");


scanf("%d",&num);
total = total + num;
printf( "The total is now %d ", total);
}
getch();
}
/////////////////////////
//PROGRAM TO SHOW WORKING OF CONTINUE
#include<stdio.h>
#include<conio.h>
int main ( )
{
int k;
for ( k= -5; k < 25; k= k+5)
{
printf("\nk=%d",k);

printf("Good Morning \n");


continue;
//

break;

printf("Good \n");
}
printf("i am outside");

getch();
}
//////////////////////////////////

Print square of numbers


#include<stdio.h>
#include<conio.h>
int main()
{
int i;
for(i=1;i<=5;i++)
{
printf("\n\nNumber:%d",i);
printf("\nIts Square:%d",i*i);
}

getch();
}

////////////////////////////////
get char()

#include<stdio.h>
#include<conio.h>
main()
{

char c;
printf("enter a character");
c=getchar();
printf("c = %c ",c);
getch();
}
/////////////////////

/* Fibonacci Series c language */


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

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

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


scanf("%d",&n);

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


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

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


{

c = a + b;
a = b;
b = c;

printf("\n%d",c);
}
getch();
return 0;
}
////////////////////////////////
Find remainder and quotient
#include <stdio.h>
int main(){

int dividend, divisor, quotient, remainder;

printf("Enter dividend: ");


scanf("%d", &dividend);

printf("Enter divisor: ");


scanf("%d", &divisor);

// Computes quotient
quotient = dividend/divisor;

// Computes remainder

remainder = dividend%divisor;

printf("Quotient = %d\n",quotient);
printf("Remainder = %d",remainder);

return 0;
}

//////////////////////////////////////////

Count the digits in number


#include <stdio.h>
int main()
{
long long n;
int count=0;

printf("Enter an integer: ");


scanf("%lld", &n);

while(n!=0)
{
n /= 10;
++count;
}

// n = n/10

printf("Number of digits: %d",count);


}
/////////////////////////////
Reverse of a number

#include <stdio.h>
int main()
{
int n, reversedNumber = 0, remainder;

printf("Enter an integer: ");


scanf("%d", &n);

while(n != 0)
{
remainder = n%10;
reversedNumber = reversedNumber*10 + remainder;
n /= 10;
}

printf("Reversed Number = %d",reversedNumber);

return 0;
}

///////////////////////////////

/* C program to check whether a number is palindrome or not */


#include<conio.h>
#include <stdio.h>
int main()
{
int n, reverse=0, rem,temp;
printf("Enter an integer: ");
scanf("%d", &n);
temp=n;
while(temp!=0)
{
rem=temp%10;
reverse=reverse*10+rem;
temp/=10;
}
/* Checking if number entered by user and it's reverse number is equal. */
if(reverse==n)
printf("%d is a palindrome.",n);
else
printf("%d is not a palindrome.",n);
getch();
return 0;
}
//////////////////////////////////////////////

Find the power of number


#include <stdio.h>
int main()
{
int base, exponent;

long long result = 1;

printf("Enter a base number: ");


scanf("%d", &base);
printf("Enter an exponent: 4");
scanf("%d", &exponent);

while (exponent != 0)
{
result *= base;
--exponent;
}

printf("Answer = %lld", result);

return 0;
}
//////////////////////////////////

Check Armstrong Number of three digits


In case of an Armstrong number of 3 digits, the sum of cubes of each digits is equal
to the number itself. For example:

153 = 1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number.

#include <stdio.h>
int main()
{
int number, originalNumber, remainder, result = 0;

printf("Enter a three digit integer: ");


scanf("%d", &number);

originalNumber = number;

while (originalNumber != 0)
{
remainder = originalNumber%10;
result += remainder*remainder*remainder;
originalNumber /= 10;
}

if(result == number)
printf("%d is an Armstrong number.",number);
else
printf("%d is not an Armstrong number.",number);

return 0;
}

Output

Enter a three digit integer: 371


371 is an Armstrong number.

// C Program to demonstrate the working of arithmetic operators


#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;
}
////////////////////////////////////////////////////////
Output

a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when a divided by b=1
///////////////////////////////////////////////
// C Program to demonstrate the working of increment and decrement operators
#include <stdio.h>
int main()
{
int a = 10, b = 100;
float c = 10.5, d = 100.5;

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

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

printf("++c = %f \n", ++c);

printf("--d = %f \n", --d);

return 0;
}

Output

++a = 11
--b = 99
++c = 11.500000
++d = 99.500000
/////////////////////////////////////////////////////////////

// C Program to demonstrate the working of assignment operators


#include <stdio.h>
int main()
{
int a = 5, c;

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

c += a; // c = c+a
printf("c = %d \n", c);

c -= a; // c = c-a
printf("c = %d \n", c);

c *= a; // c = c*a
printf("c = %d \n", c);

c /= a; // c = c/a
printf("c = %d \n", c);

c %= a; // c = c%a
printf("c = %d \n", c);

return 0;
}

Output

c=5
c = 10
c=5
c = 25
c=5
c=0

///////////////////////////////////////

// C Program to demonstrate the working of arithmetic operators


#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10;

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


printf("%d == %d = %d \n", a, c, a == c); // false

printf("%d > %d = %d \n", a, b, a > b); //false


printf("%d > %d = %d \n", a, c, a > c); //false

printf("%d < %d = %d \n", a, b, a < b); //false


printf("%d < %d = %d \n", a, c, a < c); //true

printf("%d != %d = %d \n", a, b, a != b); //false


printf("%d != %d = %d \n", a, c, a != c); //true

printf("%d >= %d = %d \n", a, b, a >= b); //true


printf("%d >= %d = %d \n", a, c, a >= c); //false

printf("%d <= %d = %d \n", a, b, a <= b); //true


printf("%d <= %d = %d \n", a, c, a <= c); //true

return 0;

Output

5 == 5 = 1
5 == 10 = 0
5>5=0
5 > 10 = 0
5<5=0
5 < 10 = 1
5 != 5 = 0
5 != 10 = 1
5 >= 5 = 1
5 >= 10 = 0
5 <= 5 = 1
5 <= 10 = 1
/////////////////////////////////////////////

// C Program to demonstrate the working of logical operators

#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;

result = (a = b) && (c > b);


printf("(a = b) && (c > b) equals to %d \n", result);

result = (a = b) && (c < b);


printf("(a = b) && (c < b) equals to %d \n", result);

result = (a = b) || (c < b);


printf("(a = b) || (c < b) equals to %d \n", result);

result = (a != b) || (c < b);


printf("(a != b) || (c < b) equals to %d \n", result);

result = !(a != b);


printf("!(a == b) equals to %d \n", result);

result = !(a == b);


printf("!(a == b) equals to %d \n", result);

return 0;
}

Output

(a = b) && (c > b) equals to 1


(a = b) && (c < b) equals to 0
(a = b) || (c < b) equals to 1
(a != b) || (c < b) equals to 0
!(a != b) equals to 1
!(a == b) equals to 0
///////////////////////////////////////////////

#include <stdio.h>
int main()
{
int a, e[10];
float b;
double c;
char d;
printf("Size of int=%lu bytes\n",sizeof(a));
printf("Size of float=%lu bytes\n",sizeof(b));
printf("Size of double=%lu bytes\n",sizeof(c));
printf("Size of char=%lu byte\n",sizeof(d));
printf("Size of integer type array having 10 elements = %lu bytes\n", sizeof(e));
return 0;
}

Output

Size of int = 4 bytes


Size of float = 4 bytes
Size of double = 8 bytes
Size of char = 1 byte
Size of integer type array having 10 elements = 40 bytes
//////////////////////////////////////
C conditional Operator
#include <stdio.h>
int main()
{
int d;
printf("enter any value:");
scanf("%d",&d);
(d>=10)?printf("bigger than 10"):printf("smallar than 10");
return(0);

}
/////////////////

You might also like