1. C programto print hello world without using semicolon
#include<stdio.h>
int main()
{
if(printf("hello world"))
{
}
}
2. C
program to find even or odd
#include<stdio.h>
int mian()
{
int n;
printf("enter the
number\n");
scanf("%d",&n);
if(n%2==0)
{
printf("evev
number\n");
}
else
{
printf("odd
number\n");
}
}
3. C
program to find even or odd using bitwise operator
#include<stdio.h>
int main()
{
int n;
printf("enter the
number\n");
scanf("%d",&n);
if(n&0x01)
printf("odd number\n");
else
printf("even
number\n");
}
4. C program to find even or odd using goto
#include<stdio.h>
int main()
{
int n;
printf("enter the
number\n");
scanf("%d",&n);
if(n%2==0)
goto even;
else
goto odd;
even: printf("even
number\n");
return;
odd:
printf("odd number\n");
return;
}
5. C program to find greatest among three numbers
#include<stdio.h>
int main()
{
int a,b,c;
printf("enter the three
numbers\n");
scanf("%d%d%d",&a,&b,&c);
if(a>b && a>c)
{
printf("%d is
greatest",a);
}
else
{
if(b>c &&
b>a)
{
printf("%d is greatest",b);
}
else
{
printf("%d is greatest",c);
}
}
}
6. C program to find palindrome number or not
#include<stdio.h>
int main()
{
int n,rem,rev=0,temp;
printf("enter the
number\n");
scanf("%d",&n);
temp=n;
while(temp!='\0')
{
rev=rev*10;
rev=rev+temp%10;
temp=temp/10;
}
printf("reverse
number:%d\n",rev);
if(rev==n)
printf("%d is
palindrome number\n",n);
else
printf("not
palindrome number");
}
7. C
program to generate fibanacci series
#include<stdio.h>
int main()
{
int a=0,b=1,n,i,c;
printf("enter the
number\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
c=a+b;
a=b;
b=c;
printf("%d
",c);
}
}
8. C
program to swap two numbers
#include<stdio.h>
int main()
{
int a,b,temp;
printf("eneter the two
numbers\n");
scanf("%d%d",&a,&b);
printf("before swapping\n");
printf("a=%d b=%d\n",a,b);
temp=a;
a=b;
b=temp;
printf("after swapping\n");
printf("a=%d b=%d",a,b);
}
9. C
program to swap two numbers without third variable
#include<stdio.h>
int main()
{
int a,b;
printf("enter the two
numbers\n");
scanf("%d%d",&a,&b);
printf("before the
swapping\n");
printf("a=%d b=%d\n",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("after swapping\n");
printf("a=%d b=%d",a,b);
}
10. C
program to swap two numbers with bitwise operator
#include<stdio.h>
int main()
{
int a,b;
printf("enter the two
numbers\n");
scanf("%d%d",&a,&b);
printf("before swapping\n");
printf("a=%d b=%d\n",a,b);
a=a^b;
b=a^b;
a=a^b;
printf("after swapping\n");
printf("a=%d b=%d",a,b);
}
11. C
program to find string palindrome or not
#include<string.h>
#include<stdio.h>
int main(){
char str[20],rev[20];
int i,j;
printf("\nEnter a string:");
gets(str);
for(i=strlen(str)-1,j=0;i>=0;i--,j++)
rev[j]=str[i];
rev[j]='\0';
printf("reverse string:%s\n",rev);
if(strcmp(rev,str)==0)
printf("The string is a
palindrome\n");
else
printf("The string is a
not palindrome");
}
12. C
program to find factorial
#include<stdio.h>
int main()
{
int n,fact=1;
printf("enter the
number\n");
scanf("%d",&n);
while(n>0)
{
fact=fact*n;
n--;
}
printf("factorial %d",fact);
}
13. C
program to find prime number or not
#include<stdio.h>
int main()
{
int n,i;
printf("enter the
number\n");
scanf("%d",&n);
for(i=2;i<=n-1;i++)
{
if(n%2==0)
{
printf("not prime
number\n");
break;
}
}
if(i == n)
printf("prime
number\n");
}
14. C
program to swap two nibbles
#include<stdio.h>
//void displaybit(int x);
int main()
{
int n,temp1,temp2,result;
printf("enter the
number\n");
scanf("%x",&n);
temp1=n&0x0f;
temp1=temp1<<4;
temp2=n&0xf0;
temp2=temp2>>4;
result=temp1|temp2;
printf("%x",result);
}
15. C
program to find Armstrong number
#include<stdio.h>
int mian()
{
int sum=0,n,temp,d,rem;
printf("enter the
number\n");
scanf("%d",n);
temp=n;
while(n>0)
{
rem=temp%10;
d=rem*rem*rem;
sum=sum+d;
temp=temp/10;
}
printf("%d",sum);
if(n==sum)
printf("armstrong number\n");
else
printf("not armstrong");
}
16. C
program to sort numbers in ascending order
#include<stdio.h>
int main()
{
int a[5],i,j,t;
printf("Enter 5 nos.\n\n");
for (i=0;i<5;i++)
scanf("%d",&a[i]);
for (i=0;i<5-1;i++)
{
for(j=i+1;j<5;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf("Ascending Order is:");
for(j=0;j<5;j++)
printf("\n%d",a[j]);
}
17. C
program to sort numbers in descending order
#include<stdio.h>
int main()
{
int a[5],i,j,t;
printf("Enter 5 nos.\n\n");
for (i=0;i<5;i++)
scanf("%d",&a[i]);
for (i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(a[i]<a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf("descending Order is:");
for(j=0;j<5;j++)
printf("\n%d",a[j]);
}
18. C
program to print ASCII values from 0-255
#include<stdio.h>
int main()
{
int i,a=0;
for(i=0;i<=255;i++)
{
printf("%d:%c\n",a,i);
a++;
}
}
19. C
program to convert decimal to binary number
#include<stdio.h>
int main()
{
int n,i,mask;
printf("enter the
number\n");
scanf("%d",&n);
for(i=31;i>=0;i--)
{
mask=0x01<<i;
if((mask&n)==0)
{
printf("0");
}
else
{
printf("1");
}
if(i%4==0)
printf(" ");
}
}
20. C
program to perform implicit conversion
#include<stdio.h>
int main()
{
char a='A';//implicit conversion
the value of A is coverted in to as a ASCII value
int b=5,c;
c=a+b;
printf("%d",c);
}
21) C program
to find even or odd?
#include<stdio.h>
int main()
{
int n,z=0x01;
printf("enter the
number\n");
scanf("%d",&n);
(n&z) &&
printf("odd")||printf("even");
}
22) C program
to display the alphabets in given range?
#include<stdio.h>
int main()
{
int n,m,i;
char
a[]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
printf("enter the range of
numbers\n");
scanf("%d%d",&n,&m);
for(n;n<=m;n++)
{
printf("%c",a[n-1]);
}
}
23) C program
to find given number even or odd without using if condition?
#include<stdio.h>
int main()
{
int n,z=0x01;
printf("enter the
number\n");
scanf("%d",&n);
(n&z) &&
printf("odd")||printf("even");
}
24) C program
to perform explicit type conversion?
#include<stdio.h>
int main()
{
int a=5,b=3;
float c,d;
c=a/b;
printf("%d\n",c);
printf("%f\n",c);
d=(float)a/b; //explicit
conversion
printf("%f",d);
}
25) C program
to find given year leap year or not?
#include<stdio.h>
int main()
{
int n;
printf("enter the
year\n");
scanf("%d",&n);
if(n%4==0 || n%100==0)
{
printf("leap year\n");
}
else
{
printf("not leap year\n");
}
}
26) C program to print multiplication table?
#include<stdio.h>
int main()
{
int table,n=1,mul,i;
printf("enter the
number\n");
scanf("%d",&table);
for(i=0;i<10;i++)
{
mul=
table*n;
printf("%d*%d=%d\n",table,n,mul);
n++;
}
}
27) C program
to count no.of digits in given number?
#include<stdio.h>
int main()
{
int n,c=0;
printf("enter the
number\n");
scanf("%d",&n);
while(n>0)
{
n=n/10;
c++;
}
printf("digits:%d",c);
}
28) C program
to perform operation by giving operator?
#include<stdio.h>
int main()
{
int d;
char c;
int a=50,b=3;
// printf("enter the
operator\n");
while(1)
{
printf("enter the
operator\n");
scanf("%c",&c);
if(c==42)
{
d=a*b;
printf("product:%d\n",d);
break;
}
else
if(c==43)
{
d=a+b;
printf("sum:%d\n",d);
break;
}
else
if(c==47)
{
d=a/b;
printf("div:%d\n",d);
break;
}
else
if(c==45)
{
d=a-b;
printf("sub:%d\n",d);
break;
}
else
if(c==37)
{
d=a%b;
printf("module:%d",d);
break;
}
}
}
29) C program to find product of two numbers without
using * operator?
#include<stdio.h>
int main()
{
int result=0,i;
int a,b;
printf("enter the two
nums\n");
scanf("%d%d",&a,&b);
for(i=0;i<b;i++)
result=result+a;
printf("product:%d",result);
}
30) C programto
count string length?
#include<stdio.h>
#define max 50
int main()
{
int i=0,n;
char s[max];
printf("entr the
string\n");
gets(s);
while(s[i]!='\0')
{
i++;
}
printf("the length of the
string is %d charecters",i++);
}
31) C programto find sum and product of given
number?
#include<stdio.h>
int main()
{
int n,sum=0,rem,product=1;
printf("enter the
number\n");
scanf("%d",&n);
while(n!='\0')
{
rem=n%10;
sum=sum+rem;
product=product*rem;
n=n/10;
}
printf("the sum of given
number: %d\n",sum);
printf("the product of given
number: %d",product);
}
Nice set of c programming language questions for beginners. I found fer more c tricks puzzles questions.
ReplyDeleteIt is very good to maintain the stuff like this.
ReplyDeleteI have a concern about the program " 25) C program to find given year leap year or not?".
This program is not correct. Please check https://www.programiz.com/c-programming/examples/leap-year
good collection of programs and it is very useful for beginners. and some logics are also very good.
ReplyDeletefinding even or odd logic using condtional statements is new logic i have seen thank u.
Program number 13 is wrong. The condition should be ( n%i == 0)
ReplyDeleteIn program 15, in scanf function &n is missing
ReplyDelete