You are here

C programs

Floyd's triangle in C

C program to print Floyd's triangle:- This program prints Floyd's triangle. Number of rows of Floyd's triangle to print is entered by the user. First four rows of Floyd's triangle are as follows :-
1
2 3
4 5 6
7 8 9 10
It's clear that in Floyd's triangle nth row contains n numbers.

C programming code

#include <stdio.h>

int main()
{
  int n, i,  c, a = 1;

  printf("Enter the number of rows of Floyd's triangle to print\n");
  scanf("%d", &n);

  for (i = 1; i <= n; i++)
  {
    for (c = 1; c <= i; c++)
    {
      printf("%d ",a);
      a++;
    }

C program to add two complex numbers

C program to add two complex numbers: this program calculate the sum of two complex numbers which will be entered by the user and then prints it. User will have to enter the real and imaginary parts of two complex numbers. In our program we will add real parts and imaginary parts of complex numbers and prints the complex number, i is the symbol used for iota. For example,if user entered two complex numbers as (1 + 2i) and (4 + 6 i) then output of program will be (5+8i). A structure is used to store complex number.

C programming code

#include <stdio.h>

struct complex

Fibonacci series in C

Fibonacci series in C programming: C program for Fibonacci series without and recursion. Using the code below you can print as many number of terms of series as desired. Numbers of Fibonacci sequence are known as Fibonacci numbers. First few numbers of series are 0, 1, 1, 2, 3, 5, 8 etc, Except first two terms in sequence every other term is the sum of two previous terms, For example,8 = 3 + 5 (addition of 3, 5). This sequence has many applications in mathematics and Computer Science.

Fibonacci series in C using for loop

 

String length in C

C string length program: This program prints length of a string, for example, consider the string "c programming" it's length is 13. Null character isn't counted when calculating string length. To find string length we use strlen function of string.h.

C program to find string length

#include<stdio.h>
#include<string.h>

main()
{
   char a[100];
   int length;

   printf("Enter a string to calculate it's length\n");
   gets(a);

   length = strlen(a);

   printf("Length of entered string is = %d\n",length);

   return 0;
}

Factorial program in C

Factorial program in c: c code to find and print factorial of a number, three methods are given, first one uses for loop, second uses a function to find factorial and third using recursion. Factorial is represented using '!', so five factorial will be written as (5!), n factorial as (n!). Also n! = n*(n-1)*(n-2)*(n-3)...3.2.1 and zero factorial is defined as one i.e. 0! = 1.

Factorial program in C using for loop

: Here we find factorial using for loop.
 

C program to generate random numbers

This C program generates random numbers using random function, randomize function is used to initialize random number generator. If you don't use randomize function then you will get same random numbers each time you run the program.

C programming code using rand

#include <stdio.h>
#include <stdlib.h>

int main() {
  int c, n;

  printf("Ten random numbers in [1,100]\n");

  for (c = 1; c <= 10; c++) {
    n = rand()%100 + 1;
    printf("%d\n", n);
  }

  return 0;
}

C programming code using random function(Turbo C compiler only)

 

C program to get IP address

This C program prints IP (internet protocol) address of your computer, system function is used to execute the command ipconfig which prints IP address, subnet mask, and default gateway. The code given below works for Windows XP and Windows 7. If you are using Turbo C compiler then execute the program from the folder, it may not work when you are working in compiler and press Ctrl + F9 to run your program.

C programming code

#include<stdlib.h>

main()
{
   system("C:\\Windows\\System32\\ipconfig");
   system("pause");
       
   return 0;
}

Pages

Subscribe to RSS - C programs