C Programming
C Programming
C Programming
Multiplication Table
#include <stdio.h>
int main() {
int num, i = 1;
printf(" Enter any Number:");
scanf("%d", &num);
printf("Multiplication table of %d: ", num);
while (i <= 10) {
printf(" %d x %d = %d", num, i, num * i);
i++;
}
return 0;
}
2.Pyramid
# include<iostream.h>
# include <conio.h>
main()
{
int space=10; \\ to print the pyramid in center, you can also increase the # of
spaces
for (int i=0;i<=5;i++)
{
for (int k=0;k<space;k++)
{
cout<<" ";
}
for (int j=0;j<2*i-1;j++)
{
cout<<"*";
}
space--;
cout<<endl;
}
getch();
}
3.Swapping Two Numbers
#include <stdio.h>
int main() {
int a, b, temp;
printf("Enter the value of a and b: ");
scanf("%d %d", &a, &b);
printf("Before swapping a=%d, b=%d ", a, b);
/*Swapping logic */
temp = a;
a = b;
b = temp;
printf("After swapping a=%d, b=%d", a, b);
return 0;
}
4.Fibonnaci series
#include<stdio.h>
int main() {
//array fib stores numbers of fibonacci series
int i, fib[25];
//initialized first element to 0
fib[0] = 0;
//initialized second element to 1
fib[1] = 1;
//loop to generate ten elements
for (i = 2; i < 10; i++) {
//i'th element of series is equal to the sum of i-1'th element and i-2'th
element.
fib[i] = fib[i - 1] + fib[i - 2];
}
printf("The fibonacci series is as follows ");
//print all numbers in the series
for (i = 0; i < 10; i++) {
printf("%d ", fib[i]);
}
return 0; }
5.C Ask Number from 1 to 9
#include<stdio.h>
int getnumber();
int main() {
int input = 0; //call a function to input number from key board
input = getnumber();
11.Staring pyramid in c
#include <stdio.h>
int main() {
int rows, star, spaces;
int number_of_stars = 6;
int number_of_rows = number_of_stars;
#include <stdio.h>
int main()
{
int n, c, k, space = 1;
space = n - 1;
space--;
printf("\n");
}
space = 1;
space++;
printf("\n");
}
return 0;
}
13.Generate ArmStrong Number
#include<stdio.h>
#include<conio.h>
main()
{
int r;
long number = 0, c, sum = 0, temp;
printf("Enter the maximum range upto which you want to find armstrong numbers ");
scanf("%ld",&number);
getch();
return 0;
}
14.Find Armstrong Number
#include <stdio.h>
main()
{
int number, sum = 0, temp, remainder;
printf("Enter a number\n");
scanf("%d",&number);
temp = number;
while( temp != 0 )
{
remainder = temp%10;
sum = sum + remainder*remainder*remainder;
temp = temp/10;
}
if ( number == sum )
printf("Entered number is an armstrong number.");
else
printf("Entered number is not an armstrong number.");
return 0;
}
15.Floyed’s triangle
#include <stdio.h>
int main()
{
int n, i, c, a = 1;
return 0;
}
16.Pascal Triangle in C
#include<stdio.h>
long factorial(int);
main()
{
int i, n, c;
printf("\n");
}
return 0;
}
long factorial(int n)
{
int c;
long result = 1;
return ( result );
}
17.Bubblesort in c
#include <stdio.h>
int main()
{
int array[100], n, c, d, swap;
return 0;
}
18.HCF LCM in C
#include <stdio.h>
int main() {
int a, b, x, y, t, gcd, lcm;
a = x;
b = y;
while (b != 0) {
t = b;
b = a % b;
a = t;
}
gcd = a;
lcm = (x*y)/gcd;
return 0;
}
#include<stdio.h>
#include<string.h>
main()
{
char a[100];
int length;
length = strlen(a);
return 0;
}
20.String comparision using strcmp
#include<stdio.h>
#include<string.h>
main()
{
char a[100], b[100];
if( strcmp(a,b) == 0 )
printf("Entered strings are equal.\n");
else
printf("Entered strings are not equal.\n");
return 0;
}
21.String comparision without strcmp
int compare(char a[], char b[])
{
int c = 0;
main()
{
char a[100], b[100];
strcat(a,b);
getch();
return 0;
}
23.Programme to check leap year
#include <stdio.h>
int main()
{
int year;
if ( year%400 == 0)
printf("%d is a leap year.\n", year);
else if ( year%100 == 0)
printf("%d is not a leap year.\n", year);
else if ( year%4 == 0 )
printf("%d is a leap year.\n", year);
else
printf("%d is not a leap year.\n", year);
return 0;
}
24.Check weather input alphabet is vowel or not
#include <stdio.h>
main()
{
char ch;
printf("Enter a character\n");
scanf("%c", &ch);
return 0;
}
25.Insertion sort in c
* insertion sort ascending order */
#include <stdio.h>
int main()
{
int n, array[1000], c, d, t;
d--;
}
}
return 0; }
26.Selection sort in c
#include<stdio.h>
main()
{
int array[100], n, c, d, position, swap;
return 0;
}
27.Sort string in C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void sort_string(char*);
main()
{
char string[100];
sort_string(string);
printf("%s\n", string);
return 0;
}
length = strlen(s);
result = (char*)malloc(length+1);
pointer = s;
strcpy(s, result);
free(result);
}
28.strlwr in c
#include<stdio.h>
#include<string.h>
main()
{
char string[] = "Strlwr in C";
printf("%s\n",strlwr(string));
return 0;
}
29.Strupr in c
#include<stdio.h>
#include<string.h>
main()
{
char string[] = "strupr in c";
printf("%s\n",strupr(string));
return 0;
}
#include<stdio.h>
#include<conio.h>
#include<dos.h>
main()
{
struct date d;
getdate(&d);
main()
{
char string[100], *pointer;
int position, length;
printf("Enter a string\n");
gets(string);
free(pointer);
return 0;
}
pointer = malloc(length+1);
if (pointer == NULL)
{
printf("Unable to allocate memory.\n");
exit(EXIT_FAILURE);
}
*(pointer+c) = '\0';
return pointer;
}
32.Swap two String
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<conio.h>
main()
{
char first[100], second[100], *temp;
printf("\nBefore Swapping\n");
printf("First string: %s\n",first);
printf("Second string: %s\n\n",second);
temp = (char*)malloc(100);
strcpy(temp,first);
strcpy(first,second);
strcpy(second,temp);
printf("After Swapping\n");
printf("First string: %s\n",first);
printf("Second string: %s\n",second);
getch();
return 0;
}
33.Reverse String
#include<stdio.h>
#include<string.h>
main()
{
char arr[100];
strrev(arr);
return 0;
}
34.Shutdown pc in widows XP
#include <stdio.h>
#include <stdlib.h>
main()
{
char ch;
return 0;
}
35.Shutdown pc in widows7
#include <stdio.h>
#include <stdlib.h>
main()
{
char ch;
return 0;
}
#include <stdio.h>
int main() {
system("shutdown -P now");
return 0;
}
37.The remainder (%) operator
#include <stdio.h>
int
main (void)
{
int a, b, c, d;
/* a few operations */
a = 10 % 3;
b = -10 % 3;
c = 10 % -3;
d = -10 % -3;
return (0);
}
int
main (void)
{
int a, b, c;
double x, y, z, w;
a = 10; b = 20;
return (0); }
39. Functions from the math library
#include <stdio.h>
#include <math.h>
#define PI 3.1416
int
main (void)
{
double before, after, x, y, z;
return (0);
}
#include <stdio.h>
int
main (void)
{
char c;
return (0);
}
41. The (-)unary operator
#include <stdio.h>
int
main (void)
{
int a, b, c;
a = 10; b = 20;
/* b is 20 so -b is -20 */
b = -b + a;
return (0);
}
42. Increment (++) and decrement (--) */
#include <stdio.h>
int
main (void)
{
int a, b;
a = 5;
/* increment (++) */
/* a is incremented by 1 */
++a;
printf ("After ++a, a is now %d\n", a);
/* decrement (--) */
/* a is decremented by 1 */
--a;
printf ("After --a, a is now %d\n", a);
return (0);
}
43. The random [rand()] function */
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int
main (void)
{
/* initialize random generator */
srand (time(NULL));
return (0);
}
int
main (void)
{
/* c and d are pointers to integers */
int a, b, *c, *d, e;
a = 10;
b = a * 3;
c = &a; /* address of a goes into c */
d = &b; /* address of b goes into d */
e = *c + *d; /* *c is a and *d is b */
*d = a;
d = &a;
*c = *d - a % b + *c;
return (0);
}
45.just a silly program playing with pointers */
#include <stdio.h>
int
main (void)
{
/* a and e are integers */
int a, e;
/* b is a pointer to an integer */
int* b;
return (0);
}
int
main (void)
{
int time;
/* the loop */
for (time=60; time>=0; time=time-1)
{
printf ("%d ", time);
}
printf ("\n\nLIFTOFF!\n\n");
return(0);
}
47. A user-defined function without argument nor
result */
#include <stdio.h>
/* main program */
int
main (void)
{
printf ("The line of stars comes from a function.\n");
return (0);
}
48. A void function with one argument */
#include <stdio.h>
int
main (void)
{
int a;
a=10;
stars2 (20);
stars2 (a);
stars2 (a+2);
return (0);
}
49. The factorial function: one argument, one result
#include <stdio.h>
product = 1; /* initialization */
/* computes n*n-1... */
for (i=n; i>1; i=i-1)
{
product = product * i;
}
int
main (void)
{
int a, result;
return (0);
}
50. Problem: Write a program that prints out all the
prime numbers between 1 and 1000. */
#include <stdio.h>
#include <math.h>
int divisor, i;
/* looking for a divisor. if found, it
is not a prime number */
divisor = 0;
int
main (void)
{
int x;
return (0);
}
51. Function finding prime numbers (no pointers)
#include <stdio.h>
#include <math.h>
divisor = 0;
int
main (void)
{
int x;
return (0);
}
int
main (void)
{
char city[20], city2[20];
return (0);
}