Programs related to C++ on Code Blocks
![]() |
Pythonpedia |
Programs
related to C++ on Code Blocks
1.Program
to generate Armstrong number?
Program:-
![]() |
Pythonpedia |
Code:-
#include <stdio.h>
int check_armstrong(int);
int power(int, int);
int main () {
int c, a, b;
printf("Input two integers\n");
scanf("%d%d", &a, &b);
for (c = a; c <= b; c++) {
if (check_armstrong(c) == 1)
printf("%d\n", c);
}
return 0;
}
int check_armstrong(int n) {
long long sum = 0, temp;
int remainder, digits = 0;
temp = n;
while (temp != 0) {
digits++;
temp = temp/10;
}
temp = n;
while (temp != 0) {
remainder = temp%10;
sum = sum + power(remainder, digits);
temp = temp/10;
}
if (n == sum)
return 1;
else
return 0;
}
int
power(int n, int r) {
int c, p = 1;
for (c = 1; c <= r; c++)
p = p*n;
return p;
}
Output:-
![]() |
Pythonpedia |
Input two integers
1 1000
1
2
3
4
5
6
7
8
9
153
370
371
407
Process returned 0 (0x0) execution time : 12.266 s
Press any key to continue.
2.Program
to delete vowels from string?
Program:-
![]() |
Pythonpedia |
Code:-
#include <stdio.h>
#include <string.h>
int check_vowel(char);
int main()
{
char s[100], t[100];
int i, j = 0;
printf("Enter a string to delete vowels\n");
gets(s);
for(i = 0; s[i] != '\0'; i++) {
if(check_vowel(s[i]) == 0) {
//not a vowel
t[j] = s[i];
j++;
}
}
t[j] = '\0';
strcpy(s, t); //We are changing
initial string
printf("String after deleting vowels: %s\n", s);
return 0;
}
int check_vowel(char c)
{
switch(c) {
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
return 1;
default:
return 0;
}
}
Output:-
![]() |
Pythonpedia |
Enter a string to delete vowels
Hardwork is a key to success.
String after deleting vowels: Hrdwrk
s ky t sccss.
Process returned 0 (0x0) execution time : 82.677 s
Press any key to continue.oot3
3.Program
to convert number from decimal to binary?
Program:-
![]() |
Pythonpedia |
Code:-
#include <stdio.h>
int main()
{
int n, c, k;
printf("Enter an integer in decimal number system\n");
scanf("%d", &n);
printf("%d in binary number system is:\n", n);
for (c = 31; c >= 0; c--)
{
k = n >> c;
if (k & 1)
printf("1");
else
printf("0");
}
printf("\n");
return 0;
}
Output:-
![]() |
Pythonpedia |
Enter an integer in decimal number
system
214
214 in binary number system is:
00000000000000000000000011010110
Process returned 0 (0x0) execution time : 14.891 s
Press any key to continue.
4.Program
to print Pascal’s triangle?
Program:-
![]() |
Pythonpedia |
Code:-
#include <stdio.h>
long factorial(int);
int main()
{
int i, n, c;
printf("Enter the number of rows you wish to see in pascal
triangle\n");
scanf("%d",&n);
for (i = 0; i < n; i++)
{
for (c = 0; c <= (n - i - 2); c++)
printf(" ");
for (c = 0 ; c <= i; c++)
printf("%ld ",factorial(i)/(factorial(c)*factorial(i-c)));
printf("\n");
}
return 0;
}
long factorial(int n)
{
int c;
long result = 1;
for (c = 1; c <= n; c++)
result = result*c;
return result;
}
Output:-
![]() |
Pythonpedia |
Enter the number of rows you wish to see
in pascal triangle
8
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1
6 15 20 15 6 1
1 7 21 35 35 21 7 1
Process returned 0 (0x0) execution time : 8.844 s
Press any key to continue.
5.Program
to transpose matrix?
Program:-
![]() |
Pythonpedia |
Code:-
#include <stdio.h>
int main()
{
int m, n, c, d, matrix[10][10], transpose[10][10];
printf("Enter the number of rows and columns of matrix\n");
scanf("%d%d", &m, &n);
printf("Enter elements of the matrix\n");
for (c = 0; c < m; c++)
for(d = 0; d < n; d++)
scanf("%d", &matrix[c][d]);
for (c = 0; c < m; c++)
for( d = 0 ; d < n ; d++ )
transpose[d][c] = matrix[c][d];
printf("Transpose of the matrix:\n");
for (c = 0; c < n; c++) {
for (d = 0; d < m; d++)
printf("%d\t", transpose[c][d]);
printf("\n");
}
return 0;
}
Output:-
![]() |
Pythonpedia |
Enter the number of rows and columns of
matrix
2 3
Enter elements of the matrix
1,2
Transpose of the matrix:
1
176
5243536 0
174
9437184
Process returned 0 (0x0) execution time : 16.391 s
Press any key to continue.
-Thank
you
Comments
Post a Comment