Friday 25 June 2021

file input and output in c program

 file input and output in c program

#include <stdio.h>
typedef struct employees
{
    int salary;
    char name[20];
emp;

int main()
{
    FILE *ptr;
    ptr = fopen("a.text""w");
    emp e1e2;
    printf("enter the name of e1:");
    scanf("%s"e1.name);
    printf("enter the salary of e1:");
    scanf("%d"&e1.salary);
    printf("enter the name of e2:");
    scanf("%s"e2.name);
    printf("enter the salary of e2:");
    scanf("%d"&e2.salary);

    fprintf(ptr"%s\t,"e1.name);
    fprintf(ptr"%d\n"e1.salary);
    fprintf(ptr"%s\t,"e2.name);
    fprintf(ptr"%d"e2.salary);
    fclose(ptr);

    return 0;
}


#include <stdio.h>

int main()
{
    FILE *ptr;
    char c;
    ptr = fopen("getdemo.text""r");
    c = fgetc(ptr);
    while (c != EOF)
    {
        printf("%c"c);
        c = fgetc(ptr);
    }

    fclose(ptr);
    return 0;
}


#include <stdio.h>

int main()
{
     FILE *ptr1;
     FILE *ptr2;
     ptr1 = fopen("single.text""r");
     ptr2 = fopen("double.text""w");
     int numnum2;
     fscanf(ptr1"%d"&num);
     num2 = 2 * num;
     fprintf(ptr2"%d"num2);

     return 0;
}


# include<stdio.h> 

 int main(){ 
FILE *ptr;
ptr=fopen("upen1.text","w");
int num=25;
fprintf(ptr,"the number is %d \n",num);
fprintf(ptr,"thanks for using it ");
fclose(ptr);
     
     return 0
}


#include <stdio.h>

int main()
{
  int numnum1;
  FILE *ptr;
  ptr = fopen("upen.text""r");
  if (ptr == NULL)
  {
    printf("this file doesn't exist");
  }
  else
  {
    fscanf(ptr"%d%d"&num&num1);
    printf("The number is %d \n"num);
    printf("The number is %d \n"num1);
    fclose(ptr);
  }

  return 0;
}


#include <stdio.h>

int main()
{
     FILE *ptr;
     char c;
     ptr = fopen("fputdemo.text""w");
     c = fputc('M'ptr);
     c = fputc('O'ptr);
     c = fputc('B'ptr);
     c = fputc('I'ptr);
     c = fputc('L'ptr);
     c = fputc('E'ptr);
     fclose(ptr);

     return 0;
}


#include <stdio.h>

int main()
{
   int mul[10];
   int num;
   FILE *ptr;
   ptr = fopen("Mul.text""w");
   printf("Enter the number of which table U want:");
   scanf("%d"&num);
   for (int i = 0i < 10i++)
   {
      mul[i= num * (i + 1);
   }
   for (int i = 0i < 10i++)
   {
      fprintf(ptr"%d*%d=%d\n"num, (i + 1), mul[i]);
   }

   return 0;
}


#include <stdio.h>

int main()
{
     FILE *ptr;
     ptr = fopen("a.text""r");
     int num[2];
     fscanf(ptr"%d%d%d"&num[0], &num[1], &num[2]);
     printf("%d \n"num[0]);
     printf("%d \n"num[1]);
     printf("%d \n"num[2]);

     return 0;
}


#include <stdio.h>

int main()
{
    FILE *ptr;
    ptr = fopen("Table.text""w");
    int num;
    printf("Enter the number:");
    scanf("%d"&num);
    for (int i = 0i < 10i++)
    {
        fprintf(ptr"%d*%d=%d\n"num, (i + 1), num * (i + 1));
    }
    fclose(ptr);

    fprintf("sucessfuly generated table of %d"num);

    return 0;
}


#include <stdio.h>

int main()
{
   FILE *ptr1;
   FILE *ptr2;
   ptr1 = fopen("file.text""r");
   ptr2 = fopen("file1.text""w");
   char c;
   c = fgetc(ptr1);
   while (c != EOF)
   {
      fputc(cptr2);
      fputc(cptr2);
      c = fgetc(ptr1);
   }
   fclose(ptr1);
   fclose(ptr2);

   return 0;
}



Pointer in c program problems

 Pointer in c program problems

#include <stdio.h>

int main()
{
    int i = 34;
    int *j = &i;
    int **k = &j;
    int ***l = &k;
    // int *ch_integer=&i;
    printf("the value of i is %d \n "i);
    printf("the value of i is %d \n "*(&i));
    printf("the address of i is %u \n "&i);
    printf("the add of j is %u \n "&j);
    printf("the value of j is %d \n "j);
    printf("the value of j is %d \n "*(&j));
    printf("the value of k is %d \n"*(&k));
    printf("the add of l is %d \n", (&l));
    printf("the value of l is %d \n"*(&l));
    // printf("the value of i is %d ", i);

    return 0;
}


#include <stdio.h>

int main()
{
    int x = 9;
    int *y = &x;
    printf("the add of x is %u \n"&x);
    printf("the value of x is %d \n"*(&x));
    printf("the value of x is %d \n"*y);
    printf("the add of y is %u \n"&y);
    printf("the value of y is %d \n"y);

    return 0;
}


#include <stdio.h>
void add(int a);
int main()
{
    int i = 4;
    printf("the value of i is %d \n"i);
    add(4);
    printf("the add of i is %u \n"&i);

    return 0;
}

void add(int a)
{
    printf("the add of a is %u\n"&a);
}


#include <stdio.h>
void change_10(int *a);
int main()
{
    int x;
    x = 10;
    printf("the value of x before is %d\n"x);
    change_10(&x);
    printf("the value of x after is %d "x);

    return 0;
}
void change_10(int *a)
{
    int temp;
    temp = *a * 10;
    *a = temp;
}


#include <stdio.h>
int sum(int *aint *b);
int avg(int *aint *b);
int main()
{
    int xyresultsresults1;
    printf("Enter the value of X and Y:\n");
    scanf("%d%d"&x&y);
    results = sum(&x&y);
    printf("the value of sum is %d\n"results);
    results1 = avg(&x&y);
    printf("the value of avg is %d\n"results1);

    return 0;
}
int sum(int *aint *b)
{
    return *a + *b;
}
int avg(int *aint *b)
{
    return (*a + *b/ 2;
}



#include <stdio.h>
void SumandAvg(int aint bint *sumfloat *avg)
{
    *sum = a + b;
    *avg = (float)(*sum / 2);
}
int main()
{
    int xysum;
    float avg;
    x = 267;
    y = 120;
    SumandAvg(xy&sum&avg);
    printf("The value of sum is %d \n"sum);
    printf("The value of avg is %f \n"avg);

    return 0;
}


#include <stdio.h>

int main()
{
    int i = 5;
    int *j = &i;
    int **k = &j;
    printf("the value of i is %d "*(*k));

    return 0;
}


#include <stdio.h>
void change(int a)
{
    int temp;
    temp = a * 10;
    a = temp;
}
int main()
{
    int x = 10;
    printf("The value of x before %d\n"x);
    change(x);
    printf("The value of x after %d"x);

    return 0;
}


#include <stdio.h>

void swap(int *aint *b);

int main()
{
    int x = 32y = 45;
    printf("the value of swap before %d , %d \n "xy);
    swap(&x&y);
    printf("the value of swap after %d , %d \n "xy);

    return 0;
}
void swap(int *aint *b)
{
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
}

function and recursion in c programs

 function and recursion in c programs

#include <stdio.h>
float average(int aint bint c); //prototype

int main()
{
    int number1number2number3;
    float results;
    printf("enter the number \n");
    scanf("%d%d%d"&number1&number2&number3);

    results = average(number1number2number3);
    printf("The value of average is %f"results);

    return 0;
}

float average(int aint bint c)
{
    float d;
    d = (float)(a + b + c/ 3;
    return d;
}


# include<stdio.h> 
float convertor(float a);
 int main(){ 
     float celsius,farheint;
     printf("Enter the Temperature in celsuis: \n");
     scanf("%f",&celsius);
     farheint=convertor(celsius);
     printf("%f is Farheint Temperatre",farheint);

     
     return 0
}

float convertor(float a){
    float c;
    c=(a*9/5)+32;
    return c;
}


# include<stdio.h> 
float force(int mass);

 int main(){ 

int   mass;
float c;
     printf("enter the mass \n ");
     scanf("%d",&mass);

c= force(mass);
printf("force is %f",c);

     
     return 0
}

float force(int mass ){
    float c;
    c=mass*9.8;
    return c;

}


#include <stdio.h>
int fibonacci(int a);
int main()
{
int number,results;
printf("enter the number u want fibonacci:");
scanf("%d",&number);
results= fibonacci(number);
printf("fibonacci of %d is %d",number,results);
    return 0;
}

int fibonacci(int a){
   
    if(a<=1)
   {return a;} 
    else{
    return fibonacci(a-1)+fibonacci(a-2);
    }
}




#include <stdio.h>

int main()
{
    int numberi;
    printf("enter the number of n to star print \n");
    scanf("%d"&number);
    for (int j = 1j <= numberj++)
    {
        for (int i = 1i <= ji++)
        {

            printf("*");
        }
        printf("\n");
    }

    return 0;
}


#include <stdio.h>
void printpattern(int n); // function prototype
int main()
{
    int number;
    printf("enter the number of star \n");
    scanf("%d"&number);
    printpattern(number);

    return 0;
}
void printpattern(int n)
{
    if (n == 1)
    {
        printf("*\n");

        return;
    }
    printf("calling function %d \n"n);
    printpattern(n - 1);

    for (int i = 0i < ni++)
    {
        printf("*");
    }
    printf("\n");
}



#include <stdio.h>
void print_table(int n);
int main()
{
    int number;
    printf("enter the natural number 1-n \n");
    scanf("%d"&number);
    print_table(number);

    return 0;
}

void print_table(int n)
{
    if (n == 1)
    {
        printf("%d\n"n);
        return;
    }
    printf("calling function %d\n"n);
    print_table(n - 1);
    printf("%d\n"n);
}


Loop and control in c

 Loop and control in c 

#include <stdio.h>

int main()
{

    int isum = 0;
    for (int i = 1i <= 10i++)
    {
        sum = sum + 8 * i;
        printf("the sum first 8 table multi is %d \n"sum);
    }
    return 0;
}


#include <stdio.h>

int main()
{
    int num = 8;
    while (num == 8)
    {
        printf("%d"num);
    }

    return 0;
}


#include <stdio.h>

int main()
{
    int num;
    printf("enter which table U want :");
    scanf("%d"&num);
    for (int i = 10ii--)
    {
        printf("%d*%d=%d \n"numinum * i);
    }

    return 0;
}


#include <stdio.h>

int main()
{
int i=1,sum=0;
do{
    sum=sum+i;
    printf("the sum of first 10 natural number is %d\n",sum);
    i++;
}while(i<=10);
    return 0;
}

# include<stdio.h> 

 int main(){ 
int fac=1,num;
printf("enter the number fac u want :");
scanf("%d",&num);
for (int i = 1i <=numi++)
{
    fac*=i;
}
printf("the value of %d! is %d",num,fac);
     
     return 0
}


#include <stdio.h>

int main()
{

   int sum = 0;
   for (int i = 1i <= 10i++)
   {
      sum = sum + i;
      printf("the sum of natural number is %d \n"sum);
   }

   return 0;
}


#include <stdio.h>

int main()
{
  int ijn;
  printf("Enter the n number of tables:");
  scanf("%d"&n);
  for (i = 1i <= ni++)
  {
    for (j = 1j <= 10j++)
    {
      printf("the value is %d\n"i * j);
    }
  }

  return 0;
}


#include <stdio.h>

int main()
{
    int inumprime = 1;
    printf("Enter The Number to verify prime Or Not:");
    scanf("%d"&num);
    for (int i = 2i < numi++)
    {
        if (num % i == 0)
        {
            prime = 0;
            break;
        }
    }

    if (prime == 0)
    {
        printf("this is not a prime Number");
    }

    else
    {
        printf("This is a prime Number");
    }

    return 0;
}


#include <stdio.h>

int main()
{
  int ij;

  for (i = 10ii--)
  {
    for (j = 1j <= 10j++)
    {
      printf("the value is %d\n"i * j);
    }
  }

  return 0;
}


#include <stdio.h>

int main()
{
    int numsum = 0;
    printf("Enter the sum of n natural num U want?:");
    scanf("%d"&num);
    for (int i = 1i <= numi++)
    {
        sum += i;
    }
    printf("the sum of sigma(1-num) is %d"sum);

    return 0;
}


#include <stdio.h>

int main()
{
    int i = 1sum = 0num;
    printf("Enter the n natural number Sum U want:");
    scanf("%d"&num);
    while (i <= num)
    {
        sum += i;
        i++;
    }
    printf("the sum of sigma(1-num): %d"sum);

    return 0;
}


#include <stdio.h>

int main()
{
  int i = 1sum = 0;
  while (i <= 10)
  {
    sum = sum + i;

    printf("The sum of value is %d \n"sum);
    i++;
  }

  return 0;
}


#include <stdio.h>

int main()
{
    int n = 0;
    while (n < 10)
    {
        printf("The sum of first n natural n is %d\n"n + 1);
        n++;
    }
    return 0;
}


#include <stdio.h>

int main()
{
    int i = 1sum = 0num;
    printf("Enter the  sum of natural number U want :");
    scanf("%d"&num);
    while (i <= num)
    {

        sum = sum + i;
        printf("The sum of natural number is %d\n"sum);
        i++;
    }

    return 0;
}



Python if / else

 Python if / else a1 = int ( input (" Enter the number: \n ")) a2 = int ( input (" Enter the number: \n ")) a3 = int ( i...