Thursday, December 12, 2019

C Program to print negative series 0, -1, -2, -3, -4, -5

Print Negative Series


C Program to print negative series 0, -1, -2, -3, -4, -5.... for the given value of -N.


For example, if n = -5, output will be 0, -1, -2, -3, -4, -5
 
#include <stdio.h>

int main()
{
    int i, n;
    printf("Enter n:");
    scanf("%d", &n);
    for(i=0; i>=-5; i--)
    {
        printf("%d ", i);
    }
    
    return 0;
}

Output of Program Execution-1

Enter n:-3
0 -1 -2 -3

Output of Program Execution-2

Enter n:-5
0 -1 -2 -3 -4 -5

 

Friday, November 29, 2019

Triangle Star Pattern


Following C program will print a triangle pattern based on given number N. For example, for n=3, program will print

   * 
  * * 
 * * * 

Triangle Star Pattern C Program


#include<stdio.h>

int main()
{
  int i, j, n;
  printf("Enter your triangle pattern size:");
  scanf("%d", &n);
  
  for(i=0; i<=n; i++)
  {
    for(j=0; j<=n-1-i; j++)
       printf(" "); 
    for(j=0; j<i; j++)
       printf("* "); 
    printf("\n"); 
  }
  return 0;
}

Program output for Execution 1:

Enter your triangle pattern size:3       
   * 
  * * 
 * * * 

Program output for Execution 2:

Enter your triangle pattern size:4       
     * 
    * * 
   * * * 
  * * * * 

Program output for Execution 3:

Enter your triangle pattern size:7       
      * 
     * * 
    * * * 
   * * * * 
  * * * * * 
 * * * * * * 
* * * * * * * 

Click here to read more interesting C Patterns Practicals.



Monday, April 29, 2019

Top 10 Programming Languages of the World – 2019


Top Programming Languages of the World


If you are a new to the field of programming, the very first question comes to your mind is
  • What to learn first? 
  • Which programming language will be useful to me in 2019?
  • Which will be most suitable language from career point of view?

There are many programming languages available in the field of Computer Science. One of the easiest ways to pick the best programming language to learn is by observing the demand of Software Industry. Some of the survey conducted by popular organizations are given below for your ready reference.

IEEE Spectrum [01]
GeeksForGeeks [02]
Guru99 [03]
FullStackAcademy [04]
1.      Python
2.      C++
3.      Java
4.      C
5.      C#
6.      PHP
7.      R
8.      JavaScript
9.      Go
10.  Assembly
1.      JavaScript
2.      Python
3.      Java
4.      C/CPP
5.      PHP
6.      Swift
7.      C#
8.      Ruby
9.      Objective – C
10.  SQL
1.      Python
2.      Java
3.      R
4.      JavaScript
5.      Swift
6.      C++
7.      C#
8.      PHP
9.      SQL
10.  Go
1.      Java Script
2.      Swift
3.      Java
4.      C/C++
5.      Python
6.      PHP
7.      Ruby
8.      C#
9.      RUST


References:
[01] spectrum.ieee.org/at-work/innovation/the-2018-top-programming-languages [This app/ranking mechanism was originally developed in collaboration with IEEE Spectrum by data journalist Nick Diakopoulous. ]
[02] www.geeksforgeeks.org/top-10-programming-languages-of-the-world-2019-to-begin-with
[03] www.guru99.com/best-programming-language.html
[04] www.fullstackacademy.com/blog/nine-best-programming-languages-to-learn-2018

More details about programming languages and its use will be added soon.

For more details visit: C Program Practicals

Sunday, March 31, 2019

Nested Structure in C

C programming provides important feature called nesting of Structure. A structure can be nested inside another structure. This feature is useful to maintain complex data structure. Following example explains how a Person structure is used in a Student structure.

Nesting of Structure in C

#include<stdio.h>

//Declaring a structure student
struct student
{
    struct person
    {
        char firstname[20];
        char lastname[20];
        char dob[10];
    } p ;

    int rollno;
    float marks;
};

int main()
{
    struct student s1;

    printf("*** READING STUDENT'S DETAILS *** \n\n");

    printf("Enter firstname: ");
    scanf("%s", s1.p.firstname);

    printf("Enter lastname: ");
    scanf("%s", s1.p.lastname);

    printf("Enter DoB(dd-mm-yyyy): ");
    scanf("%s", s1.p.dob);

    printf("Enter roll no: ");
    scanf("%d", &s1.rollno);

    printf("Enter marks: ");
    scanf("%f", &s1.marks);

    printf("\n*** PRINTING DETAILS OF STUDENT FROM STRUCTURE ***\n\n");

    printf("Name: %s %s\n", s1.p.firstname, s1.p.lastname);
    printf("DOB: %s\n", s1.p.dob);
    printf("Roll no: %d\n", s1.rollno);
    printf("Marks: %.2f\n", s1.marks);

    return 0;
}

Output of Program

*** READING STUDENT'S DETAILS ***

Enter firstname: Purv
Enter lastname: Patel
Enter DoB(dd-mm-yyyy): 10-12-2008
Enter roll no: 101
Enter marks: 85

*** PRINTING DETAILS OF STUDENT FROM STRUCTURE ***

Name: Purv Patel
DOB: 10-12-2008
Roll no: 101
Marks: 85.00


* * * * *

Recommended Readings: C Structure



Sunday, March 17, 2019

Caesar Cipher - Decryption Program


Caesar Cipher - Decryption Program

This program will decrypt a given encrypted (Cipher Text) file using Caesar Cipher Decryption Cryptography Algorithm.

Assumptions: 

  • Caesar cipher key used is 3.
  • Input file name is: cipher.txt
  • Output file will be plaintext.txt


#include<stdio.h>
int main() {
   FILE *inputFile, *outputFile;
   char ch;

   inputFile = fopen("cipher.txt", "r");
   if (inputFile == NULL) {
      puts("File cipher.txt Read Error.");
      exit(1);
   }

   outputFile = fopen("plaintext.txt", "w");
   if (outputFile == NULL) {
      puts("File plaintext.txt Write Error.");
      exit(1);
   }

   do {
      ch = fgetc(inputFile);
      fputc(ch - 3, outputFile);
   } while (ch != EOF);
   
  printf("cipher.txt file is successfully decrypted using Caesar Cipher.\n");
  printf("plaintext.txt file is generated successfully.");
  
  fclose(inputFile);
  fclose(outputFile);
  
  return 0;
}

Output of program

cipher.txt file is successfully decrypted using Caesar Cipher.
plaintext.txt file is generated successfully.

Notes: This program will deduct 3 from ASCII value of each character of a given cipher text data file. A sample execution data is given below:

Original content of cipher.txt input file before program execution:

Zhofrph#wr#F#Surjudp#Sudfwlfdov1Eorjvsrw1Frp
Wklv#lv#Fdhvdu#Flskhu#Hqfu|swlrq#Ghprqvwudwlrq1

Content of plaintext.txt file after decryption using program:

Welcome to C Program Practicals.Blogspot.Com
This is Caesar Cipher Encryption Demonstration.



Recommended Readings: Computer Security Practicals

Caesar Cipher - File Encryption Program


Caesar Cipher - Encryption Program

This program will encrypt a given Data file using Caesar Cipher Cryptography Algorithm.

Assumptions: 




  • Caesar cipher key used is 3.
  • Input file name is: data.txt
  • Output file will be output.txt

#include<stdio.h>
int main() {
   FILE *inputFile, *outputFile;
   char ch;

   inputFile = fopen("data.txt", "r");
   if (inputFile == NULL) {
      puts("File data.txt Open Error.");
      exit(1);
   }

   outputFile = fopen("output.txt", "w");
   if (outputFile == NULL) {
      puts("File output.txt Open Error.");
      exit(1);
   }

   do {
      ch = fgetc(inputFile);
      fputc(ch+3, outputFile);
   } while (ch != EOF);
   
  printf("Data.txt file is successfully encrypted using Caesar Cipher.\n");
  printf("Output.txt file is generated successfully.");
  
  fclose(inputFile);
  fclose(outputFile);
  
  return 0;
}


Output of program


Data.txt file is successfully encrypted using Caesar Cipher.
Output.txt file is generated successfully.

Notes: This program will add 3 to ASCII value of each character of a given data file. A sample execution data is given below:

Original content of Data.txt input file before program execution:

Welcome to C Program Practicals.Blogspot.Com
This is Caesar Cipher Encryption Demonstration.

Content of Output.txt file after program encryption:

Zhofrph#wr#F#Surjudp#Sudfwlfdov1Eorjvsrw1Frp
Wklv#lv#Fdhvdu#Flskhu#Hqfu|swlrq#Ghprqvwudwlrq1


Recommended Readings: Computer Security Practicals


* * * * *


Sunday, March 3, 2019

Program to find execution time.


Program to find execution time.

This program will find total CPU execution time taken by a for loop of a program.

#include<stdio.h>
#include<time.h>
int main() 
{
int i;
float executionTime;
clock_t startTime, endTime;

startTime = clock();
//Get Time value before for loop execution 

for (i = 0; i < 100; i++) {
printf("%d, ", i);
}

endTime = clock();
//Get Time after for loop execution 

executionTime = ((float) (endTime - startTime)) / CLOCKS_PER_SEC;
//calulate total time

printf("\n\nTime taken to execute \"for loop\" 100 times is: %f seconds.", executionTime);
return 0;
}

Output of program

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,

Time taken to execute "for loop" 100 times is: 0.016000 seconds.



Recommended Readings: C Program Practicals / Special C Programs

Wednesday, February 20, 2019

Special alphabet pattern as per user request


Special Alphabet Pattern

This program is written for special request received from blogger "Sree". Thanks to Sree for referring this blog.

Following C program will print special alphabet pattern series.

For n=3, expected output is

A B C B A
  B C B
    C

For n=4, expected output is

A B C D C B A
  B C D C B
    C D C 
      D

#include<stdio.h>
int main() 
{
  int i, j, k, n;
  printf("Enter your number:");
  scanf("%d", &n);
  for (i=0; i<n; i++) 
  {
     //logic for intial spaces
     for(j=0; j<i; j++) 
          printf("  ");

     //printing first half in a row(eg for n=4; ABCD)
     for(k=65+i; k<65+n-1; k++)
           printf("%c ", k);
     //printing second half in a row(eg for n=4; CBA)

     for( ; k >=65+i; k--)
           printf("%c ", k);

     printf("\n");
  }
  return 0; 
}

Output of program

Enter your number:4

A B C D C B A
  B C D C B
    C D C
      D


Read more Recommended Patterns Programs here.


Saturday, February 9, 2019

C++ Program to calculate Area of Circle

/*
Following C++ program will calculate Area of Circle for the given radius.
*/

#include <iostream>
using namespace std;

int main()
{
    float radius, area;

    cout << "Enter the radius of circle : ";
    cin >> radius;
    
    area = 3.14 * radius * radius;
    
    cout << "Area of circle with radius "
         << radius << " is " << area;
}

Output of program

Enter the radius of circle : 2
Area of circle with radius 2 is 12.56