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

C Coding2

The document contains C program code snippets to solve common programming problems like checking if a number is a palindrome, generating a multiplication table, determining if a number is prime, calculating factorials, finding the sum of digits, sorting names alphabetically, counting vowels and consonants, adding two matrices, reversing a string, and arranging numbers in ascending order.

Uploaded by

Kshitij Pandit
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

C Coding2

The document contains C program code snippets to solve common programming problems like checking if a number is a palindrome, generating a multiplication table, determining if a number is prime, calculating factorials, finding the sum of digits, sorting names alphabetically, counting vowels and consonants, adding two matrices, reversing a string, and arranging numbers in ascending order.

Uploaded by

Kshitij Pandit
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

//11. Write a C program to check whether given is palindrome or not.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
OUTPUT RESULT:
int n, reversed = 0, remainder, original;
printf("Enter an integer: ");
scanf("%d", &n);
original = n;

// reversed integer is stored in reversed variable


while (n != 0) {
remainder = n % 10;
reversed = reversed * 10 + remainder;
n /= 10;
}
// palindrome if orignal and reversed are equal
if (original == reversed)
printf("%d is a palindrome.", original);
else
printf("%d is not a palindrome.", original);

getch();
}
//12. Write a C program to generate multiplication table of given number.
#include<stdio.h>
#include<conio.h>
OUTPUT RESULT:
void main()
{
clrscr();
int i, num;

/* Input a number to print table */


printf("Enter number to print table: ");
scanf("%d", &num);

for(i=1; i<=10; i++)


{
printf("%d * %d = %d\n", num, i, (num*i));
}
getch();
}
//13. Write a C program to find whether the given number is prime or
composite
#include<stdio.h>
#include<conio.h>
OUTPUT RESULT:
void main()
{
clrscr();
int i,n,c=0;
printf ("Enter a number:");
scanf ("%d",&n);
for (i=1;i<=n;i++)
{
if(n%i==0)
c=c+1;
}
if (c==2)
printf ("The number is PRIME");
else
printf ("The number is COMPOSITE");
getch();
}
//14. Write a C program to find factorial of a given number.
#include<stdio.h>
#include<conio.h>
void main() OUTPUT RESULT:
{
clrscr();
int n, i;
unsigned long long fact = 1;
printf("Enter an integer: ");
scanf("%d", &n);

// shows error if the user enters a negative integer


if (n < 0)
printf("Error! Factorial of a negative number doesn't exist.");
else {
for (i = 1; i <= n; ++i) {
fact *= i;
}
printf("Factorial of %d = %llu", n, fact);
}
getch();
}
//15. Write a C program to find sum of digits of given number.
#include<stdio.h>
#include<conio.h>
OUTPUT RESULT:
void main()
{
clrscr();
int num, sum=0;

/* Input a number from user */


printf("Enter any number to find sum of its digit: ");
scanf("%d", &num);

/* Repeat till num becomes 0 */


while(num!=0)
{
/* Find last digit of num and add to sum */
sum += num % 10;

/* Remove last digit from num */


num = num / 10;
}

printf("Sum of digits = %d", sum);


getch();
}
//16. Write a C program that read name of different students and displays the
name in alphabetical orders.
#include<stdio.h>
#include<conio.h>
OUTPUT RESULT:
#include<string.h>
void main()
{
clrscr();
int i,j,n;
char str[100][100],s[100];
printf("Enter number of names :\n");
scanf("%d",&n);
printf("Enter names in any order:\n");
for(i=0;i<n;i++){
scanf("%s",str[i]);
}
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(strcmp(str[i],str[j])>0){
strcpy(s,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],s);
} } }
printf("\nThe sorted order of names are:\n");
for(i=0;i<n;i++){
printf("%s\n",str[i]);
} getch(); }
//17. Write a C program to count no. of vowels and consonants in the given
text.
#include<stdio.h>
#include<conio.h>
#include <string.h>
void main() OUTPUT RESULT:

{
clrscr();
char s[1000];
int i,vowels=0,consonants=0;
printf("Enter the string : ");
gets(s);
for(i=0;s[i];i++)
{
if((s[i]>=65 && s[i]<=90)|| (s[i]>=97 && s[i]<=122))
{
if(s[i]=='a'||
s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'||s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O'
||s[i]=='U')
vowels++;
else
consonants++;
}}
printf("vowels = %d\n",vowels);
printf("consonants = %d\n",consonants);
getch();
}
//18. Write a C program to add two different matrixes.
#include<stdio.h>
#include<conio.h>
void main() {
clrscr();
int i, j, rows, columns, a[10][10], b[10][10];
int arr[10][10];
printf("\n Please Enter Number of rows and columns : ");
scanf("%d %d", &i, &j);
OUTPUT RESULT:
printf("\n Please Enter the First Elements\n");
for(rows = 0; rows < i; rows++) {
for(columns = 0; columns < j; columns++) {
scanf("%d", &a[rows][columns]);
} }
printf("\n Please Enter the Second Elements\n");
for(rows = 0; rows < i; rows++) {
for(columns = 0; columns < j; columns++) {
scanf("%d", &b[rows][columns]);
} }
for(rows = 0; rows < i; rows++) {
for(columns = 0; columns < j; columns++) {
arr[rows][columns] = a[rows][columns] + b[rows][columns]; } }
printf("\n The Sum of Two a and b = a + b \n");
for(rows = 0; rows < i; rows++) {
for(columns = 0; columns < j; columns++) {
printf("%d \t ", arr[rows][columns]); }
printf("\n"); }
getch();
}
//19. Write a C program to reversed given string.
#include<stdio.h>
#include<conio.h> OUTPUT RESULT:
#include <string.h>
void main()
{
clrscr();
char str[40]; // declare the size of character string
printf (" \n Enter a string to be reversed: ");
scanf ("%s", str);

// use strrev() function to reverse a string


printf (" \n After the reverse of a string: %s ", strrev(str));
getch();
}
//20. Write a C Program to arrange 10 numbers in ascending order.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,a,n=5,number[15];
printf ("\nPlease enter how many numbers you want to sort:");
scanf ("%d", &n);
printf ("\nPlease enter the numbers to be sorted as ascending order:");
for (i=0; i<n; ++i)
OUTPUT RESULT:
scanf ("%d",&number[i]);
for (i=0; i<n; ++i) {
for (j=i+1; j<n; ++j) {
if (number[i] > number[j])
{
a= number[i];
number[i] = number[j];
number[j] = a;
} } }
printf ("\nAscending order of entered numbers");
for (i=0; i<n; ++i)
printf ("\n%d",number[i]);
}
getch();
}

You might also like