You are on page 1of 8

C programming code

This code checks whether an input alphabet is a vowel or not. Both lower-case and upper-case are
checked.

#include <stdio.h>
int main()
{
char ch;
printf("Enter a character\n");
scanf("%c", &ch);
if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch ==
'i' || ch == 'I' || ch =='o' || ch=='O' || ch == 'u' || ch ==
'U')
{ printf("%c is a vowel.\n", ch);
}
else
{ printf("%c is not a vowel.\n", ch);
}
return 0;
}
Output of program:

These program prints various different patterns of numbers and stars. These codes illustrate how to
create various patterns using c programming. Most of these c programs involve usage of nested loops
and space. A pattern of numbers, star or characters is a way of arranging these in some logical
manner or they may form a sequence. Some of these patterns are triangles which have special
importance in mathematics. Some patterns are symmetrical while other are not.

#include <stdio.h>
int main()
{
int row, c, n, temp;
printf("Enter the number of rows in pyramid of stars you wish
to see ");
scanf("%d",&n);
temp = n;
for ( row = 1 ; row <= n ; row++ )
{
for ( c = 1 ; c < temp ; c++ )
printf(" ");
temp--;
for ( c = 1 ; c <= 2*row - 1 ; c++ )
printf("*");
printf("\n");
}
return 0;
}
Output of program:

C program for bubble sort: c programming code for bubble sort to sort numbers or arrange them in
ascending order. You can easily modify it to print numbers in descending order.
Bubble sort algorithm in c
/* Bubble sort code */
#include <stdio.h>
int main()
{
int array[100], n, c, d, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0 ; c <
{
for (d = 0 ; d
{
if (array[d]
{
swap
array[d]
array[d+1]
}
}
}

( n - 1 ); c++)
< n - c - 1; d++)
> array[d+1]) /* For decreasing order use < */
= array[d];
= array[d+1];
= swap;

printf("Sorted list in ascending order:\n");


for ( c = 0 ; c < n ; c++ )
printf("%d\n", array[c]);
}

return 0;

Download Bubble sort program.

Output of program:

This c program compares two strings using strcmp, without strcmp and using pointers. For
comparing strings without using library function see another code below.

C program to compare two strings using strcmp


#include <stdio.h>
#include <string.h>
int main()
{
char a[100], b[100];
printf("Enter the first string\n");
gets(a);
printf("Enter the second string\n");
gets(b);
if( strcmp(a,b) == 0 )
printf("Entered strings are equal.\n");
else
printf("Entered strings are not equal.\n");
return 0;
}
Output of program:

This program concatenates strings, for example if the first string is "c " and second string is
"program" then on concatenating these two strings we get the string "c program". To concatenate two
strings we use strcat function of string.h, to concatenate without using library function see another
code below which uses pointers.

C programming code
#include <stdio.h>
#include <string.h>
int main()
{
char a[100], b[100];
printf("Enter the first string\n");
gets(a);
printf("Enter the second string\n");
gets(b);
strcat(a,b);
printf("String obtained on concatenation is %s\n",a);
return 0;
}
Output of program:

C code to remove spaces or excess blanks from a string, For example consider the string
"c programming"

There are two spaces in this string, so our program will print a string
"c programming". It will remove spaces when they occur more than one time consecutively in string
anywhere.

C programming code
#include <stdio.h>
int main()
{
char text[100], blank[100];
int c = 0, d = 0;
printf("Enter some text\n");
gets(text);
while (text[c] != '\0')
{
if (!(text[c] == ' ' && text[c+1] == ' ')) {
blank[d] = text[c];
d++;
}
c++;
}
blank[d] = '\0';
printf("Text after removing blanks\n%s\n", blank);
return 0;
}

Output of program:

C Program to shutdown your computer: This program turn off i.e shutdown your computer system.
Firstly it will asks you to shutdown your computer if you press 'y' then your computer will shutdown
in 30 seconds, system function of "stdlib.h" is used to run an executable file shutdown.exe which is
present in C:\WINDOWS\system32 in Windows XP. You can use various options while executing
shutdown.exe, you can use -t option to specify number of seconds after which shutdown occurs.
Syntax: shutdown -s -t x; here x is the number of seconds after which shutdown will occur.
By default shutdown occur after 30 seconds. To shutdown immediately you can write "shutdown -s -t
0". If you wish to restart your computer then you can write "shutdown -r".
If you are using Turbo C Compiler then execute your program from command prompt or by opening
the executable file from the folder. Press F9 to build your executable file from source program. When
you run program from within the compiler by pressing Ctrl+F9 it may not work.

C programming code for Windows XP


#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch;
printf("Do you want to shutdown your computer now (y/n)\n");
scanf("%c", &ch);
if (ch == 'y' || ch == 'Y')
system("C:\\WINDOWS\\System32\\shutdown -s");
return 0;
}

strlwr in c
#include <stdio.h>
#include <string.h>
int main()
{
char string[] = "Strlwr in C";
printf("%s\n",strlwr(string));
return

0;

strupr in c
#include <stdio.h>
#include <string.h>
int main()
{
char string[] = "strupr in c";
printf("%s\n",strupr(string));
return
}

0;

You might also like