Professional Documents
Culture Documents
#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);
//display as such
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;
//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();
}
////////////////////////////////
#include <stdio.h>
int main()
{
double firstNumber, secondNumber, temporaryVariable;
temporaryVariable = firstNumber;
firstNumber = secondNumber;
secondNumber = temporaryVariable;
return 0;
}
//////////////////////////////////////////
BAsic If statement
///////////////////////////////////////////////
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();
}
/////////////////////////////////////////////////////////
#include <stdio.h>
int main()
{
double n1, n2, n3;
return 0;
}
////////////////////////////////
/////////////////////////////////
with break
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();
}
/////////////////////////////////////////////////
#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();
}
////////////////////////////////////////////////
# include <stdio.h>
int main()
{
jump:
average=sum/(i-1);
printf("Sum = %.2f\n", sum);
printf("Average = %.2f", average);
return 0;
}
/////////////////////////////////////////////////////////////////
Switch case
}
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();
}
///////////////////////////////////////////
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;
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
//////////////////////////
//void print();
int main()
{
{
for(int i=1;i<35;i++)
printf("*");
printf("\n");
////////////////////////////////////
////////////////////////
}
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();
}
//////////////////////////////////
/////////////////////////////
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();
}
/////////////////////////////////////
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;
}
////////////////////////////////////////////
getch();
}
//////////////////////////////////
break;
printf("Good \n");
}
printf("i am outside");
getch();
}
//////////////////////////////////
getch();
}
////////////////////////////////
get char()
#include<stdio.h>
#include<conio.h>
main()
{
char c;
printf("enter a character");
c=getchar();
printf("c = %c ",c);
getch();
}
/////////////////////
int main()
{
int n, a = 0, b = 1, c, i;
c = a + b;
a = b;
b = c;
printf("\n%d",c);
}
getch();
return 0;
}
////////////////////////////////
Find remainder and quotient
#include <stdio.h>
int main(){
// Computes quotient
quotient = dividend/divisor;
// Computes remainder
remainder = dividend%divisor;
printf("Quotient = %d\n",quotient);
printf("Remainder = %d",remainder);
return 0;
}
//////////////////////////////////////////
while(n!=0)
{
n /= 10;
++count;
}
// n = n/10
#include <stdio.h>
int main()
{
int n, reversedNumber = 0, remainder;
while(n != 0)
{
remainder = n%10;
reversedNumber = reversedNumber*10 + remainder;
n /= 10;
}
return 0;
}
///////////////////////////////
while (exponent != 0)
{
result *= base;
--exponent;
}
return 0;
}
//////////////////////////////////
#include <stdio.h>
int main()
{
int number, originalNumber, remainder, result = 0;
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
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;
return 0;
}
Output
++a = 11
--b = 99
++c = 11.500000
++d = 99.500000
/////////////////////////////////////////////////////////////
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
///////////////////////////////////////
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
/////////////////////////////////////////////
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;
return 0;
}
Output
#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
}
/////////////////