c language tutorial

Print numbers from 1 to 10 using while loop Program in C

/*Program to print numbers from 1 to 10 using while loop*/ #include<stdio.h> int main(void) { int i=1; while(i<=10) { printf(“%d\n”,i); i = i+1; /*Statement that changes the value of condition*/ } printf(“\n”); return 0; } Output: 1 2 3 4 5 6 7 8 9 10

Find out the grade of a student Program in C

/*Program to find out the grade of a student when the marks of 4 subjects are given. The method of assigning grade is – percentage>=85 grade=A percentage<85 and percentage>=70 grade=B percentage<70 and percentage>=55 grade=C percentage<55 and percentage>=40 grade=D percentage

Binary Search Program in C

#include<stdio.h> void main() { int i,ar[10],pos=-1, n,mid, lb, ub; printf(“Enter array in sorted order:-\n”); for(i=0; i<=9; i++) { printf(“ar[%d]=”,i); scanf(“%d”,&ar[i]); } lb = 0; ub = 9; printf(“Enter number to be search=”); scanf(“%d”,&n); while(lb<ub) { mid = (lb+ub)/2; if(ar[mid]==n) { pos=mid+1; break; } else if(ar[mid]&lt;n) { lb = mid+1; } else { ub = mid-1; […]

Find biggest number from three given numbers Program in C

/*Program to find biggest number from three given numbers*/ #include<stdio.h> int main(void) { int a,b,c,big; printf(“Enter three numbers : “); scanf(“%d%d%d”, &a, &b, &c); if(a>b) { if(a>c) big=a; else big=c; } else { if(b>c) big=b; else big=c; } printf(“Biggest number is %d\n”,big); return 0; }/*End of main()*/ Output: Enter three numbers : 5 10 14 […]

Find quotient and remainder Program in C

/*Program to find quotient and remainder*/ #include<stdio.h> int main(void) { int x,y,quo,rem; printf(“Enter two numbers : “); scanf(“%d%d”,&x,&y); if(y) /*if y is non-zero*/ { quo=x/y; rem=x%y; printf(“Quotient=%d, Remainder=%d\n”,quo,rem); } else printf(“Divide by zero error\n”); return 0; } Output: Enter two numbers : 25 7 Quotient=3, Remainder=4

Print a message if negative number is entered Program in C

/*Program to print a message if negative number is entered */ #include<stdio.h> int main(void) { int num; printf(“Enter a number : “); scanf(“%d”,&num); if(num<0) { printf(“Number entered is negative\n”); num=-num; } printf(“Value of num is : %d\n”, num); return 0; } Output: Enter a number : -15 Number entered is negative Value of num is […]

Print whether a number is even or odd Program in C

/*Program to print whether a number is even or odd */ #include<stdio.h> int main(void) { int num; printf(“Enter a number : “); scanf(“%d”,&num); if(num%2 == 0) /*test for even */ printf(“Number is even\n”); else { printf(“Number is odd\n”); num*=2; printf(“Now the number is %d\n”,num); } return 0; } Output: Enter a number : 15 Number […]

Print the bigger number Program in C

/*Program to print the bigger number*/ #include<stdio.h> int main(void) { int a,b; printf(“Enter two numbers : “); scanf(“%d%d”,&a,&b); if(a>b) printf(“Bigger number=%d\n”,a); else printf(“Bigger number=%d\n”,b); return 0; } Output: Enter two numbers : 5 15 Bigger number=15

BODMAS expressions Program in C

/*P4.12 Program to evaluate some expressions*/ int main(void) { int a,b,c,d,e,f,g,h,k; a=8, b=4, c=2, d=1, e=5, f=20; printf(“%d\t”, a+b-(c+d)*3%e+f/9); a=17, b=5, c=6, d=3, e=5; printf(“%d\t”,a%6-b/2+(c*d-5)/e); a=4, b=5, c=6, d=3, e=5, f=10; printf(“%d\t”,a*b-c/d

use of cast operator Program in C

/*P4.11 Program to illustrate the use of cast operator*/ #include<stdio.h> int main(void) { int x=5,y=2; float p,q; p=x/y; printf(“p=%f\n”,p); q=(float)x/y; printf(“q=%f\n”,q); return 0; } Output: p=2.000000 q=2.500000

1 2 3 4 13