C language Question
#include <stdio.h>
int main()
{
int n, i;
printf("Enter a number: ");
scanf("%d", &n);
printf("Multiplication table of %d:\n ", n);
for (i = 1; i <= 10; i++)
printf("%d x %d = %d\n", n, i, n * i);
return 0;
}
#include <stdio.h>
void main() {
int i, n, sum = 0;
printf("enter number of terms : ");
scanf("%d", &n);
printf("\nThe odd numbers are :");
for (i = 1; i <= n; i++)
{
printf("%d ", 2 * i - 1);
sum += 2 * i - 1;
}
printf("\nThe Sum of odd Natural Number upto %d terms : %d \n", n, sum);
}
#include <stdio.h>
void main() {
int i, j, rows, k = 1;
printf("Input number of rows : ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; j++)
printf("%d ", k++);
printf("\n");
}
}
#include <stdio.h>
int main()
{
int arr1[100];
int i, mx, mn, n;
printf("number of elements to be stored in the array :");
scanf("%d", &n);
printf("enter %d elements in the array :\n", n);
for (i = 0; i < n; i++)
{
printf("element - %d : ", i);
scanf("%d", &arr1[i]);
}
mx = arr1[0];
mn = arr1[0];
for (i = 1; i < n; i++)
{
if (arr1[i] > mx)
mx = arr1[i];
if (arr1[i] < mn)
mn = arr1[i];
}
printf("Maximum element is : %d\n", mx);
printf("Minimum element is : %d\n\n", mn);
return 0;
}
#include <stdio.h>
int main()
{
int i, j, a, n, number[30]={9,8,7,6,5,4,3,2,1};
n=9;
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("The numbers arranged in ascending order \n");
for (i = 0; i < n; ++i)
printf("%d ", number[i]);
}
#include <stdio.h>
int main() {
int arr1[3][3], i, j;
printf("Input elements in the matrix :\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("element - [%d][%d] : ", i, j);
scanf("%d", &arr1[i][j]);
}
}
printf("\nThe matrix is : \n");
for (i = 0; i < 3; i++) {
printf("\n");
for (j = 0; j < 3; j++)
printf("%d\t", arr1[i][j]);
}
printf("\n\n");
return 0;
}
#include <stdio.h>
int main()
{
int i;
int data[5];
printf("Enter elements: ");
for (i = 0; i < 5; ++i)
scanf("%d", &data[i]);
printf("You entered: ");
for (i = 0; i < 5; ++i)
printf("%d ", *(data + i));
return 0;
}
#include <stdio.h>
int main()
{
char str[100], * ptr;
int count;
printf("Enter any string: ");
gets(str);
ptr = str;
count = 0;
while ( *ptr != '\0')
{
count++;
ptr++;
}
printf("The length of the string is: %d", count);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main() {
int i,n;
int *data;
printf("Enter the total number of elements: ");
scanf("%d", &n);
data = (int *)calloc(n, sizeof(int));
if (data == NULL) {
printf("memory not allocated.");
exit(0);
}
for (i = 0; i < n; ++i) {
printf("Enter number%d: ", i + 1);
scanf("%d", data + i);
}
for ( i = 1; i < n; ++i) {
if (*data < *(data + i)) {
*data = *(data + i);
}
}
printf("Largest number = %d ", *data);
free(data);
return 0;
}
#include <stdio.h>
void swap(int *, int *);
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b);
swap(&a,&b);
printf("After swapping values in main a = %d, b = %d\n",a,b);
return 0;
}
void swap (int *a, int *b)
{
int temp;
temp = *a;
*a=*b;
*b=temp;
printf("After swapping values in function a = %d, b = %d\n",*a,*b);
}
#include <stdio.h>
int main()
{
char str[150];
char *p;
int vCnt=0,cCnt=0;
printf("Enter only alphabet without space: ");
gets(str);
p=str;
while(*p!='\0')
{
if(*p=='A' ||*p=='E' ||*p=='I' ||*p=='O' ||*p=='U'
||*p=='a' ||*p=='e' ||*p=='i' ||*p=='o' ||*p=='u')
vCnt++; //vowel count
else
cCnt++; //consonant count
p++;
}
printf("Number of Vowels in String: %d\n",vCnt);
printf("Number of Consonants in String: %d",cCnt);
return 0;
}
#include <stdio.h>
struct book{
char title[10];
double price;
int pages;
};
int main(){
struct book b1 = {"Learn C", 75.50, 325};
struct book *strptr;
strptr = &b1;
printf("Title: %s\n", strptr -> title);
printf("Price: %lf\n", strptr -> price);
printf("No of Pages: %d\n", strptr -> pages);
return 0;
}
#include <stdio.h>
int main() {
char str1[50];
char revstr[50];
char *stptr = str1;
char *rvptr = revstr;
int i = -1;
printf(" Input a string : ");
scanf("%s", str1);
//length of the string
while (*stptr) {
stptr++;
i++;
}
while (i >= 0) {
stptr--;
*rvptr = *stptr;
rvptr++;
--i;
}
*rvptr = '\0';
printf(" Reverse of the string is : %s\n\n", revstr);
return 0;
}
#include <stdio.h>
int fact(int);
void main()
{
int sum;
sum=fact(1)/1 + fact(2)/2 + fact(3)/3 + fact(4)/4 + fact(5)/5;
printf("\n\n Function : find the sum of 1!/1+2!/2+3!/3+4!/4+5!/5 :\n");
printf("The sum of the series is : %d\n\n",sum);
}
int fact(int n)
{
int num=0,f=1;
while(num<=n-1)
{
f = f+f*num;
num++;
}
return f;
}
#include <stdio.h>
void decToBinary(int n)
{
int binaryNum[32]; int main()
{
int i = 0; int n = 17;
int j; decToBinary(n);
while (n > 0) {
// storing remainder in binary array return 0;
binaryNum[i] = n % 2; }
n = n / 2;
i++;
}
for (j = i - 1; j >= 0; j--)
printf("%d", binaryNum[j]);
}
#include <stdio.h>
int checkArmstrong(int n1);
int checkPerfect(int n1);
int main()
{
int n1;
printf(" Input any number: ");
scanf("%d", &n1);
if(checkArmstrong(n1))
{
printf(" The %d is an Armstrong number.\n", n1);
}
else
{
printf(" The %d is not an Armstrong number.\n", n1);
}
if(checkPerfect(n1))
{
printf(" The %d is a Perfect number.\n\n", n1);
}
else
{
printf(" The %d is not a Perfect number.\n\n", n1);
}
return 0;
}
// Checks whether a three digits number is Armstrong number or not.
//An Armstrong number is an n-digit number that is equal
//to the sum of the n-th powers of its digits.
int checkArmstrong(int n1)
{
int ld, sum, num;
sum = 0;
num = n1; // Checks whether the number is perfect number or not.
while(num!=0) //a perfect number is a positive integer that is equal to
{ //the sum of its positive divisors excluding the number itself
ld = num % 10; int checkPerfect(int n1)
sum += ld * ld * ld; {
num = num/10; int i, sum, num;
} sum = 0;
return (n1 == sum); num = n1;
} for(i=1; i<num; i++)
{
if(num%i == 0)
{
sum += i;
}
}
return (n1 == sum);
}
#include <stdio.h>
int largest(int arr[], int n)
{
int i;
int max = arr[0];
for (i = 1; i < n; i++)
if (arr[i] > max)
max = arr[i];
return max;
}
int main()
{
int arr[] = { 10, 324, 45, 90, 9808 };
int n = sizeof(arr) / sizeof(arr[0]);
printf("Largest in given array is %d", largest(arr, n));
return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
char str[100];
int l=0;
printf("Input the string : ");
gets(str);
l=strlen(str);
printf("The characters of the string in reverse are : \n");
for(str[l]='\0';l>=0;l--)
{
printf("%c ", str[l]);
}
printf("\n");
return 0;
}
#include <stdio.h>
#include <string.h>
void main()
{
char s[200];
int count = 0, i;
printf("Enter the string:\n");
scanf("%[^\n]s", s);
for (i = 0;s[i] != '\0';i++)
{
if (s[i] == ' ' && s[i+1] != ' ')
count++;
}
printf("Number of words in given string are: %d\n", count + 1);
}
#include <stdio.h>
void compareStrings(char* x, char* y)
{
int flag = 0;
// Iterate a loop till the end
// of both the strings
while (*x != '\0' || *y != '\0') {
int main(void) if (*x == *y) {
{ x++;
char s1[20] = "dsa"; y++;
}
char s2[20] = "dsa";
// If two characters are not same
compareStrings(s1, s2); // print the difference and exit
return 0; else if ((*x == '\0' && *y != '\0')
|| (*x != '\0' && *y == '\0')
} || *x != *y) {
flag = 1;
printf("Unequal Strings\n");
break;
}
}
// If two strings are exactly same
if (flag == 0) {
printf("Equal Strings\n");
}
}
#include <stdio.h>
void main()
{
FILE *fptr;
char name[20];
int age;
//open for writing
fptr = fopen("file.txt", "w");
if (fptr == NULL)
{
printf("File does not exists \n");
return;
}
printf("Enter the name \n");
scanf("%s", name);
fprintf(fptr, "Name = %s\n", name);
printf("Enter the age\n");
scanf("%d", &age);
fprintf(fptr, "Age = %d\n", age);
fclose(fptr);
}
#include <stdio.h>
int main()
{
FILE *fileptr;
int count_lines = 0;
char filechar[40], chr;
printf("Enter file name: ");
scanf("%s", filechar);
fileptr = fopen(filechar, "r");
chr = getc(fileptr);
while (chr != EOF)
{
if (chr == '\n')
count_lines = count_lines + 1;
chr = getc(fileptr);
}
fclose(fileptr);
printf("There are %d lines in %s in a file\n", count_lines, filechar);
return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
FILE* ptr;
char ch;
ptr = fopen("file.txt", "r");
if (NULL == ptr) {
printf("file can't be opened \n");
}
printf("content of this file are \n");
do {
ch = fgetc(ptr);
printf("%c", ch);
} while (ch != EOF);
fclose(ptr);
return 0;
}
Thank You!!