0% found this document useful (0 votes)
102 views25 pages

C Programming Basics

The document contains 30 code snippets demonstrating various C programming concepts like input/output, arithmetic operations, conditional statements, loops, functions, arrays, pointers, structures, unions and more. Some key concepts covered include Hello World, addition of two numbers, area of a circle, greater of two/three numbers, simple interest calculation, grade of student, factorial, Fibonacci series, finding LCM and GCD, and sum of digits.

Uploaded by

sohil khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views25 pages

C Programming Basics

The document contains 30 code snippets demonstrating various C programming concepts like input/output, arithmetic operations, conditional statements, loops, functions, arrays, pointers, structures, unions and more. Some key concepts covered include Hello World, addition of two numbers, area of a circle, greater of two/three numbers, simple interest calculation, grade of student, factorial, Fibonacci series, finding LCM and GCD, and sum of digits.

Uploaded by

sohil khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

1.

Hello Word
#include<stdio.h>
int main(){
printf("Hello Word");
return 0;
}

2.Add two different number


#include<stdio.h>
int main(){
int a,b,s;
printf("enter two value");
scanf("%d%d",&a,&b);
s=a+b;
printf("s=%d",s);
}

3.Area of circle
#include<stdio.h>
int main (){
int r;
float PI=3.14,area;
printf("enter r");
scanf("%d",&r);
area=PI*r*r;
printf("%f",area);
return 0;
}

4.Age of father
#include<stdio.h>
int main (){
int x,y;
printf("enter x");
scanf("%d",&x);
y=3*x+5;
printf("%d",y);
return 0;
}

5.Simple Interest
#include<stdio.h>
int main(){
int p,r,t;
float s;
printf("enter p");
scanf("%d",&p);
printf("enter r");
scanf("%d",&r);
printf("enter t");
scanf("%d",&t);
s=p*r*t/100;
printf("%f",s);
return 0;
}

6.Greater b/w two number (if else)


#include<stdio.h>
int main(){
int n,m;
printf("enter two value");
scanf("%d%d",&n,&m);
if(n>m)
printf("greater no.is n%d",n);
else
printf("greater no.is m%d",m);
return 0;
}

7.Greater b/w three number


#include<stdio.h>
int main(){
int a,b,c;
printf("enter three value");
scanf("%d%d%d",&a,&b,&c);
if((a>b)&&(a>c))
printf("%d",a);
if((b>c)&&(b>a))
printf("%d",b);
if((c>a)&&(c>b))
printf("%d",c);
return 0;
}

8.Grade of student (if else ladder)


#include<stdio.h>
int main(){
float p;
char grade;
printf("enter p");
scanf("%f",&p);
if(p>=90)
{ grade='A';}
else{
if(p>=80&&p<90)
{ grade='B';}
else{
if(p>=60&&p<=79)
{ grade='C';}
else{
if(p>=33&&p<60)
{ grade='D';}
}
}
}
printf("%c",grade);
return 0;
}
9.Week day number (Switch)
#include<stdio.h>
int main(){
int week;
printf("enter week no.(1-7)");
scanf("%d",&week);
switch(week)
{
case 1:
printf("monday");
break;
case 2:
printf("tuesday");
break;
case 3:
printf("wednesday");
break;
case 4:
printf("thursday");
break;
case 5:
printf("friday");
break;
case 6:
printf("saturday");
break;
case 7:
printf("sunday");
break;
default:
printf("chup kr be");
}
return 0;
}

10.opreasion on two variable (Switch)


#include<stdio.h>
int main(){
int a,b,n,c;
printf("enter two no.");
scanf("%d%d",&a,&b);
printf("1.add,2.sub,3.multiply,4.divide,5.modules");
printf("enter the choice(1-5)");
scanf("%d",&n);
switch(n)
{
case 1:
c=a+b;
printf("%d",c);
break;
case 2:
c=a-b;
printf("%d",c);
break;
case 3:
c=a*b;
printf("%d",c);
break;
case 4:
c=a/b;
printf("%d",c);
break;
case 5:
c=a%b;
break;
printf("%d",c);

default:
printf("chup kr be");
}
return 0;
}

11.for loop
#include<stdio.h>
int main (){
int a;
/*for loop execution*/
for(a=10;a<20;a=a+1)
{
printf("value of a %d \n",a);
}
return 0;
}

12.preincrement-postincrement (Operators)
#include<stdio.h>
int main(){
int a=10;
printf("value of a is %d\n",++a);
printf("value of a is %d\n",a++);
printf("a=%d",a);
return 0;
}

13.while loop
#include<stdio.h>
int main(){
int a=10;
/*while loop execution*/
while(a<20)
{
printf("value of a %d\n",a);
a++;
}
return 0;
}
14.do while loop
#include<stdio.h>
int main (){
/*local variable defination*/
int a=10;
/*do loop condition*/
do{
printf("value of a %d\n",a);
a=a+1;
}
while(a<20);
return 0;
}

15.Break Statement
#include<stdio.h>
int main (){
/*local variable defination*/
int a=10;
/*while loop execution*/
while(a<20)
{
printf("vale of a=%d\n",a);
a++;
if(a>15){
/*terminatates the loop using*/
break;
}
}
return 0;
}

16.Add first n natural number


#include<stdio.h>
int main(){
int sum=0,i,n;
printf("enter value of n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+i;
printf("sum %d\n",sum);
}
return 0;
}

17.continue Statement
#include<stdio.h>
int main (){
/*local variable defination*/
int a=10;
/*do loop execution*/
do{
if(a==15)
{
/*skip the interation*/
a=a+1;
continue;
}
printf("value of a=%d\n",a);
a++;
}
while(a<20);
return 0;
}

18.Factoral of positive number


#include<stdio.h>
int main(){
int factorial=1,n,i;
printf("enter value of n");
scanf("%d",&n);
for(i=1;i<=n;i++){
factorial=factorial*i;
}
printf("factorial %d\n",factorial);
return 0;
}

19.goto statement
#include<stdio.h>
int main(){
/*local variable definition*/
int a=10;
/*do loop execution*/
loop: do{
if(a==15){
/*skip the interation*/
a=a+1;
goto loop;
}
printf("value of a=%d\n",a);
a++;
}
while(a<20);
return 0;
}

20.Given number is even or odd


#include<stdio.h>
int main(){
int n,even,odd;
printf("enter the n");
scanf("%d",&n);
if(n%2==0)
printf("n is even %d",even);
else
printf("n is odd %d",odd);
return 0;
}

21.array
#include<stdio.h>
int main (){
int n[10];
/*n is an array of 10 intergers*/
int i,j;
/*intialize element of array n*/
for(i=0;i<10;i++)
{
n[i]=i+100;
/*set element atlocation i to i+100*/
}
/*output each array element value*/
for(j=0;j<10;j++)
printf("n[%d]=%d\n",j,n[j]);
return 0;
}
22.Two Dimensional array
#include<stdio.h>
int main(){
int a[5][2]{{0,0},{1,2},{2,4},{3,6},{4,8}};
int i,j;
for(i=0;i<5;i++){
for(j=0;j<2;j++){
printf("a[%d][%d]=%d\n",i,j,a[i][j]);
}
}
return 0;
}

23.Sum of prime number


#include<stdio.h>
int main(){
int i,num,count,sum=0;
for(num=1;num<=10;num++)
{
count=0;
for(i=2;i<=num/2;i++){
if(num%i==0)
{
count++;
break;
}
}
if(count==0&&num!=1)
{
sum=sum+num;
}
}
printf("sum between 1 to 10=%d",sum);
return 0;
}

24.Structure
#include<stdio.h>
#include<string.h>
struct books{
char title[50];
char author[50];
char subject[100];
int book_id;
};
int main(){
struct books book1;
/*declare book1 of type book*/
struct books book2;
/*declare book2 of type book*/
/*book1 specification*/
strcpy(book1.title,"telecome billing tutorial");
strcpy(book1.author,"nuhan ali");
strcpy(book1.subject,"c programing tutorial");
book1.book_id=6495407;
/*book2 specification*/
strcpy(book2.title,"telecome billing");
strcpy(book2.author,"zain ali");
strcpy(book2.subject,"c programming tutorial");
book2.book_id=6495007;
/*print book1 info*/
printf("book1 title:%s\n",book1.title);
printf("book1 author:%s\n",book1.author);
printf("book1 subject:%s\n",book1.subject);
printf("book1 book_id:%d\n",book1.book_id);
/*print book2 info*/
printf("book2 title:%s\n",book2.title);
printf("book2 author:%s\n",book2.author);
printf("book2 subject:%s\n",book2.subject);
printf("book2 book_id:%d\n",book2.book_id);
return 0;
}

25.Union
#include<stdio.h>
#include<string.h>
union books{
char title[50];
char author[50];
char subject[100];
int book_id;
};
int main(){
union books book1;
/*declare book1 of type book*/
union books book2;
/*declare book2 of type book*/
/*book1 specification*/
strcpy(book1.title,"telecome billing tutorial");
strcpy(book1.author,"nuhan ali");
strcpy(book1.subject,"c programing tutorial");
book1.book_id=6495407;
/*book2 specification*/
strcpy(book2.title,"telecome billing");
strcpy(book2.author,"zain ali");
strcpy(book2.subject,"c programming tutorial");
book2.book_id=6495007;
/*print book1 info*/
printf("book1 title:%s\n",book1.title);
printf("book1 author:%s\n",book1.author);
printf("book1 subject:%s\n",book1.subject);
printf("book1 book_id:%d\n",book1.book_id);
/*print book2 info*/
printf("book2 title:%s\n",book2.title);
printf("book2 author:%s\n",book2.author);
printf("book2 subject:%s\n",book2.subject);
printf("book2 book_id:%d\n",book2.book_id);
return 0;
}

26.sizeof operator
#include<stdio.h>
int main(){
int a=8;
printf("size of varible of a:%d\n",sizeof(a));
printf("size of int data type:%d\n",sizeof(int));
printf("size of float data type:%d\n",sizeof(float));
printf("size of char data type:%d\n",sizeof(char));
printf("size of double data type:%d\n",sizeof(double));
return 0;
}

27.Fibonacci series
#include<stdio.h>
int main(){
int i=0,number,next,first=0,second=1;
printf("please enter the range number");
scanf("%d",&number);
while(i<number){
if(i<=1){
next=i;
}
else{
next=first+second;
first=second;
second=next;
}
printf("%d\t",next);
i++;
}
return 0;
}

28.Malloc
#include<stdio.h>
#include<stdlib.h>
int main(){
int *ptr;
int n,i;
printf("enter number of element:");
scanf("%d",&n);
ptr=(int*) malloc(n* sizeof(int));
if(ptr==NULL){
printf("memory not allocated");
exit(0);
}
else{
printf("memory succesfully allocated using");
for(i=0;i<n;i++){
ptr[i]=i+1;
}
printf("the element of the array are\n");
for(i=0;i<n;i++){
printf("%d\n",ptr[i]);
}
}
return 0;
}

29.Find Lcm and Gcd of two positive number


#include<stdio.h>
int main(){
int a,b,i,lcm,gcd;
printf("enter two no");
scanf("%d%d",&a,&b);
for(i=1;i<=a&i<=b;i++){
if(a%i==0&b%i==0){
gcd=i;
}
}
printf("gcd=%d\n",gcd);
lcm=a*b/gcd;
printf("lcm=%d\n",lcm);
return 0;
}

30. Sum of digits of positive number


#include<stdio.h>
int main(){
int n,sum=0,x,y;
printf("enter the number");
scanf("%d",&n);
while(n>0){
x=n/10;
y=n%10;
sum=sum+y;
n=x;
}
printf("sum=%d",sum);
return 0;
}

You might also like