0% found this document useful (0 votes)
4 views

C basic Programs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

C basic Programs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

1.

Find particular character position from the string


//c program specifying the particular char position from the string
#include<stdio.h>
#include<string.h>
void main()
{
char str[100],c;int i,lenf=0;
printf("\nenter a string:");
gets(str);
len=strlen(str);
printf("\nenter a character to find its position:");
scanf("%c",&c);
for(i=0;i<len;i++)
{
if(str[i]==c)
{
printf("character position:\n%d",i+1);
f=1;
}
}
if(f==0)
{
printf("\ncharacter not found");
}
}
2. Floyds Triangle
C program to print Floyd's triangle: A user will input how many numbers of rows of Floyd's triangle to print. First four rows of
Floyd's triangle are:
1
23
456
7 8 9 10

1. #include <stdio.h>
2.
3. int main()
4. {
5. int n, i, c, a = 1;
6.
7. printf("Enter the number of rows of Floyd's triangle to print\n");
8. scanf("%d", &n);
9.
10. for (i = 1; i <= n; i++)
11. {
12. for (c = 1; c <= i; c++)
13. {
14. printf("%d ",a);
15. a++;
16. }
17. printf("\n");
18. }
19.
20. return 0;
21. }
3. Pascal triangle C program
It's four rows are:

1
1 1
1 2 1
1 3 3 1

#include <stdio.h>
int main()
{
int rows, coef = 1, space, i, j;

printf("Enter number of rows: ");


scanf("%d",&rows);

for(i=0; i<rows; i++)


{
for(space=1; space <= rows-i; space++)
printf(" ");

for(j=0; j <= i; j++)


{
if (j==0 || i==0)
coef = 1;
else
coef = coef*(i-j+1)/j;

printf("%4d", coef);
}
printf("\n");
}

return 0;
}
4. C program to delete duplicate elements from
an array
1.#include<stdio.h>
2.#include<conio.h>
3.void main()
4.{
5. int a[20], i, j, k, n;
6. clrscr();
7.
8. printf("\nEnter array size: ");
9. scanf("%d", &n);
10.
11. printf("\nEnter %d array element: ", n);
12. for(i = 0; i < n; i++)
13. {
14. scanf("%d", &a[i]);
15. }
16.
17. printf("\nOriginal array is: ");
18. for(i = 0; i < n; i++)
19. {
20. printf(" %d", a[i]);
21. }
22.
23. printf("\nNew array is: ");
24. for(i = 0; i < n; i++)
25. {
26. for(j = i+1; j < n; )
27. {
28. if(a[j] == a[i])
29. {
30. for(k = j; k < n; k++)
31. {
32. a[k] = a[k+1];
33. }
34. n--;
35. }
36. else
37. {
38. j++;
39. }
40. }
41. }
42.
43. for(i = 0; i < n; i++)
44. {
45. printf("%d ", a[i]);
46. }
47. getch();
48. }

5. Program to print half pyramid using alphabets


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

#include <stdio.h>
int main()
{
int i, j;
char input, alphabet = 'A';

printf("Enter the uppercase character you want to print in last row: ");
scanf("%c",&input);

for(i=1; i <= (input-'A'+1); ++i)


{
for(j=1;j<=i;++j)
{
printf("%c", alphabet);
}
++alphabet;

printf("\n");
}
return 0;
}
6. Program to check if input Number is int or float
1. #include<stdio.h>
2.
3. #include<conio.h>
4. #include<string.h>
5.
6. int main()
7. {
8. char number[10];
9. int flag = 0;
10. int length, i = 0;
11.
12. printf("\n\nEnter a number: ");
13. scanf("%s", number);
14.
15. length = strlen(number);
16.
17. // till string does not end
18. while(number[i++] != '\0') // same as while(length-->0)
19. {
20. if(number[i] == '.') // decimal point is present
21. {
22. flag = 1;
23. break;
24. }
25. }
26.
27. // if(0) is same as if(false)
28. if(flag)
29. printf("\n\n\n\tEntered Number is a Floating point Number\n\
n");
30. else
31. printf("\n\n\n\tEntered Number is a integer Number\n\n");
32.
33. printf("\n\n\n\n\t\t\tCoding is Fun !\n\n\n");
34. return 0;
35. }
7. Armstrong number
1.for(i = 1; i <= 500; i++)
2. {
3. t = i; // as we need to retain the original number
4. sum = 0;
5. while(t != 0)
6. {
7. a = t%10;
8. sum += a*a*a;
9. t = t/10;
10. }
11.
12. if(sum == i)
13. printf("\n\t\t\t%d", i);
14. }

You might also like