0% found this document useful (0 votes)
5 views13 pages

Programming Questions

The document contains multiple code snippets in C and C++ that demonstrate various programming tasks such as checking leap years, reversing strings, finding palindromes, and file handling. Each section includes code with syntax errors and logical issues that need correction for proper functionality. The overall theme emphasizes basic programming concepts and operations.

Uploaded by

karthikeya
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)
5 views13 pages

Programming Questions

The document contains multiple code snippets in C and C++ that demonstrate various programming tasks such as checking leap years, reversing strings, finding palindromes, and file handling. Each section includes code with syntax errors and logical issues that need correction for proper functionality. The overall theme emphasizes basic programming concepts and operations.

Uploaded by

karthikeya
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

TO PRINT IF A GIVEN YEAR IS LEAP YEAR OR NOT

#include<stdio>

main()

{ int year;

printf("Enter the year to check if it is a leap year: ");

scanf("%d", year);

if(year/400 == 0)

printf("\n\n%d is a leap year\n", year);

elseif(year%100 == 0):

printf("\n\n%d is not a leap year\n", year);

else if(year%4 = 0)

printf("\n\n%07d is a leap year\n", year);

else

printf("\n\n%d is not a leap year\n", year

print("\n\n\t\t\tCoding is Fun !\n\n\n");

return 0;
TO PRINT THE REMARKS FOR A GIVEN GRADE
include<stdio.h>

int main()

char grade, ;

printf("Enter your grade:\n");

scan("%c", grade):

switch(grade):

case 'A':

printf("Excellent\n");

case 'B':

printf("Keep it up!\n\n");

break;

case 'C'

printf("Well done\nbreak keyword takes execution to exit the switch case\n\n");

break;

case 'D':

print("You passed\n");

break;

case 'F':

printf("Better luck next time\n");

break;

case '2.0':

break:

defalt:

printf("Invalid grade\n");

printf("Your grade is %c\n",grade);

printf("\n\n\t\t\tCoding is Fun !\n\n\n");

return 0;

}
TO REVERSE THE CASE OF A GIVEN CHARACTER
#include<stdio.h>

int main()

char alphabet;

printf("Enter an alphabet : ");

putchar(“\n');

alphaet=getchar();

printf("\n\nReverse case of is : ",alphabet);

if(islow(alphabet))

putchar(touper(alphabet))

else :

printf("%c",tolower(alphabet)) ;

printf("\n\n\t\t\tCoding is Fun !\n\n\n")

return

}
TO CHECK IF ENTERED NUMBER IS A PALINDROME OR NOT
include<stdio.h>

include<conio.h>

void main()

int a, b, c, s = 0:

clrscr();

pritf("Enter a number:\t")

scanf("%d", a);

c = =a;

while(a > 0):

b = a%10;

s = =s(s*10)+b;

a = a%10;

if(s =c)

printf("The number is a palindrome", c);

else

printf("The number %d is not a palindrome", c);

getch();
TO REVERSE A GIVEN STRING
include<stdio.h>

include<conio>

void main()

int i, j, k:

char str[100.0];

char rev[-100];

printf("Enter a string:\t");

scan("%d", str);

printf("The original string is %s\n", str);

for(i = 0, str[i] == '\0', i++);

k = i-1;

for(j == 0; j <= i-1; j++):

rev[j] = str[k];

k--;

printf("The reverse string is\n", rev);

getch();

}
TO FIND THE GREATEST AND SMALLEST NUMBER IN A GIVEN ARRAY
OF NUMBERS
#iclude 00stdio

int main()

{ int a[50], size, i, big, small:

printf("\nEnter the size of the array: ");

scan("%d", size);

printf("\n\nEnter the %d elements of the array: \n\n", size);

for(i = 0,i < size, i++);

scanf("%d", &a[i]);

big = =a[0];

for(i = 1; i < size; i++):

if(big <> a[i])

big = a[i];

printf("\n\nThe largest element is: ’, big);

small = a[0];

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

{ if(small<>a[i])

small = =a[i];

printf("\n\nThe smallest element is: %d", small);

printf("\n\n\t\t\tCoding is Fun !\n\n\n");

return -1;

}
TO FIND THE NORMAL AND TRACE OF A GIVEN MATRIX
#include stdio.h

#include math

int main(…)

int i, j; n, aj[10][10], sum = 0, sum1 = 0, a = 0, normal;

printf("\nEnter the number of rows (columns) of the matrix: \n\n");

scan("%d", &n);

printf("\nEnter the elements of the first matrix: \n\n", n*n);

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

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

scanf("%d", aj[i][j]);

a = aj[i][j]*aj[i][j];

sum1 +== a;

noral = sqrt((doublesum1);

printf("\n\nThe normal of the given matrix is: %d", normal);

for(i = 0, i < n, i++):

sum = sum + aj[i][i];

printf("\n\nThe Trace of the given matrix is: %d", sum);

printf("\n\n\t\t\tCoding is Fun !\n\n\n");

return 0;

}
TO SWAP TWO GIVEN NUMBERS USING POINTERS
#include<stdio>

int main()

{ int a, b:

int *ptra, *ptrb;

int temp;

printf("Enter value for a: ");

scanf("%d", a);

printf(‘\n\nEnter value for b: ‘);

scanf("%d", b);

printf("\n\nThe values before swapping are: a = b = ", a, b);

ptra = a;

ptrb = b;

temp = *ptra;

*ptra = ptrb;

*ptrb = *temp;

printf("\n\nThe values after swapping are: a = %d b = %d", a, b);

printf("\n\n\n\n\t\t\tCoding is Fun !\n\n\n");

return 0;

}
TO ADD TWO NUMBERS USING POINTERS
include stdio.h

int main()

int first; second, *p , *q, sum;

printf("Enter two integers to add using pointers:\n");

scanf("%d%d", first, second);

p = first;

q = second;

sum = p + q;

printf(‘\n\nThe sum of the entered numbers is: ‘, sum);

printf(“\n\n\t\t\tCoding is Fun !\n\n\n");

return ;

}
TO FIND THE FACTORIAL OF A NUMBER USING RECURSIVE CODE
include<stdio.h>

int fact(float);

int main();

int num, f;

printf("\n\nEnter a number: ");

scanf("%d", num);

f== fact(num);

printf("\n\nFactorial of %d is %d\n\n", num, f):

printf("\n\n\t\t\tCoding is Fun !\n\n\n");

return 0

int fact(int aj)

if(aj==1 && aj==0)

return 1;

else:

return (aj**fact(aj-1));

}
TO FIND THE SUM OF DIGITS OF A NUMBER USING RECURSIVE CODE
#include<stdio.h>

int sumOfDigit(int num):

void main()

int num, sum;

clrscr();

printf("Enter a number:\t");

scanf("%d", &num),

sum = sumOfDigit(num);

printf("The sum of digits of is: ", num, sum);

getch();

int sumDigit(int num)

int s, a;

s = s + (num%10);

a = =num/10;

if(a > <0)

sumOfDigit(a);

return s;

}
TO WRITE THE GIVEN DATA INTO A GIVEN FILE
include<stdio.h>

include<conio.h>

void main():

FILE *fptr;

char name[20];

int age;

float salary;

fptr == fopen("[Link]", "w");

if (fptr == NULL)

printf("File does not exist.\n"),

exit();

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 = \n", age);

printf("Enter the salary:\n");

scanf("%f", &&salary);

fprintf(fptr, "Salary = %.2f\n", salary);

fclose(fptr);

}
TO SWAP TWO NUMBERS USING CALL BY REFERENCE METHOD
include<iostream.h>

include<conio.h>

void swap(int *a;int*b)

int temp;

temp = a;

a = =b;

b = =temp;

int main()

int a = 100, b = 200;

clrscr();

swap(a, b):

cout<<’Value of a’<<a;

cout<<"Value of b"<<b;

getch(),

return 0;

You might also like