IT Solution Menu

N O T I C E

Project on Computer topics, Programs for practicals for BCA Students, and Mobile Android app requirements. Please pay less and get more immediate delivery for school and higher level. Thank You. Contact me - at +91-8800304018(w)

Thursday, August 22, 2024

C for beginner's programming with output.

 Top


Programming and their output in C Language.

by Md Farrukh Asif

 

Programming with output are given for the beneficial of students that can help in learning.

Keywords:



Programming in C,

C Programm,

Python Programm,

C/C++ Program,

File handling inC program and many more……

 C Language:

C Language:

C for the beginner 's by Md Farrukh Asif

Operators in C by Md. Farrukh Asif

C's Control Flow by Md. Farrukh Asif

String handling in C by Md Farrukh Asif

The Function of C Language by Md. Farrukh Asif

File Handling with various statements/syntaxes in Programming and their output in C Language.

C for beginner's programming with output. Course Code – 106 [‘C’]) TMBU,BGP

BCA SOLVED EXAMINATION QUESTIONS WITH ANSWERS Course Code – 106 [‘C’]) TMBU,BGP

1. C Program to Multiply two Floating-Point Numbers

 

#include <stdio.h>

int main() {

 float num1, num2, product;

 printf(“Enter two floating-point numbers: “);

 scanf(“%f %f”, &num1, &num2);

 product = num1 * num2;

 printf(“Product: %f\n”, product);

 return 0;

}

 


2. C Program to Print the ASCII Value of a Character


 

#include <stdio.h>

int main() {

 char ch;

 printf(“Enter a character: “);

scanf(“%c”, &ch);

 printf(“ASCII value of %c = %d\n”, ch, ch);

 return 0;

}

 

3. C Program to Swap Two Numbers

 

#include <stdio.h>

int main() {

 int num1, num2, temp;

 printf(“Enter two numbers: “);

 scanf(“%d %d”, &num1, &num2);

 temp = num1;

 num1 = num2;

 num2 = temp;

 printf(“After swapping: num1 = %d, num2 = %d\n”, num1, num2);

 return 0;

}

 

4. C Program to Calculate Fahrenheit to Celsius

 

#include <stdio.h>

int main() {

 float fahrenheit, celsius;

 printf(“Enter temperature in Fahrenheit: “);

 scanf(“%f”, &fahrenheit);

 celsius = (fahrenheit – 32) * 5 / 9;

 printf(“Temperature in Celsius: %f\n”, celsius);

 return 0;

}

 

5. C Program to Find the Size of int, float, double, and char

 

#include <stdio.h>

int main() {

 printf(“Size of int: %d bytes\n”, sizeof(int));

 printf(“Size of float: %d bytes\n”, sizeof(float));

 printf(“Size of double: %d bytes\n”, sizeof(double));

 printf(“Size of char: %d byte\n”, sizeof(char));

 return 0;

}

 

6. C Program to Print Prime Numbers From 1 to N

 

#include <stdio.h>

int main() {

 int i, j, n;

 printf(“Enter a number (N): “);

 scanf(“%d”, &n);

 printf(“Prime numbers between 1 and %d are: “, n);

 for (i = 2; i <= n; ++i) {

 int isPrime = 1;

 for (j = 2; j <= i / 2; ++j) {

 if (i % j == 0) {

 isPrime = 0;

 break;

 }

 }

 if (isPrime)

 printf(“%d “, i);

 }

 return 0;

}

 

 

C Programming Examples With Output

 

1. C Program to Check Whether a Number is Positive, Negative, or Zero

 

#include <stdio.h>

int main() {

 int num;

 printf(“Enter a number: “);

 scanf(“%d”, &num);

 if (num > 0)

 printf(“Positive number\n”);

 else if (num < 0)

 printf(“Negative number\n”);

 else

 printf(“Zero\n”);

 return 0;

}

 

Output:

 

Enter a number: 7

Positive number

 

2. C Program to Check Whether Number is Even or Odd

 

#include <stdio.h>

int main() {

 int num;

 printf(“Enter a number: “);

 scanf(“%d”, &num);

 if (num % 2 == 0)

 printf(“Even number\n”);

 else

 printf(“Odd number\n”);

 return 0;

}

 

Output:

 

Enter a number: 15

Odd number

 

3. C Program to Calculate Sum of Natural Numbers

 

#include <stdio.h>

int main() {

 int n, sum = 0;

 printf(“Enter a positive integer: “);

 scanf(“%d”, &n);

 for (int i = 1; i <= n; ++i) {

 sum += i;

 }

 printf(“Sum of natural numbers from 1 to %d: %d\n”, n, sum);

 return 0;

}

 

Output:

 

Enter a positive integer: 5

Sum of natural numbers from 1 to 5: 15

 

4. C Program to Print Alphabets From A to Z Using Loop

 

#include <stdio.h>

int main() {

 char ch;

 printf(“Alphabets from A to Z:\n”);

 for (ch = ‘A’; ch <= ‘Z’; ++ch) {

 printf(“%c “, ch);

 }

 return 0;

}

 

Output:

 

Alphabets from A to Z:

 

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

5. C Program to Print Ascii value of respective character.

 

#include <stdio.h>

int main() { 

    char c;

    printf("Enter a character: ");

    scanf("%c", &c); 

   

    // %d displays the integer value of a character

    // %c displays the actual character

    printf("ASCII value of %c = %d", c, c);

   

    return 0;

}

 

Output:

 

Enter a character: 9

ASCII value of 9 = 57

 

=== Code Execution Successful ===

 

6. C Program to Calculate the Power of a Number

include <stdio.h>

int main() {

    int base, exp;

    long double result = 1.0;

    printf("Enter a base number: ");

    scanf("%d", &base);

    printf("Enter an exponent: ");

    scanf("%d", &exp);

    while (exp != 0) {

        result *= base;

        --exp;

    }

    printf("Answer = %.0Lf", result);

    return 0;

}

 

Output:

Enter a base number: 3

Enter an exponent: 4

Answer = 81

 

 

7. C Program to Display Armstrong Number Between Two Intervals

 

#include <math.h>

#include <stdio.h>

int main() {

  int low, high, number, originalNumber, rem, count = 0;

  double result = 0.0;

  printf("Enter two numbers(intervals): ");

  scanf("%d %d", &low, &high);

  printf("Armstrong numbers between %d and %d are: ", low, high);

 

  // swap numbers if high < low

  if (high < low) {

    high += low;

    low = high - low;

    high -= low;

  }

  

  // iterate number from (low + 1) to (high - 1)

  // In each iteration, check if number is Armstrong

  for (number = low + 1; number < high; ++number) {

    originalNumber = number;

 

    // number of digits calculation

    while (originalNumber != 0) {

      originalNumber /= 10;

      ++count;

    }

 

    originalNumber = number;

 

    // result contains sum of nth power of individual digits

    while (originalNumber != 0) {

      rem = originalNumber % 10;

      result += pow(rem, count);

      originalNumber /= 10;

    }

 

    // check if number is equal to the sum of nth power of individual digits

    if ((int)result == number) {

      printf("%d ", number);

    }

 

    // resetting the values

    count = 0;

    result = 0;

  }

 

  return 0;

}

 

Output:

Enter two numbers(intervals):   200

2000

 

Armstrong numbers between 200 and 2000 are: 370 371 407 1634

8. C Program to Find Transpose of a Matrix

 

#include <stdio.h>

int main() {

  int a[10][10], transpose[10][10], r, c;

  printf("Enter rows and columns: ");

  scanf("%d %d", &r, &c);

 

  // asssigning elements to the matrix

  printf("\nEnter matrix elements:\n");

  for (int i = 0; i < r; ++i)

  for (int j = 0; j < c; ++j) {

    printf("Enter element a%d%d: ", i + 1, j + 1);

    scanf("%d", &a[i][j]);

  }

 

  // printing the matrix a[][]

  printf("\nEntered matrix: \n");

  for (int i = 0; i < r; ++i)

  for (int j = 0; j < c; ++j) {

    printf("%d  ", a[i][j]);

    if (j == c - 1)

    printf("\n");

  }

 

  // computing the transpose

  for (int i = 0; i < r; ++i)

  for (int j = 0; j < c; ++j) {

    transpose[j][i] = a[i][j];

  }

 

  // printing the transpose

  printf("\nTranspose of the matrix:\n");

  for (int i = 0; i < c; ++i)

  for (int j = 0; j < r; ++j) {

    printf("%d  ", transpose[i][j]);

    if (j == r - 1)

    printf("\n");

  }

  return 0;

}

 

Output

Enter rows and columns: 2

3

 

Enter matrix elements:

Enter element a11: 1

Enter element a12: 4

Enter element a13: 0

Enter element a21: -5

Enter element a22: 2

Enter element a23: 7

 

Entered matrix:

1  4  0

-5  2  7

 

Transpose of the matrix:

1  -5

4  2

0  7

 

9. C Program to Store Information of a Student Using Structure

 

#include <stdio.h>

struct student {

    char name[50];

    int roll;

    float marks;

} s;

 

int main() {

    printf("Enter information:\n");

    printf("Enter name: ");

    fgets(s.name, sizeof(s.name), stdin);

 

    printf("Enter roll number: ");

    scanf("%d", &s.roll);

    printf("Enter marks: ");

    scanf("%f", &s.marks);

 

    printf("Displaying Information:\n");

    printf("Name: ");

    printf("%s", s.name);

    printf("Roll number: %d\n", s.roll);

    printf("Marks: %.1f\n", s.marks);

 

    return 0;

}

 

Output:

Enter information:

Enter name: Jack

Enter roll number: 23

Enter marks: 34.5

Displaying Information:

Name: Jack

Roll number: 23

Marks: 34.5

 

10. C Program to Store Data in Structures Dynamically

 

#include <stdio.h>

#include <stdlib.h>

struct course {

  int marks;

  char subject[30];

};

 

int main() {

  struct course *ptr;

  int noOfRecords;

  printf("Enter the number of records: ");

  scanf("%d", &noOfRecords);

 

  // Memory allocation for noOfRecords structures

  ptr = (struct course *)malloc(noOfRecords * sizeof(struct course));

  for (int i = 0; i < noOfRecords; ++i) {

    printf("Enter subject and marks:\n");

    scanf("%s %d", (ptr + i)->subject, &(ptr + i)->marks);

  }

 

  printf("Displaying Information:\n");

  for (int i = 0; i < noOfRecords; ++i) {

    printf("%s\t%d\n", (ptr + i)->subject, (ptr + i)->marks);

  }

 

  free(ptr);

 

  return 0;

}

 

Output:

Enter the number of records: 2

Enter subject and marks:

Science 82

Enter subject and marks:

DSA 73

 

Displaying Information:

Science     82

DSA     73

 

File Handling in C:

Data storage in a file using a program is known as file handling. Programs are written in C using file handling for storing the results and other program data in files. To use the data in the application, we may also extract or fetch it from a file.

We can use our C application to update, create, read, and delete files that are stored on the local file system thanks to file handling in C. A file can undergo the following procedures:

·        Creation of a new file

·        Opening an existing file

·        Reading from the file

·        Writing to the file

·        Deleting the file

 

File Handling Function in C

A file can be easily opened, expanded upon, read, created, deleted, closed, searched for, etc., via some different functions. 

In C, we can refer to these as file-handling operators.

fopen()

Opens a file

fprint()

Prints an available file

fscan()

Reads the data present in the file

fputc()

Writes a character to the file

fgetc()

Reads the character to the file

fseek()

Sets the pointer to the intended file position 

fclose()

Closes a program file

fputw()

Writes an integer from the file

fgetw()

Reads an integer from the file

ftell()

Tells the current position of the file

We use a straightforward text editor to create the text files with the.txt extension. The binary files contain the data and information using the 1s and 0s binary coding scheme. The disadvantage of text files in a program is fixed by creating binary files, which have the extension.bin.

fopen()

Before a file may be read, written to, or updated, the user must open it. A file is opened using the fopen() method. Below is the syntax for fopen() function.

File = fopen(“Name_of_file”,”mode)

There are a few file-opening modes present in the C language. Let us have a look at them below:

r

Will open a text file (read mode only)

w

Will open a text file (write mode only)

a

Will open a text file (append mode only)

r+

Will open a text file (both read and write modes)

w+

Will open a text file (both read and write modes)

rb

Will open a binary file (read mode only) 

wb

Will open a binary file (write mode only)

ab

Will open a binary file (append mode only)

rb+

Will open a binary file (both read and write modes)

wb+

Will open a binary file (both read and write modes)

ab+

Will open a binary file (both read and write modes)

fClose() – Closing a file 

Once a program has finished writing or reading a file, it must be closed (for both text and binary files). The fclose() method in a program is used to close a file.

 

// C program to Open a File,

// Write in it, And Close the File

 

# include <stdio.h>

# include <string.h>

 

int main( )

{

    FILE *file ;

   

    char dataToBeWritten[50]

        = "NaukriInfo - Giving best experience to its learners";

 

    file = fopen("ABC.c", "w") ;

    if ( file == NULL )

    {

        printf( "The ABC.c file failed to open." ) ;

    }

    else

    {

        printf("The file is open\n") ;

        if ( strlen ( dataToBeWritten ) > 0 )

        {

            

            fputs(dataToBeWritten, file) ;

            fputs("\n", file) ;

        }

       

        fclose(file) ;

       

        printf("The data is successfully written");

        printf("The file is close") ;

    }

    return 0;   

}

 

The output of the program will be:

The ABC.c file failed to open.

 

 

11. This program stores a sentence entered by the user in a file.

 

#include <stdio.h>

#include <stdlib.h>

 

int main() {

    char sentence[1000];

 

    // creating file pointer to work with files

    FILE *fptr;

 

    // opening file in writing mode

    fptr = fopen("program.txt", "w");

 

    // exiting program

    if (fptr == NULL) {

        printf("Error!");

        exit(1);

    }

    printf("Enter a sentence:\n");

    fgets(sentence, sizeof(sentence), stdin);

    fprintf(fptr, "%s", sentence);

    fclose(fptr);

    return 0;

}

 

Output:

Enter a sentence: C Programming is fun

 

Here, a file named program.txt is created. The file will contain C programming is fun text.

 

12. C Program to Read the First Line From a File

 

#include <stdio.h>

#include <stdlib.h> // For exit() function

int main() {

    char c[1000];

    FILE *fptr;

    if ((fptr = fopen("program.txt", "r")) == NULL) {

        printf("Error! File cannot be opened.");

        // Program exits if the file pointer returns NULL.

        exit(1);

    }

 

    // reads text until newline is encountered

    fscanf(fptr, "%[^\n]", c);

    printf("Data from the file:\n%s", c);

    fclose(fptr);

 

    return 0;

}


If the file is found, the program saves the content of the file to a string c until '\n' newline is encountered.

 

Suppose the program.txt file contains the following text in the current directory.

C programming is awesome.

I love C programming.

How are you doing?

 

The output of the program will be:

Data from the file:

 

C programming is awesome.

 

If the file program.txt is not found, the program prints the error message.

 

 

 

 

 

*** See You Again ***

************************

Share and Subscribe with Like

************************ 

No comments:

Post a Comment

Top Popular Post