6
Day 1 Examples #include <stdio.h> int main( ) { printf( "Welcome\nto\nC!\n" ); return 0; }//output is: Welcome To c #include<stdio.h> int main() { printf("\n\"It is a wise father that knows his own child.\" Shakespeare"); return 0; }// It is a wise father that knows his own child." Shakespeare #include<stdio.h> int main() { printf("\nab"); printf("\bsi"); printf("\rha"); }//hai #include<stdio.h> int main() { int n=5; printf("n+=5 %d \n",n+=5); printf("n-=5 %d \n",n-=5); printf("n*=5 %d \n",n*=5); printf("n/=5 %d \n",n/=5); printf("n%%=5 %d \n",n%=5); }//10 5 25 5 0 #include<stdio.h> int main() { int a; int b=4;

Day 1 Examples

Embed Size (px)

DESCRIPTION

rg th

Citation preview

Page 1: Day 1 Examples

Day 1 Examples#include <stdio.h>int main( ){ printf( "Welcome\nto\nC!\n" ); return 0;}//output is:WelcomeToc

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

printf("\n\"It is a wise father that knows his own child.\" Shakespeare"); return 0;}//It is a wise father that knows his own child." Shakespeare

#include<stdio.h>int main(){printf("\nab");printf("\bsi");printf("\rha");}//hai

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

int n=5;printf("n+=5 %d \n",n+=5);printf("n-=5 %d \n",n-=5);printf("n*=5 %d \n",n*=5);printf("n/=5 %d \n",n/=5);printf("n%%=5 %d \n",n%=5);

}//10 5 25 5 0

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

int a;int b=4;int c=8;int d=2;int e=4;int f=2;a=b+c/d+e*f;printf("a=%d\n",a);a=(b+c)/d+e*f;

Page 2: Day 1 Examples

printf("a=%d\n",a);a=b+c/((d+e)*f);printf("a=%d\n",a);

}//a=16A=14A=4

#include<stdio.h>main(){ printf("Character takes %d byte in memory\n", sizeof(char)); printf("Integer takes %d bytes in memory\n", sizeof(int)); printf("Float takes %d bytes in memory\n", sizeof(float)); printf("Long takes %d bytes in memory\n", sizeof(long)); printf("Double takes %d bytes in memory\n", sizeof(double)); printf("Long Double takes %d bytes in memory\n", sizeof(long double));}

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

double d=1/2.0-1/2;printf("%.2lf",d);

}//0.50

#include<stdio.h>main(){ printf("Sign of the result of division operator:\n"); printf("%d, %d\n",4/3,-4/3); printf("%d, %d\n",4/-3,-4/-3); printf("%d, %d\n",4%3,-4%3); printf("%d, %d\n",4%-3,-4%-3); printf("%d, %d\n",4%3,-4%3); printf("%d, %d\n",4%-3,-4%-3);}//#include<stdio.h>main(){ int i=0,j=1,k=2,l; l=++i&&++j&&++k; printf("%d %d %d %d\n",i ,j,k,l); i=0,j=1,k=2; l=i++&&j++&&k++; printf("%d %d %d %d\n",i ,j,k,l); i=0,j=1,k=2; l=i&&j++&&k++; printf("%d %d %d %d\n",i ,j,k,l);}

Page 3: Day 1 Examples

#include<stdio.h>main(){ int i=0,j=1,k=2,l; l=++i||++j||++k; printf("%d %d %d %d\n",i ,j,k,l); i=0,j=1,k=2; l=i++||j++||k++; printf("%d %d %d %d\n",i ,j,k,l); i=0,j=1,k=2; l=i&&j||k++; printf("%d %d %d %d\n",i ,j,k,l);}

//Use of comma operator#include<stdio.h>main(){ int a,b; a=1, 2, 3, 4, 5; b=(1, 2, 3, 4, 5); printf("Resultant values of a and b are:\n"); printf("%d %d",a ,b); }//1 5#include<stdio.h>main(){int a=5, b=10;if(a>10 && a>b) printf("a is greater than 10"); printf("a is greater than b");}//a is greater than b

#include<stdio.h>main(){int a=10, b=20;if(a==b);printf("a is not equal to b");}//a is not equal to b

//Leap year#include<stdio.h>main(){int year;printf(“Enter the year\t”);scanf(“%d”,&year);if(((year%4==0) && (year%100!=0)) || (year%400==0)) printf(“%d is a leap year”, year);

Page 4: Day 1 Examples

else printf(“%d is not a leap year”, year);}//Roots of a quadratic equation#include<stdio.h>#include<math.h>main(){int a, b, c, d;float r1,r2;int num;printf("Enter the coefficients a, b and c\t");scanf("%d %d %d", &a, &b, &c);d=b*b-4*a*c;if(d>0){ r1=(-b+sqrt(d))/(2*a); r2=(-b-sqrt(d))/(2*a); printf("Roots are real and unequal\n"); printf("Roots are: %.2f %.2f",r1,r2);}else if(d==0){ r1= -b/(2*a); printf("Roots are real and equal\n"); printf("Roots are: %.2f %.2f",r1,r1);}else printf("No real roots, roots are imaginary");}

#include<stdio.h>main(){

int a=23,b=12,c=10,d;d=c+2=b=a;printf("%d %d %d %d\n",a,b,c,d);

}

#include<stdio.h>main(){

int a=2,b=3,c=10,d;d=a<b>c;printf("%d %d %d %d\n",a,b,c,d);

}//2 3 10 0

#include<stdio.h>main(){

int a=2,b=3,c=10;

Page 5: Day 1 Examples

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

}

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

int x,y,z;x=2+3-4+5-(6-7);

y=2*33+4*(5-6);z=2*3*4/15%13;

printf(" %d %d %d\n",x,y,z);x=2*3*4/(15%13);y=2*3*(4/15%13);z=2+33%5/4;printf(" %d %d %d\n",x,y,z);

x=2+33%5/4;y=2-33%-5/-4;z=-2*-3/-4%-5;printf(" %d %d %d\n",x,y,z);

x=50%(5*(16%12*(17/3)));y=2*-3%-4/-5-6+-7;z=8/4/2*2*4*8%13%7%3;printf(" %d %d %d\n",x,y,z);

}//7 62 1 12 0 2 2 2 -1 50 -13 2

//Area of a triangle#include<stdio.h>#include<math.h>main(){float a, b, c, s, area;printf("Enter the sides of a triangle\t");scanf("%f %f %f",&a, &b, &c);s=(a+b+c)/2;area=sqrt(s*(s-a) *(s-b) *(s-c));printf("Area of triangle is %6.2f sq. units",area);}

//Comment: Swap two numbers without using a third number#include<stdio.h>main(){ int number1, number2; printf("Enter numbers\t");

Page 6: Day 1 Examples

scanf("%d %d",&number1, &number2); printf("Numbers before swap %d %d\n",number1, number2); number2=number1+number2; number1=number2-number1; number2=number2-number1; printf("Numbers after swap %d %d\n",number1, number2);}