Kategori arşivi: Uncategorized

C examples

FINDING QUADRATIC EQUATION ROOTS FROM QUADRATIC FORMULA


#include<stdio.h>
#include<conio.h>
#include<math.h>
#define PI 3.14159265359
 
float main(void)
{
   double a , b , c, delta ;
   printf("a,b,c katsayilarini sirasi ile gir=\n");
   scanf("%lf%lf%lf", &a, &b, &c);
   delta = pow(b, 2) - (4 * a * c );
   printf("delta=%2.2lf\n" , delta);
   
   if (delta > 0 ){
   printf("2 reel kok var\n");
   }
     else{
     	printf ("reel kok yok\n");
	 }

     
	 
	 if (delta < 0) {
	 
      printf("cikmak icin bir tusa bas\n ");
        getch();
      } 
         else{
         	double x1, x2;
         	x1 = (-b + sqrt(delta)) / (2 *a);
         	x2 = (-b - sqrt(delta)) / (2 *a); 
         	printf("x1=%2.lf x2=%2.lf\n",x1 ,x2);
         	printf ("cikmak icin bir tusa bas");
			 getch();
         	
		 }
      
   return 0;
}

BASIC UPTIME COUNTER

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main (void){
	int secup;
	int min, hour, day, week, sec ;
	for (; ;){
		secup += 1;
		sleep(1);
		sec=secup%60;
		min= (secup / 60)%60;
		hour = (secup / (60*60))%24;
		day = (secup / (60*60*24))%7;
		week = (secup / (60*60*24*7))%52;
		printf("uptime = week:%d,day:%d,hour:%d,min:%d,sec:%d\n",week, day, hour, min, sec);
		
	}
}

Calculating square’s perimeter or area from char veriable(If)

#include <math.h>
 int main (void) {
 
 float side, peri, area ;
 char code ;
 printf("enter p for perimeter, enter a for area\n");
 scanf("%c",&code);
 printf ("enter lenght of one side of square:");
 scanf("%f", &side);
 
 if (code == 'p'){
 	peri=side * 4;
 	printf("perimeter is :%f",peri);
 }
    else if (code =='P'){
    		peri=side * 4;
    		printf("perimeter is :%f",peri);
    		}
    		else if (code == 'a'){
    		area= pow(side,2);
    		printf("area=%f",area);
			}
			 else if (code == 'A'){
			 area= pow(side,2);
    		printf("area=%f",area);
			 }
			 else 
			 printf("wrong type input");
 	
 
 	return(0);
 }

Calculating square’s perimeter or area from char veriable(switch)

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
 int main (void) {
 
 float side, peri, area ;
 char code ;
 printf("enter p for perimeter, enter a for area\n");
 scanf("%c",&code);
 printf ("enter lenght of one side of square:");
 scanf("%f", &side);
 
 switch (code) {
 case 'p':
 case 'P':
 	peri = side * 4;
 	printf("perimeter is:%f:", peri);
 	break;
 case 'A':
 case 'a':
    area = pow(side,2);	
    printf("area is:%f", area);
    break;
default:
    	printf("wrong format input only a or p");
 }
 	return(0);
 }

Discount calculator due to total bill

// t for tables and tables are $25(per unit)

// “c” for chairs and chairs are$18(per unit)

/*if total bill is between$0-$499 discount rate is %0 ,  $500-$999 discount rate is %1.5,  $1000-$1499 discount rate is %2 , over $1500 discount rate is %3*/

#include <stdio.h>
#include <stdlib.h>
 int main (void) {
 	 char code;
 	 float bill, number_item;
 	 printf ("enter the code of the item:");
 	 scanf("%c", &code);
 	 switch (code) {
 	 	
 	 	case 'T':
 	 	case 't':	
 	 		printf("enter the number of tables sold");
 	 		scanf("%f", &number_item);
 	 		bill= 25 * number_item;
 	 		break;
 	    case 'C':
 	    case 'c':
 	 		printf("enter the number of chair sold");
			  scanf("%f", &number_item);
			  bill= 18 * number_item;
			  break;
		default:
		 printf("wrong format input");
		 	  		
	  }
 
 if (bill > 1500) {
 
 	bill= bill* 97/100;
	 }
 	else if( bill > 1000){
	 
 		bill=bill*98/100;
		 }
 		else if (bill > 500){
		 
 			bill=bill*98.5/100;
			 }
		 
	 
 	
 	printf("The bill is:%f", bill);
 	return(0);
 }