C and C++ Coding Question For Freshers
Q1. Write a Program to Reverse a Number in C
Hint : If a user enter Number 158 , then after reversing Number Program should give output as 851.
#include <stdio.h>
main()
{
int n, rev=0, mod;
printf(“Enter the Number\n”);
scanf(“%d”,&n);
while(n>0)
{
mod= n % 10;
rev= (rev*10) + mod;
n = n/10;
}
printf(” the number reverses is %d\n”,rev);
}
Q2. Write a Program to Check Whether a Number is Armstrong or Not in C .
Hint : If a user enter Number 371 , then Program should give output as “Yes It is an Armstrong Number”.
#include <stdio.h>
int main()
{
int n, a, b=0, temp;
printf(“Enter the Number \n”);
scanf(“%d”,&n);
temp = n;
while(n>0)
{
a= n % 10;
b= b + a*a*a;
n= n/10;
}
if(b==temp)
printf(“It is a armstrong Number”);
else
printf(” It is a not a armstrong Number”);
return 0;
}
Q3. Write a Program to Find Ascii Value of Character in C.
#include <stdio.h>
int main()
{
char ch;
printf(“enter the character\n”);
scanf(“%c”,&ch);
if(ch>=’a’ && ch<=’z’ ||ch>=’A’ && ch<=’Z’)
{
printf(“The ascii value of Character is %d”, ch);
}
return 0;
}
Q4. Write a Program to Check Whether a Character is Vowel Or not.
Hint: Vowels are A,E,I,O,U Or a,e,i,o,u.
#include <stdio.h>
int main()
{
char ch;
printf(“enter the character\n”);
scanf(“%c”,&ch);
if (ch==’a’||ch==’A’|| ch==’e’||ch==’E’ ||ch==’i’|| ch==’I’ || ch==’o’||ch==’O’||ch==’u’||ch==’U’)
printf(“%c is a vowel”,ch);
else
printf(“Not a Vowel”);
return 0;
}
Q5. Write a Program to Generate Fibonacci series.
Hint: Fibonacci Series is 0,1,1,2,3,5,8,13 etc
#include <stdio.h>
main()
{
int c,n,i;
int a=0;
int b=1;
printf(“Enter the Range of Fibonacci Series\n”);
scanf(“%d”,&n);
printf(“The fibonacci series are\n”);
printf(“%d %d”,a,b);
for(i=0;i<n;i++)
{
c=a+b;
a=b;
b=c;
printf(“%d”,c);
}
}
Q6. Write a Program to find its Factorial.
Hint: Factorial of 5 is 120.
#include <stdio.h>
int main()
{
int number,i;
int fact=1;
printf(“Enter the Number to find its Factorial\n”);
scanf(“%d”,&number);
for(i=1;i<=number;i++)
{
fact=fact*i;
}
printf(“The Factorial is %d”, fact);
return 0;
}
Q7.Write a Program to find Whether a year is Leap Year or Not.
Hint: Like 2000,2004,2008 all are Leap Year but 2015 is not a Leap year.
#include <stdio.h>
int main()
{
int year;
printf(“Enter Year to check whether it is a leap year or Not\n”);
scanf(“%d”, &year);
if(year%400==0)
{
printf(“%d Is Leap year”, year);
}
else if(year%100==0)
{
printf(“%d Is Not a Leap year”, year);
}
else if(year%4==0)
{
printf(“%d Is Leap year”, year);
}
else
printf(“%d Is Not a Leap year”, year);
return 0;
}
Q8. Write a Program to Check Whether a String is Palindrome or Not.
Hint: Name ‘nitin” is Palindrome or “ala” is Palindrome.
#include <stdio.h>
#include <string.h>
int main(){
char string[20];
int i, length;
int flag = 0;
printf(“Enter a string:”);
scanf(“%s”, string);
length = strlen(string);
for(i=0;i < length ;i++){
if(string[i] != string[length-i-1]){
flag = 1;
break;
}
}
if (flag==1) {
printf(“%s is not a palindrome”, string);
}
else {
printf(“%s is a palindrome”, string);
}
return 0;
}
Q9. Write a Program to check whether a Number is Odd or Even.
Hint: Number 2 is Even and 3 is odd.
#include <stdio.h>
int main() {
int a;
printf(“Enter a: “);
scanf(“%d”, &a);
if (a % 2 == 0) {
printf(“The given number is EVEN”);
}
else {
printf(“The given number is ODD”);
}
return 0;
}
Q10. Write a Program to Concatenate Two String Using strcat in C.
#include <stdio.h>
#include <string.h>
int main()
{
char a[50], b[50];
printf(“Enter the first string\n”);
gets(a);
printf(“Enter the second string\n”);
gets(b);
strcat(a,b);
printf(“String obtained on concatenation is %s\n”,a);
return 0;
}